# I'm so lost, trying to disable a loop or continue it on users input

• 02-16-2013, 03:25 PM
LetsG0Blue
I'm so lost, trying to disable a loop or continue it on users input
I can't figure this out. So, here's the question I'm being asked to solve.

Quote:

Repeat the Lower Bound/Upper bound lab from last time, except that you are to put a while
loop around it so that the user is asked at the end :

"Do you want to do this again? (y/n)"

If the user enters a y, then you repeat. This continues until the
user enters an n.
Now this is my Low Bound/Upper Bound code.

Code:

```import java.util.Scanner; public class NumberBounds {     public static void main(String[] args)  {         Scanner keyboard = new Scanner(System.in);         System.out.println("Enter a number");         int starting_number = keyboard.nextInt();         System.out.println("Enter a Upper Bound");         int upper_bound = keyboard.nextInt();         System.out.println("Enter a addition increment");         int step_size = keyboard.nextInt();         starting_number += step_size;         int count = 0;         while (starting_number <= upper_bound) {             System.out.print(starting_number + " ");             starting_number += step_size;             count += 1; if ((count % 10) == 0)                 System.out.println();         }     } }```
And this is my attempt to adding the "Do you want to do this again? (y/n)", that's asked in my homework. In this code it's now skipping the entire, which it isn't suppose too.

Code:

```while (starting_number <= upper_bound) {                                 System.out.print(starting_number + " ");                                 starting_number += step_size;                                 count += 1; if ((count % 10) == 0)                                         System.out.println();                         }```
section and I can't seem to make the (Y/N) work. I want it to where if the user enters a Y then it goes to (else) and continues the program, but if they enter a N then it takes the 1 which is assigned to "char N = 0" and minus's 1 making it 0, thus making the continueflag false and discontinuing the program.

Code:

```import java.util.Scanner; public class NumberBounds {         public static void main(String[] args)  {                 Scanner keyboard = new Scanner(System.in);                 boolean continueflag = true;                 char N = 1, Y = 5;                 do {                         System.out.println("Enter a number");                         int starting_number = keyboard.nextInt();                         System.out.println("Enter a Upper Bound");                         int upper_bound = keyboard.nextInt();                         System.out.println("Enter a addition increment");                         int step_size = keyboard.nextInt();                         starting_number += step_size;                         int count = 0;                         while (starting_number <= upper_bound) {                                 System.out.print(starting_number + " ");                                 starting_number += step_size;                                 count += 1; if ((count % 10) == 0)                                         System.out.println();                         }                         {                                 System.out.println("Would you like to continue (Y/N)");                                 if (N == 0)                                         System.out.println("The program has been terminated");                                 N--;                                 continueflag = false;                                 if (Y >= 1)                                         Y++;                         }                 }                 while (continueflag != false);         } }```
• 02-16-2013, 06:18 PM
AndrewM16921
Re: I'm so lost, trying to disable a loop or continue it on users input
I would do something like this:

Code:

```Scanner keyboard = new Scanner(System.in) while(true) {         //other logic here                 System.out.println("Continue (y/n)?");         String cont = keyboard.nextLine().toLowerCase();         if(cont.startsWith("n")) //if the user entered n                 break; //break out of (end) the while loop } //goes to this line after the "break" statement```
Hope this helps.
• 02-16-2013, 06:35 PM
LetsG0Blue
Re: I'm so lost, trying to disable a loop or continue it on users input
So, this is what I placed into the code you gave me.

Code:

```import java.util.Scanner; public class factorial {         public static void main(String[] args) {                 Scanner keyboard = new Scanner(System.in);                 while(true)                 {                         while (true){                                 int Factorial = 1, Number;                                 {                                 System.out.println("Enter a value for N");                                 Number = keyboard.nextInt();                                 }                                 for (int K = 1;K <= Number; K++)                                 {                                         Factorial *= K;                                 }                                 System.out.println("The Factorial of "+Number +" is "+ Factorial);                                                                 System.out.println("Continue (y/n)?");                                 String cont = keyboard.nextLine().toLowerCase();                                 if(cont.startsWith("n"))                                         break;                         }                 }         } }```
But this is what my output is

Code:

```Enter a value for N 6 The Factorial of 6 is 720 Continue (y/n)? Enter a value for N y Exception in thread "main" java.util.InputMismatchException         at java.util.Scanner.throwFor(Unknown Source)         at java.util.Scanner.next(Unknown Source)         at java.util.Scanner.nextInt(Unknown Source)         at java.util.Scanner.nextInt(Unknown Source)         at factorial.main(factorial.java:14)```
• 02-16-2013, 06:54 PM
LetsG0Blue
Re: I'm so lost, trying to disable a loop or continue it on users input
I figured it out
• 02-16-2013, 07:07 PM
jim829
Re: I'm so lost, trying to disable a loop or continue it on users input
First, I would recommend getting rid of the outer while(true) loop. I don't believe you need it. Second, apparently there is still
some residual characters in the input stream from your first use of keyboard.nextInt(). So put a keyboard.nextLine() right after
it and see what happens.

Jim
• 02-16-2013, 07:28 PM
AndrewM16921
Re: I'm so lost, trying to disable a loop or continue it on users input
Sorry about that. The problem is that when you use Scanner.nextInt(), the user enters this with the "enter" key, which leaves a newline character. You need to "consume" this new line by calling nextLine() right after. Also, you have two while loops, you should only have one in this case. I provided a modified version of your code below.
Code:

```import java.util.Scanner; public class factorial {         public static void main(String[] args)         {                 Scanner keyboard = new Scanner(System.in);                 while(true)                 {                         int Factorial = 1, Number;                         {                                 System.out.println("Enter a value for N");                                 Number = keyboard.nextInt();                                 keyboard.nextLine();                         }                         for(int K = 1; K <= Number; K++)                         {                                 Factorial *= K;                         }                         System.out.println("The Factorial of " + Number + " is " + Factorial);                                                 System.out.println("Continue (y/n)?");                         String cont = keyboard.nextLine().toLowerCase();                         if(cont.startsWith("n"))                                 break;                 }                 keyboard.close();         } }```

Also, for the sake of convention, class names are usually CapitalizedCamelCase, and variables are usually lowerCamalCase. This technically doesn't matter, but it may be a tad bit confusing for others reading your code. You seem to most of these switched in your code.