简体   繁体   中英

How do I add multiple lines of txt into an array and compare each letter to a user-input letter?

I'm trying to create a program that reads a txt document and counts the occurrences of a letter that the user inputs. It seems that my for-loop to index the letters is incorrect because it only reads the first or last line of the document. Any ideas?

The txt file contains:

Porsche GT2 RS

Lamborghini Huracan Evo

Mercedes Benz S 63 AMG

Merces Benz AMG GT 63s

Ferrari F8 Tributo

And the program is the following:

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

OUTPUT:

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

Your while -loop tries to copy each line into your ch -array, overwriting any previous lines in the process. After the loop only the last line and any remainders from previous lines not yet overwritten remain. Only then you perform your search, basically only searching the last line. In order to search all lines you will have to do your search while iterating over the lines, eg by putting the second loop inside the first loop:

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

Note that your array ch is a very very dangerous thing, if any line happens to have more than 100 characters your program will crash. In your current program it is not needed, you could check for your letter directly without storing the lines:

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

You do not need to create char[] ch = new char[100] . All you need to do is to compare each character of the string with the desired character and increase count whenever a match is found. Also, make sure to convert the input to lower case before getting the desired character (the first character) from it because you are converting each line of the file into the lower case before iterating its characters.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM