我这里有一个程序,它输入一个段落并将其写入文件.之后,它应计算每个字母的出现次数(区分大小写).但是,它不计算字母出现次数.我想我把 for循环放在了错误的地方.
import java.io.*;
import java.util.*;
public class Exercise1 {
public static int countLetters (String line, char alphabet) {
int count = 0;
for (int i = 0; i <= line.length()-1; i++) {
if (line.charAt(i) == alphabet)
count++;
}
return count;
}
public static void main(String[] args) throws IOException {
BufferedReader buffer = new BufferedReader (new InputStreamReader(System.in));
PrintWriter outputStream = null;
Scanner input = new Scanner (System.in);
int total;
try {
outputStream = new PrintWriter (new FileOutputStream ("par.txt"));
System.out.println("How many lines are there in the paragraph you'll enter?");
int lines = input.nextInt();
System.out.println("Enter the paragraph: ");
String paragraph = buffer.readLine();
outputStream.println(paragraph);
int j;
for (j = 1; j<lines; j++) {
paragraph = buffer.readLine();
outputStream.println(paragraph);
}
outputStream.close();
System.out.println("The paragraph is written to par.txt");
for (int k=1; k<lines; k++) {
paragraph = buffer.readLine();
total = countLetters (paragraph, 'A');
if (total != 0)
System.out.println("A: "+total);
//I'll do bruteforce here up to lowercase z
}
}
catch(FileNotFoundException e) {
System.out.println("Error opening the file par.txt");
}
}
}
Run Code Online (Sandbox Code Playgroud)
请帮我修复代码.我是编程新手,我需要帮助.非常感谢你!
首先,你的初始读取用户输入有点浪费,因为你读了一次,然后进入for循环的其余部分 - 这不是一个问题,只是一个更好的代码.
// your code
String paragraph = buffer.readLine();
outputStream.println(paragraph);
int j;
for (j = 1; j<lines; j++) {
paragraph = buffer.readLine();
outputStream.println(paragraph);
}
Run Code Online (Sandbox Code Playgroud)
你可以把它们放在循环中:
// better code
String paragraph;
int j;
for (j = 0; j<lines; j++) {
paragraph = buffer.readLine();
outputStream.println(paragraph);
}
Run Code Online (Sandbox Code Playgroud)
然后你的第一个问题来自你读行的方式:
// your code - not working
outputStream.close();
for (int k=1; k<lines; k++) {
paragraph = buffer.readLine();
total = countLetters (paragraph, 'A');
Run Code Online (Sandbox Code Playgroud)
考虑上面发生的事情:
paragraph = buffer.readLine();- 此代码的作用是什么?它等待另一个用户输入(而不是读取已插入的内容)要解决上面的问题:你需要阅读已经编写的内容 - 而不是要求另一个输入.然后,您可以将它们放入列表并编写for循环,而不是逐个强制逐个强制每个字符.
所以现在,您想要从已经创建的现有文件中读取(即,读取用户输入的WAS):
BufferedReader fileReader = new BufferedReader(new FileReader(new File("par.txt")));
String allCharacters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
String aLineInFile;
// Read the file that was written earlier (whose content comes from user input)
// This while loop will go through line-by-line in the file
while((aLineInFile = fileReader.readLine()) != null)
{
// For every line in the file, count number of occurrences of characters
// This loop goes through every character (a-z and A-Z)
for(int i = 0; i < allCharacters.length(); i++)
{
// For each single character, check the number of occurrences in the current line
String charToLookAt = String.valueOf(allCharacters.charAt(i));
int numOfCharOccurancesInLine = countLetters (aLineInFile, charToLookAt);
System.out.println("For line: " + aLineInFile + ", Character: " + charToLookAt + " appears: " + numOfCharOccurancesInLine + " times " );
}
}
Run Code Online (Sandbox Code Playgroud)
上面给出了每行中每个字符的出现次数 - 现在你只需要组织它们来跟踪整个文件的总数.
在代码方面,可能有更好的方法来编写它以实现更清晰的实现,但上面的内容很容易理解(我只是很快就写了).