【问题标题】:How do I add multiple lines of txt into an array and compare each letter to a user-input letter?如何将多行 txt 添加到数组中并将每个字母与用户输入的字母进行比较?
【发布时间】:2020-10-30 20:05:35
【问题描述】:

我正在尝试创建一个程序来读取 txt 文档并计算用户输入的字母的出现次数。似乎我索引字母的 for 循环不正确,因为它只读取文档的第一行或最后一行。有什么想法吗?

txt文件包含:

Porsche GT2 RS

Lamborghini Huracan Evo

Mercedes Benz S 63 AMG

Merces Benz AMG GT 63s

Ferrari F8 Tributo

程序如下:

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Scanner;

public class GetOccurChar {
    static void GetOccurFile(File f) throws IOException {
        System.out.println("What letter do you want to check the occurence for in the file?");
        Scanner scan2 = new Scanner(System.in);
        char c = scan2.next().charAt(0);
        int count = 0;
        String str= "";
        char[] ch = new char[100];

        try {
            Scanner scan = new Scanner (f);
            
            while (scan.hasNextLine()) {
                str = scan.nextLine().toLowerCase();
                for (int i = 0; i < str.length(); i++)
                    ch[i] = str.charAt(i);
            }
            scan.close();
            for (int i = 0; i < ch.length; i++)
                if (ch[i] == c)
                    count++;
            System.out.println(c +" occures " + count + " times in the file.");
            System.out.println(ch);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }
}

输出:

What letter do you want to check the occurence for in the file?
t
t occures 2 times in the file.
ferrari f8 tributo 63so

【问题讨论】:

    标签: java arrays java.util.scanner find-occurrences


    【解决方案1】:

    您的while-loop 尝试将每一行复制到您的ch-array 中,覆盖该过程中的任何先前行。在循环之后,仅保留最后一行和前几行中尚未覆盖的任何剩余部分。只有这样你才能执行搜索,基本上只搜索最后一行。为了搜索所有行,您必须在遍历行时进行搜索,例如通过将第二个循环放在第一个循环内:

    while (scan.hasNextLine()) {
        str = scan.nextLine().toLowerCase();
        for (int i = 0; i < str.length(); i++)
            ch[i] = str.charAt(i);
        for (int i = 0; i < ch.length; i++)
            if (ch[i] == c)
                count++;
    }
    

    请注意,您的数组ch 是一个非常非常危险的东西,如果任何一行恰好有超过100 个字符,您的程序就会崩溃。在您当前的程序中不需要它,您可以直接检查您的信件而不存储行:

    while (scan.hasNextLine()) {
        str = scan.nextLine().toLowerCase();
        for (int i = 0; i < str.length(); i++)
            if (str.charAt(i) == c)
                count++;
    }
    

    【讨论】:

      【解决方案2】:

      您不需要创建char[] ch = new char[100]。您需要做的就是将字符串的每个字符与所需字符进行比较,并在找到匹配项时增加count。此外,请确保在从中获取所需字符(第一个字符)之前将输入转换为小写,因为您在迭代其字符之前将文件的每一行转换为小写。

      static void GetOccurFile(File f) throws IOException {
          System.out.println("What letter do you want to check the occurence for in the file?");
          Scanner scan2 = new Scanner(System.in);
          char c = scan2.next().toLowerCase().charAt(0); // Lower case
          int count = 0;
          String str = "";
          try {
              Scanner scan = new Scanner(f);
              while (scan.hasNextLine()) {
                  str = scan.nextLine().toLowerCase();
                  for (int i = 0; i < str.length(); i++) {
                      if (c == str.charAt(i)) { // Compare
                          count++;
                      }
                  }
              }
              scan.close();
              System.out.println(c + " occurs " + count + " times in the file.");
          } catch (FileNotFoundException e) {
              e.printStackTrace();
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-10-24
        • 1970-01-01
        • 1970-01-01
        • 2019-05-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多