So I'm learning Java and maybe he didn't explain well enough how scanners work and their limits or maybe I'm looking over something silly... but I'm getting an error on answer = answer.nextInt();
I don't get this error for bomb
but it's used pretty much the same way...
Code:
Scanner yesNo = new Scanner(System.in);
Scanner input = new Scanner(System.in);
//
//answer auto set to no. goes to first if, then asks for confirmation,
// then check answer again and go to if, else if or else.
//
int answer = 0;
while (answer != 1)
if (answer == 0) {
System.out.println("In how many seconds should we detonate?");
int bomb = input.nextInt();
//this number will be used later in else if (answer == 1)
System.out.println("Is " + bomb + " seconds correct? 1 for yes, 0 for no");
answer = answer.nextInt();
//error above "Cannot invoke nextint() on the primitive type int"
//Need this to grab a number from user to assign a new value to answer
What do? Thanks.
First of all, you have one Scanner instance with paramether System.in, so it will "record" your keyboard (I assume that yesNo scanner is not used). Then, you have a int variable called "answer" which you assign zero value. Finally you have another variable called "bomb" where you will get your requested value.
As I see in your answers' comments, you're wrong in one thing: "input.nextInt()" is an int value. When you use input.nextInt(), you're sending it a message that says "Hey bro, give me the first int that this stupid human have pressed", but you aren't doing anything more. "input" is only a scanner (as it class name says) that records keystrokes.
So in fact, when you do "input.nextInt()" you'll get an int value, and when you do "bomb = input.nextInt()" or "answer = input.nextInt()" the only thing that you're doing is giving "bomb" or "answer" that int value.
int is a primitive value. It is not an Object and it has no methods.
probably you want to do
answer = input.nextInt();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With