-3

I am getting an error where I can't compile my code due to:

error: cannot find symbol

while(numbers.hasNextInt()) {

symbol: method

hasNextInt() location: variable numbers of type String

I believe it has something to do with the String but I'm not quite sure.

//Takes a string of numbers, adds them together, and gives average

import java.util.*;
import java.io.*;

public class Section6HW {
   public static void main(String[] args) {
      Scanner console = new Scanner(System.in);

      //intro
      System.out.println("Enter a string of numbers with spaces between numbers");
      System.out.println("to signify the end of a number.");
      System.out.println();

      //input
      System.out.print("Enter numbers: ");
      String numbers = console.nextLine();

      //processing
      int sum = 0;//priming loop
      int count = 0;
      while(numbers.hasNextInt()) {
         count++;
         sum += numbers.nextInt();
         System.out.println("sum of " + count + " = " + sum);
      }
      System.out.println("average = " + (sum / count));
   }
}

I know there are a couple of unnecessary pieces of code in this but disregard that, it's for further development.

Alex
  • 140
  • 6
  • 19

1 Answers1

3

Change

numbers.hasNextInt()

to

console.hasNextInt())

You need to call the hasNextInt() method on the Scanner object, not a on a String.

Mohammed Aouf Zouag
  • 17,042
  • 4
  • 41
  • 67