Q:

Cannot find symbol hasNextInt for a java.util.Scanner?

I am learning Java by myself and I have come across a problem that I would like to tackle. Here is the problem:

Write a class named Calculator with a method int sum(String s). String s contains a set of integers separated by white space (blanks, tabs, or newlines). Return the sum of the integers.
You can use a Scanner object to solve this problem. Create a new Scanner(s) and store it in a variable, say in. Then, use in.hasNextInt() to control a while loop. Each iteration of the while loop uses in.nextInt() to get the next integer from the String s. Accumulate this integer into a variable and return that variable when the loop exits.
You may use a main method to test your method by creating an instance of the Calculator class and calling sum(…) with several combinations of values using that instance.

My code so far is
import java.util.Scanner;

public class calculator{
    public int sum(String s){
        new Scanner(s);
        String in = s; 
        while (s.hasNextInt()) {
            s = in.nextInt();
        }
        return(0);
    }
}

Not finished yet of course but I am getting an error saying that it

cannot find symbol : method hasNextInt()

Any ideas why? I am not quite sure what to do from this point onwards, so could anyone point me in the right direction? Has s been stored into in correctly?

A:

Your variable declaration:
new Scanner(s);
String in = s; 

Is quite wrong. You have to store the scanner in a variable, that means:
Scanner myScanner = new Scanner(s);

Then
while (myScanner.hasNextInt())

A:

Just creating a new Scanner(s) won't do much if you don't store it somewhere. The assignment seems to be asking you to store it (as opposed to s) in a variable called in:
Scanner in = new Scanner(s);

After that, you shouldn't ever have to deal with s directly again: the Scanner can do the work for you.
In your code, s is a String, so it doesn't have methods like hasNextInt() and nextInt(). You should be calling those on in which should be a Scanner (this is the direct cause of the error).

A:

You are calling the method on a String. 
You need to call the hasNextInt() method on Scanner:
Scanner sc = new Scanner(s);
while (sc.hasNextInt()) {
   int i = sc.nextInt();
}

