Reputation: 5
I have been working on this code for a project and I keep getting the error "java.util.InputMismatchException". I have searched and found similar questions but I don't get how the answers apply to my code. I know that I'm entering the input correctly so that's out. Also, I have been trying to reformat my code, but it just seems to make it worse. I'm sorry if this is plainly obvious and I don't recognize it, I just started to code. Thanks for your patience.
Here's the full error message:
Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at songBottlesOfBeer.BottlesOfBeer.main(BottlesOfBeer.java:47)
Here's my full piece of code:
package songBottlesOfBeer;
import java.util.Scanner;
public class BottlesOfBeer {
private static Scanner bottles;
public static void number(int n) {
if (n>1) {
System.out.print(n+" bottles of beer on the wall, "+
n+" bottles of beer, ya' take one down, "+
"ya' pass it around, ");
n=n-1 ;
System.out.println(n+" bottles of beer on the wall.");
number(n);
}else{
if(n==1) {
System.out.print(n+" bottle of beer on the wall, "+
n+" bottle of beer, ya' take one down, "+
"ya' pass it around, ");
n=n-1 ;
System.out.println(n +" bottles of beer on the wall.");
number(n);
}else{
System.out.println("No more bottles of beer on the wall, " +
"no bottles of beer, ya' can't take one down, "
+ "ya' can't pass it around, 'cause there are"
+ " no more bottles of beer on the wall!");
}
}
}
public static void main(String[] args) {
bottles = new Scanner(System.in);
bottles.useDelimiter("\n");
System.out.println("Enter the starting number of "
+ "bottles in the song "
+ "'99 Bottles of Beer on the Wall':");
number(bottles.nextInt());
}
}
The error is located in number(bottles.nextInt());
Upvotes: 0
Views: 105
Reputation: 13427
Remove the line
bottles.useDelimiter("\n");
First of all avoid using \n
as a line separator since it is OS dependent. Use System.lineSeparator()
or a \R
if in a regex. Secondly, the delimiter is used to tokenize (break) a single input into several parts, which is not what you need. Pressing Enter automatically submits a single input.
Example
Scanner bottles = new Scanner(System.in);
System.out.println(bottles.nextInt());
System.out.println(bottles.nextInt());
System.out.println(bottles.nextInt());
The first call will block the execution waiting for input. The input is tokenized by the default delimiter which is \p{javaWhitespace}+
(basically white-spaces). Let's look at the following cases:
Input: 1 2 3
Enter
Output:
1
2
3
This happens because the single 5 character input was tokenized into 3 segments, which where then called sequentially by the nextInt
methods.
Input 1 2
Enter
Output:
1
2
//cursor mark
This happens because you "saturated" the first 2 calls to nextInt
, but the 3rd did not find another integer, and so it prompts the user for input (and blocks execution).
Input: 1 2 3 4
Enter
Output:
1
2
3
As in the first case, only now the scanner stores the input 4
and the next call will utilize it.
Remember to close the scanner when you finish:
bottles.close();
Upvotes: 1
Reputation: 21004
I tryed your code and it works perfectly..
This exception simply means that you have entered something (I have no clue what) but it was not an Integer
which is what is supposed to be read.
Here is the output I get when writing 5 :
Enter the starting number of bottles in the song '99 Bottles of Beer on the Wall': 5 5 bottles of beer on the wall, 5 bottles of beer, ya' take one down, ya' pass it around, 4 bottles of beer on the wall. 4 bottles of beer on the wall, 4 bottles of beer, ya' take one down, ya' pass it around, 3 bottles of beer on the wall. 3 bottles of beer on the wall, 3 bottles of beer, ya' take one down, ya' pass it around, 2 bottles of beer on the wall. 2 bottles of beer on the wall, 2 bottles of beer, ya' take one down, ya' pass it around, 1 bottles of beer on the wall. 1 bottle of beer on the wall, 1 bottle of beer, ya' take one down, ya' pass it around, 0 bottles of beer on the wall. No more bottles of beer on the wall, no bottles of beer, ya' can't take one down, ya' can't pass it around, 'cause there are no more bottles of beer on the wall!
Upvotes: 1