0

我有一个程序应该计算指定文件中特定字符的所有实例,例如“A”。我得到它来计算字符,有点,除了它只查看单词开头的字符。所以“aa aaa a ba”只会算作4个“A”而不是7个。我已经尽我所能评论了,所以我的思路很清楚,但我对编程还很陌生,所以我提前道歉如果我不清楚。

import java.util.Scanner;
import java.io.*;

public class Charcounter
{
public static void main(String[] args) throws IOException
{
    //accumulator
    int sum = 0;

    Scanner kb = new Scanner(System.in);

    //get filename and character to be counted from user
    System.out.println("Enter the name of a file: ");
    String filename = kb.nextLine();
    System.out.println("Enter the name of the character to be counted: ");
    char countedChar = kb.next().charAt(0);

    //check if file exists
    File file = new File(filename);
    if (!file.exists())
    {
        System.out.println("File specified not found.");
        System.exit(0);
    }

    //open file for reading
    Scanner inputFile = new Scanner(file);

    //read file and count number of specified characters
    while (inputFile.hasNext())
    {
        //read a char from the file
        char count = inputFile.next().charAt(0);

        //count the char if it is the one specified
        if (count == countedChar)
        {
             ++sum;
        }

    }

    //close file
    inputFile.close();

    //display number of the specified char
    System.out.println("The number of the character '" + countedChar + "' is : " + sum);
}
}
4

6 回答 6

2

这是因为您只比较第一个字符。

 //read a char from the file
 // THIS : only the first character
 char count = inputFile.next().charAt(0);

 //count the char if it is the one specified
 if (count == countedChar)
 {
   ++sum;
 }

您应该遍历所有字符,然后如果它与 匹配,则增加总和countedChar,例如..

 String str = inputFile.next()
 for (int i = 0; i < str.length(); i++) {
   char count = str.charAt(i);   
   // check if it matches the countedChar and then increment.
 }
于 2013-10-18T12:27:33.903 回答
0

那是因为您只阅读第一个字符

    String word = inputFile.next();     //read a word
    int counter = 0; 

    for(counter=0;counter<word.length();counter++) // traverse through the word
    { 
        char count = word.charAt(i);       // ith char

        //count the char if it is the one specified
        if (count == countedChar)
        {
             ++sum;
        }
   }
于 2013-10-18T12:28:43.297 回答
0

尝试使用一个变量而不是零,它在类的开头初始化为零,然后为它计数的每个字符递增它,所以:

   //read a char from the file
    char count = inputFile.next().charAt(m);
    ++m;
    //count the char if it is the one specified
    if (count == countedChar)
    {
         ++sum;
    }

然后在 main 方法中定义:

   int m = 0
于 2013-10-18T12:32:42.687 回答
0

这是因为你在使用

char count = inputFile.next().charAt(0);

默认情况下next将读取直到找到空格或数据结尾,所以现在它读取并返回整个单词。

如果要使这种方法起作用next,则一次只需要返回一个字符,因此将imputFile扫描仪中的分隔符设置为空字符串,例如inputFile.useDelimiter("");.

于 2013-10-18T12:35:57.447 回答
0
while (inputFile.hasNext()) {
    String word = inputFile.next();
    for (int i = 0; i < word.length(); i++) {
        if (word.charAt(i) == countedChar) {
            ++sum;
        }
    }
}
于 2013-10-18T12:40:33.637 回答
0

作为替代方案,您可以使用专用于字符读取的 Reader:

BufferedReader reader = new BufferedReader(new FileReader(file));

int read;

while((read = reader.read())>0) if (read==countedChar) count++;

reader.close();
于 2013-10-18T12:48:50.903 回答