-1
텍스트 파일의 평균 단어 길이를 찾는 데 문제가 있습니다. 어떤 이유로 출력이 0입니다. 이 프로그램은 또한 평균 텍스트 길이를 찾는 데 문제가있는 텍스트 파일의 총 단어 수를 찾습니다.평균 단어 길이 .txt
public class WordCount {
public static void main(String[] args) throws FileNotFoundException {
while (true) {
System.out.println("Enter File name: ");
Scanner input=new Scanner (System.in);
String fileName= input.nextLine();
FileReader wordReader;
File file = new File("text.txt");
try {
wordReader=new FileReader(fileName);
BufferedReader reader=new BufferedReader(wordReader);
String wordCounter;
int numberWords=0;
double avgWord=0;
double chara=0;
while((wordCounter=reader.readLine()) !=null) {
String []words=wordCounter.split(" ");
for(int i=0;i<words.length;i++)
{
numberWords++;
}
}
while((wordCounter=reader.readLine()) !=null) {
String []charWords=wordCounter.split("");
for (int j=0;j<charWords.length;j++) {
chara++;
}
avgWord=chara/numberWords;
}
System.out.println("Total words: "+ numberWords);
System.out.println("Average word length: "+ avgWord);
}catch (FileNotFoundException ex) {
System.out.println("File not found");
System.out.println("Example of a valid input: /Users/Marcus/Documents/text.txt");
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
들여 쓰기를 수정하십시오. 우리와 당신 모두에게 정말 혼란 스럽습니다. 이클립스에서는 Ctrl-Shift-F – HyperNeutrino