Search code examples
javamethodstext-files

How to return amount of a certain letters from a text file using a method


Assuming I have a text file filled with random words or letters, I need to be able to increment the amount of times a letter is seen. For this example, I am trying to find the letter 'a'. While the program does compile and return a number, the number can either be too high or low concerning the amount of the certain letter I'm looking for. Changing my code around, how can I fix this?

Oh! I should mention that the person I'm sending this code in for wants it to be structured in the form of having a method whereas Problem9 only has the parameters (File, file) and must be constructed into the form of a method

package module4labs;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.util.ArrayList;
import java.util.InputMismatchException;

public class Module4Labs {
    public static void main(String[] args) {
        new Module4Labs();
    }
    
    public Module4Labs() {
        double problemSolution = Problem9(new File("Problem9.txt"));
                System.out.println(problemSolution);        
    }

//Problem 9: Find 'a'
public int Problem9(File file) {
    int counter =0;
    try {
    Scanner input = new Scanner(file);
    ArrayList<String> list = new ArrayList<String>();
    while(input.hasNext()) {
        list.add(input.next());
    }
    
    for (int x=0; x<list.size()-1;x++) {
        if (list.subList(x,x+1).equals('a'));
        counter++;
    }
    input.close();
 } catch (FileNotFoundException e) {
    System.out.println(file + "File Not Found.");
    }
 return counter;

}

Currently, I'm getting a warning from Eclipse saying "Unlikely argument type for equals(): char seems to be unrelated to List"

Unsure on how to continue with this


Solution

  • the person I'm sending this code in for wants it to look similar to this No they don't. This code violates several coding conventions in Java; Problem9 is a terrible and undescriptive method name. You are counting the appearance of a char. Don't hardcode that char - pass it to the method and give the method a proper name, like countChar. Iterate the lines of your file and then iterate the contents of the line looking for the char. Next, don't calculate the result and print it in the constructor. I would make countChar static, but assuming you must instantiate your class I would do so in main and then invoke the method. Like,

    public static void main(String[] args) {
        Module4Labs m4l = new Module4Labs();
        double problemSolution = m4l.countChar(new File("Problem9.txt"), 'a');
        System.out.println(problemSolution);
    }
    
    public int countChar(File file, char ch) {
        int counter = 0;
        // This is a try-with-Resources, don't hard code input.close();
        try (Scanner input = new Scanner(file)) {
            while (input.hasNextLine()) {
                String line = input.nextLine();
                for (int i = 0; i < line.length(); i++) {
                    if (line.charAt(i) == ch) {
                        counter++;
                    }
                }
            }
        } catch (FileNotFoundException e) {
            System.out.println(file + " File Not Found.");
        }
        return counter;
    }