Results 1 to 4 of 4
  1. #1
    Join Date
    Feb 2014
    Posts
    15
    Rep Power
    0

    Default Looping: Using JOptionPane.showConfirmDialog

    Hi there Java enthusiasts!
    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.
    (My program doesn't give an error with a message, it just doesn't loop how I need it to)

    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, what is wrong with my loop?
    And what can I do to fix my loop in order to achieve my showConfirmDialog needs?

    I know this is a long post,
    Thank you very much for taking the time to read it.

    - A java Noob!

  2. #2
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,900
    Rep Power
    5

    Default Re: Looping: Using JOptionPane.showConfirmDialog

    Don't post duplicates of the same question, one thread is enough.

    Looping error

    Since this question has nothing at all to do with Netbeans, I'd try to stick to this thread.

    Are you also posting this to other forum sites? If you do, post links to your duplicate threads so people can follow them and see what has already been answered.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

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

    Default Re: Looping: Using JOptionPane.showConfirmDialog

    No this is the only website that I have ever used, and became a member just this morning.


    Sorry, I was using Netbeans to program Java, so I thought it applied as a Netbeans question, now I know better... apologies...
    Last edited by NoviceJavaNoob; 02-28-2014 at 12:52 PM.

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

    Default Re: Looping: Using JOptionPane.showConfirmDialog

    Again, If anyone can infer what my error is/what can be done to fix it,
    that'd be so much appreciated!

Similar Threads

  1. showConfirmDialog() - Language Options
    By Zelaine in forum New To Java
    Replies: 1
    Last Post: 01-10-2014, 09:27 PM
  2. JOptionPane looping?
    By raffs03 in forum New To Java
    Replies: 11
    Last Post: 06-16-2013, 01:01 PM
  3. MVC + JOptionPane.showConfirmDialog
    By mike101 in forum New To Java
    Replies: 3
    Last Post: 02-02-2013, 01:36 PM
  4. [SOLVED] JOptionPane.ShowConfirmDialog() problem
    By jon80 in forum New To Java
    Replies: 15
    Last Post: 06-03-2009, 02:37 AM
  5. JOptionPane - showConfirmDialog(...) options
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-17-2007, 09:38 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
  •