I need to get a series of integers from the user. The user will not be prompted to enter the numbers. The input will be of the following form:
6
34 12 7 4 22 15
3
3 6 2
The first line signifies the number of integers in the second line. I first tried to read the second line as a String and broke it to integers in the code using StringTokenizer. Sine this happens in my program a lot, it was time consuming and I needed to read them directly. I remember in C++ it used to be fairly simple. The following code segment used to do the trick.
for(i=0;i<6;i++)
cin>>a[i];
To achieve this, I used the java.util.Scanner and my code looks as below:
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
Scanner src = new Scanner(System.in);
for(x=0;x<2;x++){
arraySize = Integer.parseInt(br.readLine());
for(i=0;i<arraySize;i++)
array[i] = src.nextInt();
}
In this case, I am getting the following error:
Exception in thread "main" java.lang.NumberFormatException: For input string: "34 12 7 4 22 15"
I am open to suggestions and am not sticking to Scanner alone. If there is any other method to achieve this, I am game.
homeworktag...