如何将多行txt添加到数组中,并将每个字母与用户输入的字母进行比较?

问题描述

我正在尝试创建一个程序,该程序读取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

解决方法

您的while循环尝试将每一行复制到ch数组中,从而覆盖该过程中的所有先前行。循环之后,仅保留最后一行和之前未被覆盖的剩余行。只有这样,您才执行搜索,基本上只搜索最后一行。为了搜索所有行,您必须在遍历这些行的同时进行搜索,例如通过将第二个循环放入第一个循环:

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++;
}
,

您不需要创建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();
    }
}