-1

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?

Andrew
  • 13
  • 3
  • Does the file come with the `3` at top or is that not a part of the question? – jacob13smith Sep 11 '19 at 00:33
  • 3
    In Java, the word "[field](https://docs.oracle.com/javase/specs/jls/se11/html/jls-4.html#jls-4.12.3)" is used for *class variables* and *instance variable*. It does not cover *local variables*. So when your professor banned the use of *data fields*, it is the addition of extra static and/or instance variables to the class. It is **not a ban on local variables**, like your `data` variable. So use that `data` variable, because that is the way the code should be. (Well, maybe use `input.nextInt()` instead of `Integer.parseInt(input.next())`) – Andreas Sep 11 '19 at 00:35
  • Down-voted question, because it is meaningless, given that it is based on a misunderstanding of your professors requirements. – Andreas Sep 11 '19 at 00:37
  • You should be using while loop here. – Goion Sep 11 '19 at 02:09

2 Answers2

1

A for-loop will check its condition every time before its body is executed. Thus after every execution of the body, the increment is done and then i<input.nextInt() is checked. During this check, the Scanner is asked for an integer, thus every check of the condition will consume an integer. And since your input has no integers available for this purpose, this will not work.

Islingre
  • 1,592
  • 3
  • 14
1

Andreas is right that "fields" refers to instance variables (or, less often, class variables), not local variables, but if the number of planets isn't needed outside the for loop you can declare it in the header of the loop and it will be scoped to the loop:

for (int count = input.nextInt(), i = 0; i < count; ++i) {
    insert(input.next(), input.nextInt(), tree);
}
David Conrad
  • 12,745
  • 1
  • 37
  • 46