Results 1 to 2 of 2

Thread: Looping error

  1. #1
    Join Date
    Feb 2014
    Posts
    15
    Rep Power
    0

    Default Looping error

    Hello, this is my first post on the NetBeans Forum, hooray!

    First off, I started learning Java only 1 month ago, please go easy on me
    ANY help and or insight on my issue will be very much appreciated.
    I will explain what I am trying to do before showing my code.

    I have written a program that prompts a user to enter 3 values (to be used later to calculate a currency conversion).
    Next, my program begins a for loop, and asks the user for another value, it is stored into a variable.
    Then, I extract a single character from a String value that is entered into an InputDialogBox and store it into a ''char'' variable.
    I then start a large if/else if statement to decide whether or not the user entered "char" variable is equal to a certain letter in order to determine which variable of currency to use (it will be 1 of the 3 from the beginning: letter e for euro, letter p for Pound Sterling, or letter y for Yen).
    Once the if/else statement decides what the entered "char" value is, it then calls 1 of 2 methods to make the calculation.

    Everything above works just fine, HERE is where I am having trouble with my looping.

    Towards the bottom half of my large if/else if statement, I try to determine if the "char" is NOT equal to ANY of the letters E,e,P,p,Y, or y. If it is NOT equal to any of these, I would like my program to ask If the user would like to continue with the program.
    So for this, I programmed a showConfirmDialog box for the next step.

    If the user selects "YES" they want to continue, then I want to Prompt the user to enter either E, P, or Y again. If they don't enter 1 of the 3 letters, I would like to keep asking repeatedly until they enter one of those letters, then it should loop back through with a valid "char" letter to make the calculation.

    If the user selects anything other than "YES" such as "NO" or "Cancel", then I want my program to end/terminate.

    *Ideally though, I don't know how to loop back through after my 1st calculation is displayed in order to make a 2nd calculation.

    Here is my program.



    // Programmed by Dillon Voit

    package program6currencyconversion;
    import javax.swing.*;

    public class Program6CurrencyConversion {

    /**
    * @param args the command line arguments
    */
    public static void main(String[] args) {
    /* It should start by asking how many Euros a dollar will buy and
    reading that number. Then it should ask how many Pound Sterling a
    dollar will buy and read that number. Finally, it will ask how many
    Yen a dollar will buy and read that number.
    */
    double euros = Double.parseDouble(JOptionPane.showInputDialog(nul l,
    "How many Euros will a dollar buy?"));
    double poundSterling = Double.parseDouble(JOptionPane.showInputDialog(nul l,
    "How many Pound Sterling will a dollar buy?"));
    double yen = Double.parseDouble(JOptionPane.showInputDialog(nul l,
    "How many Yen will a dollar buy?"));
    /* Next the program will use a loop to let the user calculate how much
    various amounts of dollars are worth in other currencies. For each
    cycle of the loop, the program will ask the user to enter the number
    of dollars. Then the program will ask the user to enter E to buy
    Euros, P to buy Pounds, or Y to buy Yen. The program will then
    call a method to calculate the amount of currency that will be
    purchased. It will pass the number of dollars and the exchange rate
    for that currency.
    */
    for (int i = 0; i < euros && i < poundSterling && i < yen; i++){
    final double dollarInput = Double.parseDouble(JOptionPane.showInputDialog(nul l,
    "Please enter the number of dollars."));
    String currency = JOptionPane.showInputDialog(null,
    "Enter 'E' to buy Euros, 'P' to buy Pounds, or 'Y' to buy Yen.");
    char currencyLetter = currency.charAt(0);
    /* The program will then call a method to calculate the amount
    of currency that will be purchased. It will pass the number
    of dollars and the exchange rate for that currency.
    */
    if ((currencyLetter == 'E' || currencyLetter == 'e') && (dollarInput * euros) <= 100){
    JOptionPane.showMessageDialog(null, "The amount of Euros purchased is " + ConversionA(euros, dollarInput));
    } else if ((currencyLetter == 'E' || currencyLetter == 'e') && (dollarInput * euros) > 100){
    JOptionPane.showMessageDialog(null, "The amount of Euros purchased is " + ConversionB(euros, dollarInput));
    } else if ((currencyLetter == 'P' || currencyLetter == 'p') && (dollarInput * poundSterling) <= 100){
    JOptionPane.showMessageDialog(null, "The amount of Pound Sterling purchased is " + ConversionA(poundSterling, dollarInput));
    } else if ((currencyLetter == 'P' || currencyLetter == 'p') && (dollarInput * poundSterling) > 100){
    JOptionPane.showMessageDialog(null, "The amount of Pound Sterling purchased is " + ConversionB(poundSterling, dollarInput));
    } else if ((currencyLetter == 'Y' || currencyLetter == 'y') && (dollarInput * yen) <= 100){
    JOptionPane.showMessageDialog(null, "The amount of Yen purchased is " + ConversionA(yen, dollarInput));
    } else if ((currencyLetter == 'Y' || currencyLetter == 'y') && (dollarInput * yen) > 100){
    JOptionPane.showMessageDialog(null, "The amount of Yen purchased is " + ConversionB(yen, dollarInput));
    }
    /* The input and output will be performed by JOptionPane dialog boxes. At the end of the loop, the program will use a showConfirmDialog box to ask the user if there are more conversions to perform. If the user clicks the Yes button, then the loop continues. Otherwise, the loop terminates.
    */
    else if (currencyLetter != 'E'
    || currencyLetter != 'e'
    || currencyLetter != 'P'
    || currencyLetter != 'p'
    || currencyLetter != 'Y'
    || currencyLetter != 'y'){
    int keepgoing = JOptionPane.showConfirmDialog(null,
    "Would you like to continue?");
    if (keepgoing == JOptionPane.YES_OPTION){
    i++;
    } else if (keepgoing != JOptionPane.YES_OPTION){
    break;
    }
    }

    }

    }



    public static double ConversionA(double CurrencyType, double dollarAmount){
    double CurrencyPurchased;
    CurrencyPurchased = (CurrencyType * dollarAmount) - ((CurrencyType * dollarAmount) * 0.10);
    return CurrencyPurchased;
    }
    public static double ConversionB(double CurrencyType, double dollarAmount){
    double CurrencyPurchased;
    CurrencyPurchased = (CurrencyType * dollarAmount) - ((CurrencyType * dollarAmount) * 0.05);
    return CurrencyPurchased;
    }
    }





    I am not quite sure what to do here, should I have used a for loop?
    This seems like something that should be fairly simple compared to my actual calculations...
    I know this is a long post,
    Thank you very much for taking the time to read it.

    - A java Noob!

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,036
    Rep Power
    20

    Default Re: Looping error

    When posting code please wrap it in [code] tags [/code].
    Unformatted code is hard to follow.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. need some help in looping
    By toume in forum New To Java
    Replies: 5
    Last Post: 12-24-2012, 03:15 PM
  2. Looping: Help Please
    By michelle_323 in forum New To Java
    Replies: 5
    Last Post: 10-12-2011, 11:59 AM
  3. Help with While and For Looping
    By gmoney8316 in forum New To Java
    Replies: 2
    Last Post: 03-03-2010, 10:54 PM
  4. Looping Help Please
    By JonnySnip3r in forum New To Java
    Replies: 5
    Last Post: 01-31-2010, 05:57 AM
  5. Can't solve error message while looping
    By BHCluster in forum New To Java
    Replies: 15
    Last Post: 04-22-2008, 10:51 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •