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: Break/Continue example doesn't work with ConsoleReader

  1. #1
    Member
    Join Date
    Mar 2012
    Posts
    37
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Break/Continue example doesn't work with ConsoleReader

    class BreakExample
    {
        public static void main(String[] args)
        {
            System.out.println("(Please enter a number from 1 to 6)");
            ConsoleReader console = new ConsoleReader(System.in);
            int num = console.readInt();
            System.out.println("(Please enter another number, from 1 to 8)");
            ConsoleReader console2 = new ConsoleReader(System.in);
            int num2 = console2.readInt();
     
            for(int num = 1; num  < 6; num++)
            {
     
                for(int num2 = 1; num2 < 8; num2++)
                {
                 if(num == 4 && num2 == 1)
                 {
                     System.out.println("keep the loop going when the numbers are: " +num+ " and " + num2);
                     continue;
                    }
     
                    if( num == 5 && num2 == 3 )
     
                 {
                    System.out.println( "Break the loop when the numbers are: " +num+ " and " + num2);
                    break;
                }   
                    System.out.println("Continue with the loop when the numbers are: " +num+ "and " + num2);
                }
            }
        }
    }

    The user input with this program doesn't work. I'm having a problem with the variables that I can't seem to figure out. Any ideas on how to get this to work?


  2. #2
    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: Break/Continue example doesn't work with ConsoleReader

    user input with this program doesn't work.
    Can you explain what "doesn't work" means? If there are errors, copy the full text and pasted it here.
    If the program doesn't do what you want, please explain what you want it to do differently.

    I see nested loops. The break and continue statements as coded work in the inner loop.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Mar 2012
    Posts
    37
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Break/Continue example doesn't work with ConsoleReader

    Sorry, the error is related to this line:

    for(int num = 1; num < 6; num++)

    the error is "variable num is already defined in method main(java.lang.string{})

  4. #4
    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: Break/Continue example doesn't work with ConsoleReader

    Please copy the EXACT text of the error message and paste it here. What you posted does not make sense. There is no class: java.lang.string. The String class's name starts with a S not s

    --- Update ---

    The compiler sees another, earlier definition for the variable: num. Change one of the definitions so they are different.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Mar 2012
    Posts
    37
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Break/Continue example doesn't work with ConsoleReader

    variable num is already defined in method main(java.lang.String[])

    That is the exact error, with for(int num = 1; num < 6; num++) being highlighted.

  6. #6
    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: Break/Continue example doesn't work with ConsoleReader

    See the update section of post #4
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Member
    Join Date
    Mar 2012
    Posts
    37
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Break/Continue example doesn't work with ConsoleReader

    class BreakExample2
    {
        public static void main(String[] args)
        {
            System.out.println("(Please enter a number from 1 to 6)");
            ConsoleReader console = new ConsoleReader(System.in);
            System.out.println("(Please enter another number, from 1 to 8)");
            ConsoleReader console2 = new ConsoleReader(System.in);
     
     
            for(int num = console.readInt(); num  < 6; num++)
            {
     
                for(int num2 = console2.readInt(); num2 < 8; num2++)
                {
                 if(num == 4 && num2 == 1)
                 {
                     System.out.println("keep the loop going when the numbers are: " +num+ " and " + num2);
                     continue;
                    }
     
                    if( num == 5 && num2 == 3 )
     
                 {
                    System.out.println( "Break the loop when the numbers are: " +num+ " and " + num2);
                    break;
                }   
                    System.out.println("Continue with the loop when the numbers are: " +num+ "and " + num2);
                }
            }
        }
    }

    I tried this, and the program compiles, but doesn't run as its supposed to. Still can't wrap my head around the variable errors.

  8. #8
    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: Break/Continue example doesn't work with ConsoleReader

    doesn't run as its supposed to
    Can you explain? What does it do now and what do you want it to do differently?

    Do you want the program to read a new value every time the loop goes around? Putting the call to the readInt() method in the for statement could cause that. Read the ending values one time into a variable and then use the variables in the for statement.

    can't wrap my head around the variable errors.
    What are the "variable errors"?
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Program doesn't work!
    By Mrcinica in forum What's Wrong With My Code?
    Replies: 15
    Last Post: May 16th, 2013, 10:23 AM
  2. [SOLVED] .equals doesn't work?
    By Purple01 in forum What's Wrong With My Code?
    Replies: 0
    Last Post: November 14th, 2012, 04:20 AM
  3. Why doesn't this work!
    By Alex-Green in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 20th, 2012, 04:25 AM
  4. Why doesn't this work?
    By mailman in forum What's Wrong With My Code?
    Replies: 6
    Last Post: January 9th, 2012, 11:19 PM
  5. Help with interrupting a loop without break or continue
    By mwr76 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 11th, 2011, 08:51 PM