473,383 Members | 1,855 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

Number Averager

176 100+
Hello guys. I was trying to write a number averaging program that would continue averaging numbers until the user enters text. Here is my source:
Expand|Select|Wrap|Line Numbers
  1. /* Number Avergaer
  2. Written by Kid Programmer
  3. Coded in Java */
  4.  
  5. import java.util.Scanner;    //import a scanner
  6.  
  7. class NumberAverager {        //create a class
  8.     public static void main(String[] args) {    //define the main function
  9.         int average = 0;
  10.         System.out.println("This program will average numbers.");
  11.         System.out.println("To stop averaging numbers type in text.");
  12.         Scanner scan = new Scanner(System.in);  
  13.         while ( scan.hasNextInt() ){
  14.             int  number = scan.nextint("Please enter a number: ");
  15.              int total_average = (average + number) / 3;
  16.             System.out.println("The average is: " + total_average);
  17.         }
  18.     }
  19. }
  20.  
I get the following error:

NumberAverager.java:14: cannot find symbol
symbol : method nextint(java.lang.String)
location: class java.util.Scanner
int number = scan.nextint("Please enter a number: ");
^
1 error

What should I do?
Apr 26 '08 #1
9 1689
r035198x
13,262 8TB
Java is case sensitive. nextint != nextInt
Apr 26 '08 #2
sukatoa
539 512MB
also nextInt() method with a parameter String have never been exists....

maybe int....

regards,
sukatoa
Apr 26 '08 #3
Kid Programmer
176 100+
I fixed the problem. But I have another. There apparently is a problem with my math. Please fix it. Here is my source:
Expand|Select|Wrap|Line Numbers
  1.  
  2. /* Number Avergaer
  3. Written by Kid Programmer
  4. Coded in Java */
  5.  
  6. import java.util.Scanner;    //import a scanner
  7.  
  8. class NumberAverager {        //create a class
  9.     public static void main(String[] args) {    //define the main function
  10.         int average = 0;
  11.         System.out.println("This program will average numbers.");    //explain the program
  12.         System.out.println("To stop averaging numbers type in text.");
  13.         Scanner scan = new Scanner( System.in );    //create a scanner
  14.         while ( scan.hasNextInt() ){    
  15.             int  number = scan.nextInt();        //prompt the user for a number
  16.             int average2 = (average + number);    //calculate part of the average
  17.             if ( scan.hasNextInt() == false ) {    
  18.                 int total_average = average2 / 3;    //calculate the average
  19.                 System.out.println("The average is: " + total_average);        //print out the average
  20.             }
  21.         }
  22.     }
  23. }
  24.  
I type in the numbers 1, 3, and 5 and it says the average is 1.
Apr 26 '08 #4
r035198x
13,262 8TB
1.) Don't use int if you need to do divisions. What if the result has a decimal part?
2.) Write down your pseudo-code algorithm for solving this problem first.
Apr 26 '08 #5
JosAH
11,448 Expert 8TB
The title of this thread sounded promising: a number averager; I would've expected
something like this:

Expand|Select|Wrap|Line Numbers
  1. NumberAverager na= new NumberAverager();
  2. na.add(<some_number>): // do this several times
  3. System.out.println("total: "+na.getTotal());
  4. System.out.println("average: "+na.getAverage());
  5. System.out.println("n: "+na.getN());
  6.  
... but alas, a same old rigid fortranesque implementation was shown again.
I knew it for years: those Basic programmers are all lost for the posterity.

kind regards,

Jos
Apr 26 '08 #6
Kid Programmer
176 100+
Well I tried fixing my problem and I got this error:

NumberAverager.java:21: cannot find symbol
symbol : variable number_sum
location: class NumberAverager
int total_average = number_sum / divide_by; //calculate the average
^
1 error


This is my new source:
Expand|Select|Wrap|Line Numbers
  1. /* Number Avergaer
  2. Written by Kid Programmer
  3. Coded in Java */
  4.  
  5. import java.util.Scanner;    //import a scanner
  6.  
  7. class NumberAverager {        //create a class
  8.     public static void main(String[] args) {    //define the main function
  9.         int average = 0;
  10.         int divide_by = 0;
  11.         System.out.println("This program will average numbers.");    //explain the program
  12.         System.out.println("To stop averaging numbers type in text.");
  13.         Scanner scan = new Scanner( System.in );    //create a scanner
  14.         while ( scan.hasNextInt() ){            //ask the user to enter numbers until they enter text
  15.             int number = scan.nextInt();        //prompt the user for a number
  16.             divide_by ++;                //increase the number to divide the sum by
  17.             while ( scan.hasNextInt() == true )  {       //while the user is still entering numbers 
  18.                 double number_sum = number + 0;
  19.             }
  20.             if ( scan.hasNextInt() == false ) {    //if the user entered text
  21.                     int total_average = number_sum / divide_by;     //calculate the average
  22.                 System.out.println("The average is: " + total_average);        //print out the average
  23.             }
  24.         }
  25.     }
  26. }
  27.  
How can I fix this?

Sincerely,
Kid Programmer
Apr 26 '08 #7
Laharl
849 Expert 512MB
Expand|Select|Wrap|Line Numbers
  1.  
  2.             while ( scan.hasNextInt() == true )  {
  3.                 double number_sum = number + 0;
  4.             }
  5.             if ( scan.hasNextInt() == false ) {
  6.                     int total_average = number_sum / divide_by;     
  7.  
How can I fix this?

number_sum does not exist outside of the pair of curly braces in which it is first declared. The same is true for any other primitive (int, double, boolean, char, float, byte). Also, in your current code, if the declaration for number_sum were moved appropriately, number_sum would still always equal number.
Apr 26 '08 #8
Kid Programmer
176 100+
But I don't want number sum to always equal number. I need it to be the sum of all the numbers entered.
Apr 26 '08 #9
sukatoa
539 512MB
But I don't want number sum to always equal number. I need it to be the sum of all the numbers entered.
eg:

Expand|Select|Wrap|Line Numbers
  1. var = 0;
  2.      while( allowable ){
  3.           var += input from user;
  4.      }
  5.      showAverage( var / how many times the user inputted );
regards,
sukatoa
Apr 27 '08 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Shay Hurley | last post by:
this is probably a stupid question so apologies in advance. I am trying to format a number to look like a phone number with "-"'s between the numbers etc e.g. 15554256987 should be formatted as...
8
by: EAS | last post by:
Hey, I'm new to python (and programming in general) so I'll prolly be around here a lot... Anyways, I've found out how to make a "guess my number game" where the player guesses a number between...
11
by: don | last post by:
Ok, this is a homework assignment, but can you help me out anyway...... I need a routine for figuring out if a number inputted by the user is a prime number or not...... all I'm asking for is Not...
27
by: Gaijinco | last post by:
Sooner or later everytime I found recreational programming challenges I stumble with how I test if a number is has decimal places differnt than 0? For example if I want to know if a number is a...
13
by: Ron | last post by:
Hi all I'm deciding whether to use the PK also as an account number, invoice number, transaction number, etc that the user will see for the respective files. I understand that sometimes a...
19
by: gk245 | last post by:
Trying to write a program that will figure out if a number is perfect or not. Here is my logic: 1) Read in the number 2) Split it up (number - 1) 3) Put all the split up numbers into an...
4
by: SweetLeftFoot | last post by:
Hello, i have designed some code that works out the first 250 prime numbers and prints them to the screen. However i need to implement 2 functions, one of which returns a 1 if the number is a prime...
6
by: Kid Programmer | last post by:
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: ...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.