我正在制作一个项目,以检查文本文件并输出文件中每个字母的计数。
import java.util.*;
import java.io.*;
public class frequencyAnalysis {
private static String text;
public static String alphabet;
public static int Freq[];
public frequencyAnalysis(String text) {
this.text = text;
int [] Freq = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; //array of ints to keep track of how many of each letter there is.
alphabet = "abcdefghijklmnopqrstuvwxyz"; //point of reference for the program to know which number in the array should be increased
}
public static void freqAnalysis() throws IOException {
String token = "";
int index;
File subset = new File(text); //creates a new file from the text parameter
Scanner inFile = new Scanner(subset);
while(inFile.hasNext()) {
token = inFile.next();
index = alphabet.indexOf(token);
if (index == -1) { //makes sure that the character is a letter
break;
} else {
Freq[index]++;
}
}
inFile.close();
}
}
这是应该通过给定文本文件并计算其中每个字母有多少的类。
import java.util.*;
import java.io.*;
public class tester {
public static void main(String args[]) throws IOException {
Scanner in = new Scanner(System.in);
System.out.println("Please type the input file path: "); //allows the user to specify a file
String input = in.next();
frequencyAnalysis Freq = new frequencyAnalysis(input);
frequencyAnalysis.freqAnalysis(); //calls the method to run through the file
for(int i = 0; i <= 25; i++){ //prints the alphabet and the Freq array
System.out.println(frequencyAnalysis.alphabet.charAt(i) + ": " + frequencyAnalysis.Freq[i]); //this is where the error is
}
}
}
这是实现类,它允许用户指定文件,然后运行freqAnalysis方法来调整静态Freq数组,然后将其打印出来。但是,当我运行程序时,它在指定的行上给了我java.lang.NullPointerException错误。我已经发现问题出在“ frequencyAnalysis.Freq [i]”中,而不是在“ frequencyAnalysis.alphabet.charAt(i)”中。但是,我不知道问题出在哪里或如何解决。
答案 0 :(得分:0)
在构造函数中,您将创建一个初始化局部变量int [] Freq而不是类字段。 代替:
int [] freq = {0, 0, 0, 0, 0, 0, ...
您应该拥有:
freq = {0, 0, 0, 0, 0, 0, ...
答案 1 :(得分:0)
好的,所以您没有在frequencyAnalysis
类中正确初始化数组。
public static int Freq[];
您认为您正在使用它进行初始化
int [] Freq = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
但实际上应该是:
this.Freq = new int[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
答案 2 :(得分:0)
其他答案还不错,但是在编码风格上有缺陷。
首先,整数数组默认使用零初始化,因此您可以简化Freq
的初始化。另外,您在Freq
的构造函数中创建了局部变量 frequencyAnalysis
,而不是设置了静态类变量。
您应该将构造函数更改为此:
public frequencyAnalysis(String text) {
this.text = text;
frequencyAnalysis.Freq = new int[26]; //array of ints to keep track of how many of each letter there is.
alphabet = "abcdefghijklmnopqrstuvwxyz"; //point of reference for the program to know which number in the array should be increased
}
此外,类名应始终大写! (将其更改为FrequencyAnalysis
)
答案 3 :(得分:0)
为了通知您正在发生的事情,
public frequencyAnalysis(字符串文本){ this.text =文字; int []频率= {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0}; //整数数组,以跟踪每个字母有多少个。 字母=“ abcdefghijklmnopqrstuvwxyz”; //程序的参考点,以了解应增加数组中的哪个数字 }
执行此操作时,int [] Freq是一个新的局部变量,作用域仅限于构造函数,而另一方面,它是public static int Freq [];仍然为Null,如上文所述,指向相同的变量进行初始化