【问题标题】:Counting letter occurrence计数字母出现
【发布时间】:2013-02-07 21:25:39
【问题描述】:

程序需要统计并显示指定字符在文本文件中出现的次数。

目前总数为零。如果我应该使用不同的循环,我不是,我也尝试过使用“for”循环。

// Hold user input and sum
String fileName;    // Holds the name of the file
String letter;      // Letter to search for in the file
int total = 0;      // Holds the total number of characters in the file


// Get the name of the file and character from the user
fileName = JOptionPane.showInputDialog("Please enter the name of a file:");
letter = JOptionPane.showInputDialog("Please enter a letter contained in the string");


// Open the file for reading
File file = new File(fileName);         
Scanner inputFile = new Scanner(file);  // Declare new scanner object for file reading


// Set accumulator to zero
int count = 0;

if (inputFile.nextLine().equalsIgnoreCase(letter)) {                                

    count++;          // add letter occurrence

    total += count;   // add the letter occurrence to the total
}

【问题讨论】:

  • 每行只有一个字母吗?
  • 每一行是一个单词,.txt是一个段落

标签: java if-statement letter file-io find-occurrences


【解决方案1】:

此答案假设您的文本文件中的每一行仅包含一个字母,正如您的问题所暗示的那样。

您需要将 if 语句包装在一个循环中,因为目前您只检查文件的第一行:

     while(inputFile.hasNext()) {
         if (inputFile.nextLine().equalsIgnoreCase(letter))    {                                

             count++;          // add letter occurrence

             total += count;   // add the letter occurrence to the total
         }
     }

你也可以替换:

count++;
total+= count;

只有

total++;

【讨论】:

  • 更新了包装的“while”循环,仍然无法正常工作,该字母出现 0 次
  • 你能粘贴一个示例文本文件吗
【解决方案2】:
    BufferedReader reader = new BufferedReader(new FileReader("somefile.txt"));
    int ch;
    char charToSearch='a';
    int counter=0;
    while((ch=reader.read()) != -1) {
        if(charToSearch == (char)ch) {
            counter++;
        }
    };
    reader.close();

    System.out.println(counter);

这有什么帮助吗?

【讨论】:

    【解决方案3】:
    String line=""
    while(inputFile.hasNext()) {
      line = inputFile.nextLine();
      for(int i=0; i<line.length(); i++ ){
         if (line.charAt(i)== letter)                                    
             count++;          
    
     }
    }
    

    【讨论】:

      【解决方案4】:

      您的代码中存在错误。下面是正确的代码-

         String fileName;    // Holds the name of the file
          String letter;      // Letter to search for in the file
      
          // Get the name of the file and character from the user
          fileName = "C:\\bin\\GWT.txt";
          letter = "X";
      
      
          // Open the file for reading
          File file = new File(fileName);         
          Scanner inputFile = new Scanner(file);  // Declare new scanner object for file reading
      
      
          // Set accumulator to zero
          int count = 0;
          while(inputFile.hasNext()) {
            if (inputFile.nextLine().toLowerCase().contains(letter.toLowercase())) { 
                 count++;          // add letter occurrence
             }
          }
          System.out.println(count);
      

      【讨论】:

      猜你喜欢
      • 2013-09-08
      • 1970-01-01
      • 2022-01-07
      • 2021-10-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-20
      • 1970-01-01
      相关资源
      最近更新 更多