# i really need help

• 11-14-2007, 03:24 AM
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.
• 11-14-2007, 05:33 AM
JT4NK3D
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
• 11-14-2007, 06:27 AM
blackstone
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...
• 11-15-2007, 12:57 AM
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.