gpt4 book ai didi

java - 计算字符串的字母(大写和小写)

转载 作者:行者123 更新时间:2023-11-29 07:13:41 24 4
gpt4 key购买 nike

我这里有一个程序,可以输入一个段落并将其写入文件。之后,它应该计算每个字母的出现次数(区分大小写)。但是,它不计算字母出现的次数。我想我把 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");
}

}

}

请帮我修改代码。我是编程新手,需要帮助。非常感谢!

最佳答案

首先,您最初读取用户输入有点浪费,因为您读取一次然后进入 for 循环以完成其余部分 - 这不是问题,只是更好的代码。

// your code
String paragraph = buffer.readLine();
outputStream.println(paragraph);
int j;
for (j = 1; j<lines; j++) {
paragraph = buffer.readLine();
outputStream.println(paragraph);
}

你可以把它们放在循环中:

// better code
String paragraph;
int j;
for (j = 0; j<lines; j++) {
paragraph = buffer.readLine();
outputStream.println(paragraph);
}

那么你的第一个问题来自于你阅读这些行的方式:

// your code - not working
outputStream.close();
for (int k=1; k<lines; k++) {
paragraph = buffer.readLine();
total = countLetters (paragraph, 'A');

考虑上面发生的事情:

  • 输入已经完成,输出已经写入并且流已关闭 - 到这里一切都很好
  • 然后,当您尝试计算字符数时,您会执行:paragraph = buffer.readLine(); - 这段代码的作用是什么?它等待另一个用户输入(而不是读取已插入的内容)

要解决上述问题:您需要阅读已经 编写的内容 - 而不是要求其他输入。然后,您可以将它们放入一个列表并编写一个 for 循环,而不是一个一个地强制每个字符。

现在,您想从您已经创建的现有文件中读取(即读取用户输入的内容):

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 " );
}
}

上面为您提供了每行中每个字符的出现次数 - 现在您只需要组织它们以跟踪整个文件的总数。

在代码方面,可能有更好的方法来编写它以获得更清晰的实现,但上面的内容很容易理解(而且我写得很快)。

关于java - 计算字符串的字母(大写和小写),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11543860/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com