I have an input:
aN b c
a1 a2 a3 ... aN
for example:
4 3 2
2 1 2 1 (I have here 'a' numbers, a = 4)
5 6 3
3 9 5 7 3 (I have here 'a' numbers, a = 5)
0 0 0
I want to stop reading an input when a or b or c will equal 0. I tried this:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.LinkedList;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
public class Test {
public static void main(String[] args) throws IOException {
InputStreamReader converter = new InputStreamReader(System.in);
BufferedReader in = new BufferedReader(converter);
String line = "";
int a = -1, b = -1, c = -1;
LinkedList<Integer> list = new LinkedList<>();
while (a != 0 && b != 0 && c != 0)
{
line = in.readLine();
String tmp[] = line.split(" ");
a = Integer.parseInt(tmp[0]);
b = Integer.parseInt(tmp[1]);
c = Integer.parseInt(tmp[2]);
System.out.println("a = " + a + ", b = " + b + ", c = " + c);
line = in.readLine();
list.clear();
tmp = line.split(" ");
for (int i = 0; i < tmp.length; i++) {
list.add(new Integer(Integer.valueOf(tmp[i])));
}
System.out.println("List = 4 3 2" + list);
}
}
}
but with this simple input:
4 3 2
2 1 2 1
5 6 3
3 9 5 7 3
0 0 0
even if I type 3 zeros, my program still waits for an input. How to improve it?
EDIT:
Guy you misunderstood. I NEED to have a second readline cause I need to read a second (fourth, sixth) line of input …
1 Answer