Java 用于查找和计算文本中重复单词的程序。字符串写错了?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19311084/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Program to find and count repeated words in a text. The strings are writen wrong?
提问by
I did wrote a code that gets a text file as input, then the program makes to equal String arrays with the text so the words can be compared and find + count the unique and repeated words. The program becomes compiled successfully but if I try to execute it so I get the problem: Exception in thread"main"java.lang.NullPointerException...java:52. The problem must be in the way I declare the strings. How should I write them instead? Thanks!
我确实写了一个代码,将文本文件作为输入,然后程序使字符串与文本相等,这样就可以比较单词并找到 + 计算唯一和重复的单词。程序编译成功,但如果我尝试执行它,则会出现问题:线程“main”java.lang.NullPointerException...java:52 中的异常。问题一定出在我声明字符串的方式上。我应该如何写它们?谢谢!
import java.util.*;
class TextAnalyze{
public static void main(String[] args){
In read = new In ("text1.txt"); //input *.txt file
int tWords = 0; // counter, total words in the text file
int unW = 0; //counter UNIQUE words in the text file
String[] word = new String[31000]; //array with all the words
String[] word2 = new String[31000]; // array with all the words, used to compare
//String uniqueWords[] = new String[31000]; //array with the unique words
//int numberuniqueWords[] = new int [31000];
while(read.endOfFile() == false) {
word[tWords] = read.inWord();
word2[tWords] = word[tWords];
tWords++;
}
int totalWords = word.length;
int totalWords2 = word2.length;
List<String> uniqueWords = new ArrayList<>();
for (int i = 0; i < totalWords; i++) { // loop of the first array list
boolean unique = true;
for (int j = 0; j < totalWords2; j++) { // second loop where the
// words are being compared
if (word[i].equals(word2[j])) {
//we find two equals strings, it not unique
unique = false;
break;
}
}
//if it remains unique there wasn't equals
if (unique) {
uniqueWords.add(word[i]);
}
}
for (String s : uniqueWords) {
System.out.println(s);
}
}
}
}
回答by Aurand
int totalWords = word.length;
This is probably your problem. The length of an array is it's capacity, not the actual number of objects placed in it. So that will always return 31000. If that is not the number of words in your text file, then your loop will be pulling null values from your array.
这可能是你的问题。数组的长度是它的容量,而不是放置在其中的实际对象数量。所以这将始终返回 31000。如果这不是您的文本文件中的单词数,那么您的循环将从您的数组中提取空值。
回答by Gaurav Varma
I think @Aurand has identified the problem. You may now put a null check over your comparison :
我认为@Aurand 已经确定了问题所在。您现在可以对比较进行空检查:
if(word[i] != null && word2[j] != null){
if (word[i].equals(word2[j])) {
//we find two equals strings, it not unique
unique = false;
break;
}
}
回答by Rajesh Dev
FileReader fr = new FileReader("/home/rajesh/Desktop/movie");
BufferedReader br = new BufferedReader(fr);
String s;
Set<String> sdata = new HashSet<String>();
List<String> adata = new ArrayList<String>();
while ((s = br.readLine()) != null) {
for (String val : s.split(" ")) {
sdata.add(val);
adata.add(val);
}
}
for (String val : sdata) {
int freq = Collections.frequency(adata, val);
System.out.println("Frequency of " + val + " " + freq);
}
If you are going to run this code in command mode you have to specify try catch block or throws.
如果要在命令模式下运行此代码,则必须指定 try catch 块或 throws。
回答by Tobi
import java.util.Vector;
import java.util.Scanner;
public class Get{
public static void main(String[]args){
Scanner in = new Scanner(System.in);
System.out.print("enter a text? ");
String txt = in.nextLine();
Vector<Character> ch = new Vector<Character>();
char len[] = txt.toCharArray();
int count;
for(int i=0; i<len.length; i++){
count=0;
for(int j=0; j<len.length; j++){
if(len[i]==len[j]){
count++;
}
}
if(count>0){
if(!ch.contains(len[i])){
System.out.println(len[i] + " - " + count);
ch.add(len[i]);
}
}
}
}
}