1 - 3 of 3 Posts

#### [Teh Root]

·
##### Banned
Joined
·
3,583 Posts
Discussion Starter · ·
Hey guys, I can't get this to work for my life. This is the complete source code for my program. You need to enter 10 digits and it calcs the average and the mode, and sorts them.

It's in Java.

import java.util.*;
public class Lab10 {

public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int[] Numbers = new int[10];
int total = 0;
double average;
int count = 0;
int mode = 0;
int store = 0;
int x = 0;

System.out.println("Enter 10 integers:");
for(; x < Numbers.length; x++);
Numbers[x] = input.nextInt();

Arrays.sort(Numbers);

for(; x < Numbers.length; x++)
total += Numbers[x];

average = (total/Numbers.length);

for(int z = Numbers.length-1; z >= 0; z--){
if(z != 0){
if(Numbers[z] == Numbers[z-1]){
count++;
if(count > store){
store = count;
mode = Numbers[z];

}

else if(count == store)
mode = -1;

}
if(Numbers[z] != Numbers [z-1]){
count = 0;
}
}
}

for(; x < Numbers.length; x++)
System.out.print(Numbers[x] + ", ");
System.out.println();
System.out.println("The average is " + average);
if(mode != -1)
System.out.println("The mode is " + mode);
else
System.out.println("No mode");
}
}

However I get this error when I run, reffering to this one line in code.

Enter 10 integers:
1

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 10
at Lab10.main(Lab10.java:16)

For this line in the code.
Numbers[x] = input.nextInt();

Any help? I also for this line,

for(int x = Numbers.length-1; x >= 0; x--){
if(x != 0){

It won't let me use X.

Help?

#### Ectrian

·
##### Registered
Joined
·
77 Posts
I found the error:

Line 17:

Code:
Code:
``for(; x < Numbers.length; x++);``
Change this to:

Code:
Code:
``for(; x < Numbers.length; x++)``
You left a semi colon at the end of the for statement.

#### FaLLeNAn9eL

·
##### Yes
Joined
·
1,744 Posts
Yup. The semi-colon at the end of that line caused the for loop to run to 10 before going to the next line.

1 - 3 of 3 Posts