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 5 of 5

Thread: Do-While Loop Help

  1. #1
    Junior Member
    Join Date
    Sep 2017
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exclamation Do-While Loop Help

    My code keeps showing me an error and saying that my "redo" in the end part of my do while loop the symbol is not found.

    I want to be able to ask if the user would like to repeat this random number guessing and if they say "y" or "Y" then it produces anoter random number & repeats the program!

    Help please what is wrong..???

    package mylab;
    import javax.swing.JOptionPane;
     
    public class MyLab {
     
        public static void main(String[] args) {
     
            do {
            int random;
            int guess;
            String redo;
     
            random = (int)((Math.random()* 9 + 1));
            System.out.println("The random number is: " + random);
     
            // Prompt user to enter a guess for the number
            JOptionPane.showMessageDialog(null, "I have a secret number, can you guess what it is?");
     
            do {
            String stringOne = JOptionPane.showInputDialog("Guess the number: ");
            guess = Integer.parseInt(stringOne);
     
            if (guess < random)
                JOptionPane.showMessageDialog(null, "To small, try again!!!!");
     
            else if (guess > random)
                JOptionPane.showMessageDialog(null, "Too large, try again!!!");
     
            else if (guess == random)
                redo = JOptionPane.showInputDialog(null, "Bingo! Another try? (Y/N)");
     
            } while (guess != random);
     
            } while (redo == "Y" || redo == "y");
        } 
     
    }

  2. #2
    Member John Joe's Avatar
    Join Date
    Jun 2017
    Posts
    276
    My Mood
    Amused
    Thanks
    8
    Thanked 19 Times in 19 Posts

    Default Re: Do-While Loop Help

    You are declaring the redo inside the do , so you will get symbol is not found error. You should declare redo outside the do.

     public static void main(String[] args) {
            String redo = null; // declare and initialize it
            do {
            int random;
            int guess;
              .......
             }
    Whatever you are, be a good one

  3. #3
    Junior Member
    Join Date
    Sep 2017
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Do-While Loop Help

    Quote Originally Posted by John Joe View Post
    You are declaring the redo inside the do , so you will get symbol is not found error. You should declare redo outside the do.

     public static void main(String[] args) {
            String redo = null; // declare and initialize it
            do {
            int random;
            int guess;
              .......
             }
    Yeah but when I try that when I type "Y" in the input for if I'd like to run the program again it just ends the program after that and does not repeat the program.. it's not doing the "do" part of the do while loop even though my while says if redo = "Y" or "y".

  4. #4
    Member John Joe's Avatar
    Join Date
    Jun 2017
    Posts
    276
    My Mood
    Amused
    Thanks
    8
    Thanked 19 Times in 19 Posts

    Default Re: Do-While Loop Help

    when I type "Y" in the input for if I'd like to run the program again it just ends the program after that and does not repeat the program
    Use equals instead of == .

    while (redo.equals( "Y") || redo.equals( "y"))
    Whatever you are, be a good one

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Do-While Loop Help

    Also there is a String method that will compare Strings ignoring the case that would simplify that statement a bit.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. loop once or loop multiple times
    By stanlj in forum Loops & Control Statements
    Replies: 3
    Last Post: November 7th, 2013, 02:14 PM
  2. help with when the for loop is met and i want to run the while loop again
    By m49er704 in forum What's Wrong With My Code?
    Replies: 9
    Last Post: March 22nd, 2013, 09:03 AM
  3. For loop, the first command in the loop does not get executed the 2nd time..
    By lina_inverse in forum Loops & Control Statements
    Replies: 1
    Last Post: October 16th, 2012, 09:00 PM
  4. [SOLVED] Please help with my while loop that turned into infinite loop!
    By Hazmat210 in forum What's Wrong With My Code?
    Replies: 9
    Last Post: March 10th, 2012, 11:22 PM
  5. Converting a while loop to a for loop and a for loop to a while loop.
    By awesom in forum Loops & Control Statements
    Replies: 3
    Last Post: February 26th, 2012, 08:57 PM

Tags for this Thread