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

Thread: I can't get this loop to work correctly

  1. #1
    Junior Member
    Join Date
    Feb 2011
    Posts
    15
    Thanks
    1
    Thanked 3 Times in 3 Posts

    Default I can't get this loop to work correctly

    I'm trying to get this code to work but it will only ask for the first number to be entered again. Anytime the first number is below 10 or above 99 it will ask for the first number to be entered, but as soon as the second number is entered outside of the range it will just print the error message and ask for the third number without again requiring input for the second. This happens again for the third number. Do I need a loop encompassing all of these inputs? Should the boolean "ok" be reset as false after each input number? Any ideas as to why would be greatly appreciated. Thanks.

        public void setCoefficients() 
    	{
     
     
     
    		Scanner inputDevice = new Scanner(System.in);
     
    	JOptionPane.showMessageDialog(null, "Welcome to the lottery game! ");
     
    	do
    	{
    	  System.out.println("Please enter your first number between 10 and 99: ");	
    	  a = inputDevice.nextInt();
    	  if(a<10||a>99)
    	   System.out.println("Please enter a number between 10 and 99 ");
    	  else 
    		  ok=true; 
    	}while(!ok); 
     
    	 do
    	 { System.out.println("Please enter your second number between 10 and 99: ");
    	   b = inputDevice.nextInt();
    	   if(b<10||b>99)
    	    System.out.println("Please enter a number between 10 and 99 ");	 
    	   else 
    	      ok=true;
    	 }while(!ok);
     
    	 do
    	 {System.out.println("Please enter your last number between 10 and 99: ");
    	 c = inputDevice.nextInt();
    	 if(c<10||c>99)
    	  System.out.println("Please enter a number between 10 and 99 ");	 
    	 else 
    		 ok=true;
    	 }while(!ok);	
     
     
    	}
    Last edited by Nismoz3255; February 27th, 2011 at 04:16 PM.


  2. #2
    Junior Member
    Join Date
    Feb 2011
    Posts
    15
    Thanks
    1
    Thanked 3 Times in 3 Posts

    Default Re: I can't get this loop to work correctly

    It does work if I reset "ok" after each do-while loop. Sometimes it helps asking the questions to figure out the answers I guess. Ha.

Similar Threads

  1. [SOLVED] Simple While loop wont work??
    By chuckie987 in forum Loops & Control Statements
    Replies: 1
    Last Post: January 31st, 2011, 02:58 PM
  2. Replies: 3
    Last Post: November 9th, 2010, 01:19 PM
  3. Need help getting things to loop correctly
    By egruna2 in forum What's Wrong With My Code?
    Replies: 6
    Last Post: September 9th, 2010, 05:53 PM
  4. Can a for loop work with random number?
    By humdinger in forum Loops & Control Statements
    Replies: 7
    Last Post: January 10th, 2010, 10:06 PM
  5. Why won't this while loop work?
    By trueblue in forum Loops & Control Statements
    Replies: 2
    Last Post: July 17th, 2009, 10:10 AM