Reputation: 13
Java novice here, trying to scan through the input in the form below:
3
3 3
101
000
101
1 2
11
5 5
10001
00000
00000
00000
10001
(The first digit is the number of test cases, followed by row and column, and 0s and 1s is the state of the world, and so on. Basically, like any typical BFS questions)
Below is a part of my Java code where I'm trying to get the input from the console. The comments are the values I see in the watch section while debugging:
public static void main(String[] args) {
Scanner in = new Scanner(new BufferedReader(new InputStreamReader(System.in)));
int t = in.nextInt(); // t: 3, in.nextInt(): 3 as expected
for (int i = 1; i <= t; i++) { // i: 1, t: 3, in.nextInt(): 3 (not sure which "3")
int row = in.nextInt();
// after this line in.nextInt(): 101 and row: 0 which are all WEIRD
int column = in.nextInt();
// now here it's in.nextInt(): 2 and column: 2
int world[][] = new int[row][column];
// in.nextInt(): 11 now and as we see it changed its value when I didn't even call it
TreeMap<Integer, ArrayList<int[]>> distancesRandAcc = new TreeMap<Integer, ArrayList<int[]>>();
distancesRandAcc.put(0, new ArrayList<int[]>());
for (int j = 0; j < row; j++) {
int temp = in.nextInt();
char[] s = Integer.toString(temp).toCharArray();
for (int k = 0; k < column; k++){
world[j][k] = Character.getNumericValue(s[k]);
}
}
int result = calculateDeliveryTime(world, row, column, distancesRandAcc);
System.out.println("Case #" + i + ": " + result);
}
}
This does something weird that goes beyond 'common sense'. I was debugging line by line after putting all the inputs in the IntelliJ console and it seemed like in.nextInt() can't stop getting a random number of tokens at random lines regardless of whether I even called it or not.
When I tried to put the inputs line by line as well while debugging line by line and it seemed like one call of .nextInt is asking for two int values. (I typed in a number and entered but it still waited for another input while staying in the same line where .nextInt() is just called just once)
At this point, I'm getting the feeling that my debugging process is wrong or at least it's some external problems rather than the code itself. I might be wrong as I still consider myself a novice...
Please, could somebody more experienced teach me how to get around this problem? I found a few questions having similar-looking problems with .nextLine() and .nextInt() but not exactly this. I apologise in advance if it's really a duplicate, though...
(I'll add more info if needed)
Upvotes: 1
Views: 428
Reputation: 159096
You are calling nextInt()
, so input 000
is parsed as the number 0
and the int
return value is therefore 0
. Integer.toString()
will therefore return the string "0"
. Why are you surprised about that?
Since you don't actually care about the input as a number, just use next
:
for (int j = 0; j < row; j++) {
String s = in.next();
for (int k = 0; k < column; k++) {
world[j][k] = Character.getNumericValue(s.charAt(k));
}
}
UPDATE
That is the only problem with the question code for reading the input. Whether there is a problem in the calculateDeliveryTime
is unknown.
See IDEONE for proof. The calculateDeliveryTime
method simply prints the 2D array, so it can be seen that the input has been handle ok.
Upvotes: 1