Loop won't let me input new data

Does anyone know why this loop won't stop to let me enter in the data?

This is the output when I run it.

My guess, it should be in a while loop and not a try block. Try blocks were not meant to be loops themselves. Loops can be within try blocks, but a try block is meant to handle exceptions, like if you were writing a file IO program. Because, what you are doing is you are going to need to be in your loop continuously while you are reading data, and once the try loops ends on a successful try, escapes the try block. Do whiles ensure that the code is executed at least once, before checking the condition. I would stick with just a regular while as it would be simpler.

EDIT: writing some code that may help

You are taking in the double inside of the try block. But it will break from the try after it prints and skip that section of code. It looks like it should work the way it is though. Try to put that section of code after the catch block and see how that owrks out.

give this a try

here is the contents of the link

double[] numbers = new double[6];
for (int count = 0; count < 6; count++) {
while (true) {
switch(count) {
case 0: System.out.print("Enter value of a: "); break;
case 1: System.out.print("Enter value of b: "); break;
case 2: System.out.print("Enter value of c: "); break;
case 3: System.out.print("Enter value of d: "); break;
case 4: System.out.print("Enter value of e: "); break;
case 5: System.out.print("Enter value of f: "); break;
try {
InputStreamReader in= new InputStreamReader(System.in);
BufferedReader input = new BufferedReader(in);
String str;
while ((str = input.readLine()) != null) { System.out.println(str); }
} catch (IOException io) {io.printStackTrace(); }
numbers[count] = Double.parseDouble(str);

EDIT: I just realized I forgot to put breaks. The link will not contain them as I have to go back to fix that, so I will just fix in this post.

EDIT 2: Instead of logically checking the condition of the count with if's I changed it to switches to improve code efficiency.

So, apparently all I had to do was add the line, keyboard.nextLine();, to the catch block to fix it

1 Like

What this program should do btw?

Instead of int use Boolean for X, and name it more meaningful (inWhileLoop).
The If statements very wrong. Use switch, array or enum.
System.out.print("Enter value of " + myArray[count]);

1 Like