Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 11-14-2007, 04:24 AM
gibsonrocker800's Avatar
Senior Member
 
Join Date: Nov 2007
Location: New York
Posts: 143
gibsonrocker800 is on a distinguished road
Send a message via AIM to gibsonrocker800
i really need help
hey, this is my first post on this forum. I'll get right to the point. I'm creating a program that does a bunch of math operations such as Law of Cosines, Quadratic Formula, etc. I'm working on an tool that computes the Average of a specified amount of numbers given by the user. What i have so far, is an integer array that contains the number of numbers that the user can choose. I've also constructed a new integer array for the actual numbers. Now I'm trying to use a for loop in order for it to display "What is number 1?", "What is number 2?", etc. It prints that with no problem, but when i try to get it to have the values inputted and averaged, what it's doing is just dividing the last inputted number. This is all very unclear so take a look at the code:

Scanner scan = new Scanner(System.in);
System.out.print("How many numbers? ");
int x = scan.nextInt(); //numofnums
int[] numOfNums = {2, 3, 4, 5, 6, 7, 8, 9};
int z = numOfNums[x-2]; //index of array
int average = 0;

for(int y = 1; y < (z+1); y++)
{
System.out.print("What is number " + y + "? ");
Scanner scan2 = new Scanner(System.in);
int[] nums = new int[8];
average = scan2.nextInt() / x;
}

System.out.println(average);



-------------------
Can someone please help me figure out how to make it so that the numbers the user inputs are taken into account and averaged.

Last edited by gibsonrocker800 : 11-14-2007 at 04:30 AM.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 11-14-2007, 06:33 AM
JT4NK3D's Avatar
Member
 
Join Date: Nov 2007
Posts: 50
JT4NK3D is on a distinguished road
if its a problem with the scanner, theres a good text class i got from a tutorial called TextIO that prints, gets input and other text related things with no problem.. just click the link TextIO.java at Javanotes 5.0, Section 2.4 -- Text Input and Output
if its a problem with sumthing else i dont know.. btw just compile TextIO, get it in same folder and use either the TextIO.getlnInt(); method or TextIO.getlnDouble(); method or TextIO.getlnBoolean(); method theres on for all primitive types and string i think... use what you want in it
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 11-14-2007, 07:27 AM
Member
 
Join Date: Nov 2007
Posts: 6
blackstone is on a distinguished road
The reason it is only dividing the last number entered is because of the last line in your for loop: average = scan2.nextInt() / x;

All that is doing is getting another number from the user and dividing it by the total number of integers that there should be. What you need to do is be adding up all the numbers entered by the user and then, outside of the for loop, divide that total by the total number of integers to find the average.

And just to satisfy my own curiosity, what are you using the numOfNums array for? I don't quite understand that...
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 11-15-2007, 01:57 AM
gibsonrocker800's Avatar
Senior Member
 
Join Date: Nov 2007
Location: New York
Posts: 143
gibsonrocker800 is on a distinguished road
Send a message via AIM to gibsonrocker800
i figured it out. and the reason why i'm setting up an array for the numOfNums is because i'm trying to learn how to put arrays to use, i could have easily set this program up with if statements or something, but i want to advance in my logic skills.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +3. The time now is 10:27 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org