This discussion is archived
3 Replies Latest reply: Feb 19, 2013 9:31 AM by 991827

# frequency of numbers in txt file

Currently Being Moderated
hi,

iam a newbie in java,just started learning it.

i have numbers (only from 1 to 5) input typically at run time the input breaks only if i enter -1. , these will be saved to a txt file,numbers.txt. now my problem is to count the frequency of numbers enterted in the txt file and ouput the result to another txt file(output.txt), i know the logic , but not completely sure how to implement it .

import java.io.File;
import java.util.Formatter;
import java.util.Scanner;
public class StudentPoll{

private Scanner x;
int[] frequency = new int[5];
int count =0;
public void openFile(){
try{
x = new Scanner(new File("numbers.txt"));
}
catch(Exception e){
System.out.printf("No File found");
}
}
while(x.hasNext()){
int a= x.nextInt();
switch(a){
case 1:
++frequency[0];

case 2:
++frequency[1];

case 3:
++frequency[2];

case 4:
++frequency[3];

case 5:
++frequency [4];

}

for(int rating =1; rating<=frequency.length;rating++){
System.out.printf("%s%10s\n","rating","Frequency");
System.out.printf("%6d%10d\n",rating,frequency[rating]);

}
}
}

public void closeFile(){
x.close();
}

and in the main method

also, here i was just trying to get the output right on the console first, i don't how to write it to the txt file (output.txt)
and when i run this always the Frequency counter remains 1.

o/p when i run

run:
Enter integer result (1 - 5), -1 to quit: 1
Enter integer result (1 - 5), -1 to quit: 2
Enter integer result (1 - 5), -1 to quit: 1
Enter integer result (1 - 5), -1 to quit: 2
Enter integer result (1 - 5), -1 to quit: 3
Enter integer result (1 - 5), -1 to quit: 4
Enter integer result (1 - 5), -1 to quit: -1
rating Frequency
1 1
rating Frequency
2 1
rating Frequency
3 1
rating Frequency
4 1
rating Frequency
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5
at CreateResults.main(CreateResults.java:84)
Java Result: 1
BUILD SUCCESSFUL (total time: 10 seconds)

Edited by: 988824 on Feb 18, 2013 10:18 PM
• ###### 1. Re: frequency of numbers in txt file
Currently Being Moderated
It would have helped if you'd pointed out the line the exception was thrown from, and used code tags.

Anyway:
``````for(int rating =1; rating<=frequency.length;rating++) {
...
System.out.printf("%6d%10d\n",rating,frequency[rating]);
...``````
Java arrays are zero based.
In the above 'rating' runs from 1 to the length of the 'frequency' array.
You modify the Scanner input (in a slightly odd way) earlier in the method, so I would have thought you'd know this.
• ###### 2. Re: frequency of numbers in txt file
Currently Being Moderated
You also need to do something about terminating your loop when -1 is entered.
• ###### 3. Re: frequency of numbers in txt file
Currently Being Moderated
here i have modified the code, but still getting the error in the count

import java.io.File;
import java.util.Formatter;
import java.util.Scanner;
public class StudentPoll_dasariHaritha {

private Scanner x;
int[] frequency = new int[5];
int count =0;
public void openFile(){
try{
x = new Scanner(new File("numbers.txt"));
}
catch(Exception e){
System.out.printf("No File found");
}
}
while(x.hasNext()){
int a= x.nextInt();
if(a==1){
frequency[0]=0;
++frequency[0];
break;
}
else if (a== 2){
frequency[1]=0;
++frequency[1];
break;
}
else if( a==3){
frequency[2]=0;
++frequency[2];
break;
}
else if (a==4){
frequency[3]=0;
++frequency[3];
break;
}
else if (a== 5){
frequency[4]=0;
++frequency [4];
break;

}

}

System.out.printf("%s%10s\n","Rating","Frequency");
System.out.printf("%s%10s\n","1",++frequency[0]);
System.out.printf("%s%10s\n","2",++frequency[1]);
System.out.printf("%s%10s\n","3",++frequency[2]);
System.out.printf("%s%10s\n","4",++frequency[3]);
System.out.printf("%s%10s\n","5",++frequency[4]);

//}
}

public void closeFile(){
x.close();
}

in the main method iam calling these methods