Can't call nextint () on primitive int type

So I'm learning Java and maybe it didn't explain well enough how scanners work and their limitations, or maybe I was looking for something silly ... but I get the error answer = answer.nextInt();

t get this error for bomb

but it is being used almost the same ...

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 to do? Thank.

+3


source to share


4 answers


int is a primitive value. It is not an object and it has no methods.

maybe you want to do



answer = input.nextInt();

      

+2


source


First of all, you have one instance of the scanner with the System.in parameter, so it will "write" your keyboard (I am assuming the yesNo scanner is not being used). Then you have an int variable called "response" which you assign to a null value. Finally, you have another variable called "bomb" where you will get the requested value.

As I see in the comments of your answers, you are wrong about one thing: "input.nextInt ()" is an int value. When you use input.nextInt (), you send it a message that says, "Hey brother, give me the first inn this stupid person pressed," but you don't do anything else. "enter" is just a scanner (as its class name suggests) that records keystrokes.



So when you do "input.nextInt ()" you get an int value, and when you do "bomb = input.nextInt ()" or "answer = input.nextInt ()" then only the thing you do is it is the "bomb" or "answer" to this value.

+4


source


nextInt () is a function that is part of the Scanner object type. To call .nextInt () you must have a Scanner object.

So, the line "int bomb = input.nextInt ();" works great since the "input" is an object of the Scanner class. This function runs and returns another object, an integer, from the input, which is stored in int bomb.

The line "answer = answer.nextInt ();" cannot compile because "response" is an integer object. integer doesn't have a function called nextInt ().

Corresponding line: "answer = input.nextInt ();" using your Scanner object to return another integer to store in the "response".

+1


source


int answer = 0;

answer = answer.nextInt();

      

You are calling nextInt()

to int. You need to call it on the scanner:

answer = input.nextInt();

      

0


source







All Articles