I am trying to read in input of the form of n lines with each line having a string and an int, separated by a single space. My professor banned the use of data fields aside from the ones provided, so I ended up trying to use the input as I went. I keep getting an inputMismatchException that points to the header of my for loop and I don't know what it is reading that causes an inputMismatchException.
I wrote the code of what I tried to do with the use of an int variable to represent the maximum number of iterations my for loop should iterate, and it seemed to work without any errors.
for(int i = 0; i<input.nextInt();i++) {
insert(input.next(), input.nextInt(), tree);
}
below is what I did with a variable (what I would do if allowed to create data fields)
int data = Integer.parseInt(input.next());
for (int i = 0; i<data; i ++) {
insert(input.next(),input.nextInt(),tree);
}
while trying a sample input of
3
earth 10
venus 30
jupiter 5
The error pops up after I enter venus 30. Does the value I get from the input.next() in the for loop header change as I iterate (I fail to see why it would)? Or does this simply seem to be a logic error?