Results 1 to 3 of 3

Thread: Java Error: scan.nextInt();

  1. #1
    Join Date
    Mar 2009
    Location
    San Diego
    Beans
    8
    Distro
    Ubuntu 10.10 Maverick Meerkat

    Java Error: scan.nextInt();

    So I'm writing a program for class that is supposed to ask the user an infinite amount of times for an integer, until the user types -999, which will end the program. Then the program computes the average of the inputs and displays it on the screen.

    Here is my code:
    Code:
    import sdsu.io.*;
    public class HW6
    {
    public static void main(String args[])
    {
            int item;
            int total = 0;
            int numberofitems = 0;
            int average = 0;
    
            System.out.println("Input next data item: ");
            item = scan.nextInt();
    
            while (item != -999)
            {
                    total = (total + item);
                    numberofitems +=
                    average = total/numberofitems;
                    item = scan.nextInt();
            }
            System.out.println("The average of your data values is: " + average);
    
    }
    }
    but I get the following errors when I javac it:
    Code:
    HW6.java:12: cannot find symbol
    symbol  : variable scan
    location: class HW6
    	item = scan.nextInt();
    	       ^
    HW6.java:19: cannot find symbol
    symbol  : variable scan
    location: class HW6
    		item = scan.nextInt();
    		       ^
    2 errors
    And I've searched and searched the internet trying to look for a similar problem on threads but can't seem to find anyone who knows. Help?

  2. #2
    Join Date
    Nov 2009
    Beans
    1,081

    Re: Java Error: scan.nextInt();

    You didn't declare any variables named 'scan', let alone initialize them.

  3. #3
    Join Date
    Jul 2008
    Location
    England
    Beans
    866

    Re: Java Error: scan.nextInt();

    Code:
            while (item != -999)
            {
                    total = (total + item);
                    numberofitems +=
                    average = total/numberofitems;
                    item = scan.nextInt();
            }
    Once you have fixed your first problem, highlighted by Some Penguin, I would also have a look at this loop, particularly at the numberofitems line.

    Paul

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •