Number Averager

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Kid Programmer
    New Member
    • Mar 2008
    • 176

    Number Averager

    Hello guys. I am rather new to Java and so I decided to test my knowledge and write a simple number averaging program. When I compile it though I get the following errors:

    NumberAverager. java:14: cannot find symbol
    symbol : variable n1
    location: class NumberAverager
    int area = (n1 + n2 + n3) / 3;
    ^
    NumberAverager. java:14: cannot find symbol
    symbol : variable n2
    location: class NumberAverager
    int area = (n1 + n2 + n3) / 3;
    ^
    NumberAverager. java:14: cannot find symbol
    symbol : variable n3
    location: class NumberAverager
    int area = (n1 + n2 + n3) / 3;
    ^
    NumberAverager. java:14: operator / cannot be applied to java.lang.Strin g,int
    int area = (n1 + n2 + n3) / 3;
    ^
    4 errors

    Here is my source code. I tried to add a lot of comments.
    Code:
    /* Number Averaging Program
    Written by Kid Programmer */
    import java.util.Scanner;
    
    class NumberAverager {                                          //define the class
    	public static void main(String[] args) {		//make the main function	
    		Scanner scan = new Scanner (System.in);		//create a scanner
    		System.out.println("This program will average 3 numbers.");	//explain the program
    		System.out.println("Please enter the three numbers: ");		//ask the user to enter the numbers
    		while ( scan.hasNextInt() ) {			
    			int n1 = scan.nextInt();			//prompt the user for the first number
    			int n2 = scan.nextInt();			//prompt the user for the second number
    			int n3 = scan.nextInt();			//prompt the user for the third number
    		}
    		int area = (n1 + n2 + n3) / 3;				//calculate the area
    		System.out.println("The area is: " +area);		//print out the area
    	}
    }
  • tomshorts07
    New Member
    • Apr 2008
    • 9

    #2
    you didnt declare the variable correctly

    all of those errors- (the ones that say cannot find symbol) are caused because you are supposed to use a 'double' variable type

    rather than declare it as
    Code:
    int area = (n1 + n2 + n3) / 3;        //calculate the area
    reformat it to be
    Code:
    double area = (n1 + n2 + n3) / 3;        //calculate the area
    and it should work fine!

    Comment

    • Kid Programmer
      New Member
      • Mar 2008
      • 176

      #3
      Originally posted by tomshorts07
      you didnt declare the variable correctly

      all of those errors- (the ones that say cannot find symbol) are caused because you are supposed to use a 'double' variable type

      rather than declare it as
      Code:
      int area = (n1 + n2 + n3) / 3;        //calculate the area
      reformat it to be
      Code:
      double area = (n1 + n2 + n3) / 3;        //calculate the area
      and it should work fine!
      I tried changing my code to:
      Code:
      /* Number Averaging Program
      Written by Kid Programmer */
      import java.util.Scanner;
      
      class NumberAverager {                                          //define the class
      	public static void main(String[] args) {		//make the main function	
      		Scanner scan = new Scanner (System.in);		//create a scanner
      		System.out.println("This program will average 3 numbers.");	//explain the program
      		System.out.println("Please enter the three numbers: ");		//ask the user to enter the numbers
      		while ( scan.hasNextInt() ) {			
      			double n1 = scan.nextInt();			//prompt the user for the first number
      			double n2 = scan.nextInt();			//prompt the user for the second number
      			double n3 = scan.nextInt();			//prompt the user for the third number
      		}
      		double area = (n1 + n2 + n3) / 3;        //calculate the area
      		System.out.println("The area is: " +area);		//print out the area
      	}
      }
      yet I still get the errors:

      NumberAverager. java:15: cannot find symbol
      symbol : variable n1
      location: class NumberAverager
      double area = (n1 + n2 + n3) / 3; //calculate the area
      ^
      NumberAverager. java:15: cannot find symbol
      symbol : variable n2
      location: class NumberAverager
      double area = (n1 + n2 + n3) / 3; //calculate the area
      ^
      NumberAverager. java:15: cannot find symbol
      symbol : variable n3
      location: class NumberAverager
      double area = (n1 + n2 + n3) / 3; //calculate the area
      ^
      NumberAverager. java:15: operator / cannot be applied to java.lang.Strin g,int
      double area = (n1 + n2 + n3) / 3; //calculate the area
      ^
      4 errors

      Comment

      • tomshorts07
        New Member
        • Apr 2008
        • 9

        #4
        alrighty, i figured it out kidprogrammer!

        you have to define the variables in the beginning of the program
        Code:
          Scanner scan = new Scanner (System.in);  //create a scanner
                 double area;
                 double n1=0;
                 double n2=0;
                 double n3=0;
                 System.out.println("This program will average 3 numbers."); //explain the program
                 System.out.println("Please enter the three numbers: ");  //ask the user to enter the numbers
        then just take out the 'double' declarations in the remainder of the program


        also, i noticed that your while () loop will not end-

        if you want to utilize the while loop, make a 'counting variable' to count up to end the loop

        Code:
         int i = 0;
                 while (i < 3) {         
                    n1 = scan.nextInt();   //prompt the user for the first number
                    n2 = scan.nextInt();   //prompt the user for the second number
                    n3 = scan.nextInt();   //prompt the user for the third number
        i++; //increases the value of the count by 1
                 }

        Comment

        • JosAH
          Recognized Expert MVP
          • Mar 2007
          • 11453

          #5
          You defined your variables n1, n2 and n3 (no matter their type) in the body of the
          while loop; this means that they don't exist outside of that while loop body; your
          compiler rightly complains about it so define them somewhere else where they
          are visible when you need them.

          kind regards,

          Jos

          Comment

          • Kid Programmer
            New Member
            • Mar 2008
            • 176

            #6
            Thank you. I got the program to work with this source:
            Code:
            /* Number Averaging Program
            Written by Kid Programmer */
            import java.util.Scanner;
            
            class NumberAverager {                                          //define the class
            	public static void main(String[] args) {		//make the main function	
                  	       Scanner scan = new Scanner (System.in);  //create a scanner
                           double area;
                           double n1=0;
                           double n2=0;
                           double n3=0;
                           System.out.println("This program will average 3 numbers."); //explain the program
                           System.out.println("Please enter the three numbers: ");  //ask the user to enter the numbers
                  	       int i = 0;
                           while (i < 3) {         
                              n1 = scan.nextInt();   //prompt the user for the first number
                              n2 = scan.nextInt();   //prompt the user for the second number
                              n3 = scan.nextInt();   //prompt the user for the third number
            		  area = (n1 + n2 + n3) / 3;
            		  System.out.println("The area is: " +area);
                		  i++;			 //increases the value of the count by 1
            
            		}
            	}		
            }
            But one thing. I get an error whenever you type characters while being prompted for the numbers. Is there a way to stop this?

            Comment

            • JosAH
              Recognized Expert MVP
              • Mar 2007
              • 11453

              #7
              Originally posted by Kid Programmer
              But one thing. I get an error whenever you type characters while being prompted for the numbers. Is there a way to stop this?
              Yes there is; read the API documentation for the Scanner class; it has methods
              that test whether or not you can read an int from the stream.

              kind regards,

              Jos

              Comment

              Working...