Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: How can I make a loop repeat?

  1. #1
    Junior Member
    Join Date
    Feb 2013
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default How can I make a loop repeat?

    import javax.swing.JOptionPane;
    public class program7 {
     
        public static void main(String[] args) {
     
    //ask user how many euros a dollar buys
    String euroExchangeRateString = JOptionPane.showInputDialog
            (null, "Enter how many Euros one Dollar will buy");
    double euroExchangeRate = Double.parseDouble(euroExchangeRateString);
     
    //ask user how many pounds a dollar will buy
    String poundExchangeRateString = JOptionPane.showInputDialog
            (null, "Enter how many Pounds one Dollar will buy");
    double poundExchangeRate =  Double.parseDouble(poundExchangeRateString);
     
    //ask user how many yen a dollar will buy
    String yenExchangeRateString = JOptionPane.showInputDialog
            (null, "Enter how many Yen one Dollar will buy");
    double yenExchangeRate = Double.parseDouble(yenExchangeRateString);
     
    //ask user to enter the number of dollars they have
     
        String amountDollarsString = JOptionPane.showInputDialog
    (null, "Enter the amount of foreign currency you want to buy in U.S. dollars");
    double amountDollars = Double.parseDouble(amountDollarsString);
    if (amountDollars <= 100){
    amountDollars -= amountDollars * 0.10;
    JOptionPane.showMessageDialog
            (null,"You can buy $" + amountDollars + " in foreign currency.");
    }
    else if (amountDollars > 100) {
    amountDollars -= amountDollars * 0.05;
    JOptionPane.showMessageDialog
            (null,"You can buy $" + amountDollars + " in foreign currency.");
        }
     
     
    String reply = JOptionPane.showInputDialog 
        (null, "Enter 'E' to buy Euros, 'P' to buy Punds or 'Y' to buy Yen");
    char firstChar = reply.charAt(0);
     
    while (firstChar == 'E' || firstChar == 'e'){
    double amountEuros = (amountDollars * euroExchangeRate);
    JOptionPane.showMessageDialog
            (null, "You have purchased " + amountEuros + " Euros.");
    }
    if (firstChar == 'P' || firstChar == 'p') {
    double amountPounds = (amountDollars * poundExchangeRate);
    JOptionPane.showMessageDialog
            (null, "You have purchased " + amountPounds + " Pounds.");
    }
    else if (firstChar == 'Y' || firstChar == 'y') {
    double amountYen = (amountDollars * yenExchangeRate);
    JOptionPane.showMessageDialog
            (null, "You have purchased " + amountYen + " Yen.");
    }
    JOptionPane.showConfirmDialog
            (null, "Are there any more conversions you would like to perform?");
            }
        }

    On the last part, the program is supposed to ask the user if there are anymore conversions to perform. I put in a confirm dialog box, but when the user clicks yes it does not go back to repeat the previous loop. How can I make it do that?


  2. #2
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: How can I make a loop repeat?

    Please edit your code so that its format is readable including using standard and consistent indentation. Next, any code that you want to repeat must be enclosed in a loop, and I don't see you doing this. Enclose it all that needs to repeat in a while loop.

  3. #3
    Junior Member
    Join Date
    Feb 2013
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How can I make a loop repeat?

    Okay I have enclosed the loop, but it still won't repeat when yes is selected. I have also attempted to make the program more readable with indentations
    import javax.swing.JOptionPane;
    public class program7 {
     
        public static void main(String[] args) {
     
    //ask user how many euros a dollar buys
    String euroExchangeRateString = JOptionPane.showInputDialog
            (null, "Enter how many Euros one Dollar will buy");
        double euroExchangeRate = Double.parseDouble(euroExchangeRateString);
     
    //ask user how many pounds a dollar will buy
    String poundExchangeRateString = JOptionPane.showInputDialog
            (null, "Enter how many Pounds one Dollar will buy");
        double poundExchangeRate =  Double.parseDouble(poundExchangeRateString);
     
    //ask user how many yen a dollar will buy
    String yenExchangeRateString = JOptionPane.showInputDialog
            (null, "Enter how many Yen one Dollar will buy");
        double yenExchangeRate = Double.parseDouble(yenExchangeRateString);
     
    //ask user to enter the number of dollars they have
     
        String amountDollarsString = JOptionPane.showInputDialog
    (null, "Enter the amount of foreign currency you want to buy in U.S. dollars");
    double amountDollars = Double.parseDouble(amountDollarsString);
     
    if (amountDollars <= 100){
        amountDollars -= amountDollars * 0.10;
        JOptionPane.showMessageDialog
            (null,"You can buy $" + amountDollars + " in foreign currency.");
    }
    else if (amountDollars > 100) {
        amountDollars -= amountDollars * 0.05;
        JOptionPane.showMessageDialog
            (null,"You can buy $" + amountDollars + " in foreign currency.");
        }
     
     
    String reply = JOptionPane.showInputDialog 
        (null, "Enter 'E' to buy Euros, 'P' to buy Punds or 'Y' to buy Yen");
    char firstChar = reply.charAt(0);
     
    while (firstChar == 'E' || firstChar == 'e'){
        double amountEuros = (amountDollars * euroExchangeRate);
        JOptionPane.showConfirmDialog
            (null, "You have purchased " + amountEuros + 
            " Euros. Are there any other conversions you would like to perform?");
    }
    if (firstChar == 'P' || firstChar == 'p') {
        double amountPounds = (amountDollars * poundExchangeRate);  
        JOptionPane.showConfirmDialog
            (null, "You have purchased " + amountPounds + 
            " Pounds. Are there any other conversions you would like to perform");
    }
    else if (firstChar == 'Y' || firstChar == 'y') {
        double amountYen = (amountDollars * yenExchangeRate);
        JOptionPane.showConfirmDialog
            (null, "You have purchased " + amountYen + 
            " Yen. Are there any other conversions you would like to perform");
    }
    }
    }

  4. #4
    Member Zyrion's Avatar
    Join Date
    Feb 2013
    Location
    Iowa
    Posts
    106
    My Mood
    Angelic
    Thanks
    2
    Thanked 8 Times in 8 Posts

    Default Re: How can I make a loop repeat?

    I do not like looking at your code, it is very difficult to see your program's flow. Indent your program control statements effectively so the code is easier to follow.

  5. #5
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: How can I make a loop repeat?

    I agree with Zyrion. It's hard to see what block of code encloses another block of code if your indentation style is wrong. You should indent each block about 3 spaces to the right of the indentation of the block that encloses it. If you don't do this, it will be very difficult for you or us to debug your code.
    Last edited by Norm; March 9th, 2013 at 12:20 PM. Reason: to left vs to right?

  6. #6
    Junior Member
    Join Date
    Feb 2013
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How can I make a loop repeat?

    Okay, I have made some indentations at key points and just including the final loop. This is the part I want fixed.
    String reply = JOptionPane.showInputDialog 
        (null, "Enter 'E' to buy Euros, 'P' to buy Punds or 'Y' to buy Yen");
     
        char firstChar = reply.charAt(0);
     
    //begin loop
    while (firstChar == 'E' || firstChar == 'e'){
     
        double amountEuros = (amountDollars * euroExchangeRate);
     
        JOptionPane.showConfirmDialog
            (null, "You have purchased " + amountEuros + 
            " Euros. Are there any other conversions you would like to perform?");
    }
     
    if (firstChar == 'P' || firstChar == 'p') {
     
        double amountPounds = (amountDollars * poundExchangeRate);  
     
        JOptionPane.showConfirmDialog
            (null, "You have purchased " + amountPounds + 
            " Pounds. Are there any other conversions you would like to perform");
    }
     
    else if (firstChar == 'Y' || firstChar == 'y') {
     
        double amountYen = (amountDollars * yenExchangeRate);
     
        JOptionPane.showConfirmDialog
            (null, "You have purchased " + amountYen + 
            " Yen. Are there any other conversions you would like to perform");
    }
    }
    }

    When Yes is selected, I want the program to go back to the beginning of the loop

  7. #7
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: How can I make a loop repeat?

    Let me repost some of your code:

          String reply = JOptionPane.showInputDialog(null,
                "Enter 'E' to buy Euros, 'P' to buy Punds or 'Y' to buy Yen");
          char firstChar = reply.charAt(0);
     
          // begin loop
          while (firstChar == 'E' || firstChar == 'e') {
             double amountEuros = (amountDollars * euroExchangeRate);
             JOptionPane.showConfirmDialog(null, "You have purchased " + amountEuros
                   + " Euros. Are there any other conversions you would like to perform?");
          }
     
          if (firstChar == 'P' || firstChar == 'p') {
             double amountPounds = (amountDollars * poundExchangeRate);
             JOptionPane.showConfirmDialog(null, "You have purchased " + amountPounds
                   + " Pounds. Are there any other conversions you would like to perform");
          }
     
          else if (firstChar == 'Y' || firstChar == 'y') {
             double amountYen = (amountDollars * yenExchangeRate);
             JOptionPane.showConfirmDialog(null,  "You have purchased "+ amountYen
                   + " Yen. Are there any other conversions you would like to perform");
          }

    You need to put your code aside and instead think of the logic of your program, and even write some pseudo-code from this logic. Something like:

    declare variable to check to see if user wants to continue looping.
    check the result of variable above. While they want to continue looping, do loop:
      Find out what kind of converstion to make
      if conversion is foo
        do foo
      end if
      else if conversion is bar
        do bar
      end else if
      else if conversion is baz
        do baz
      end else if
     
      find out if the user wants to continue looping and put result in a variable.
    end loop

  8. #8

    Default Re: How can I make a loop repeat?

    It appears you might want to add to the first question " or 'X' to exit"

    Then preceed the question with

    char firstChar = "";
    while ((firstChar != 'X') && ((firstChar != 'x'))) {

    Remove the exiting declaration of firstChar since it is already declared.
    Change the existing while to an "if".
    Add a } at the end

Similar Threads

  1. How can I make a loop repeat?
    By JohnEliot in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 8th, 2013, 11:08 PM
  2. Simple Repeat.
    By Tecness2 in forum Java Theory & Questions
    Replies: 3
    Last Post: May 30th, 2012, 01:35 PM
  3. Prompt user to repeat
    By Callcollect in forum Loops & Control Statements
    Replies: 18
    Last Post: December 4th, 2011, 07:15 PM
  4. How to repeat a while loop?
    By BITmixit in forum What's Wrong With My Code?
    Replies: 4
    Last Post: October 12th, 2011, 12:51 PM
  5. How to make for loop work?
    By Dragonkndr712 in forum What's Wrong With My Code?
    Replies: 9
    Last Post: March 8th, 2011, 02:14 PM