0

I have a program that asks for file name but I am not sure what to do next, like how can I make a method to count the occurrences of each letter in that text file(and I need to use toLower and toUpper)

So please help.. Thank you

Here is my code:

import java.util.*;
import java.io.*;

public class FileOpener {

    public static void main(String[] args) throws IOException
    { 
        Scanner input = new Scanner(System.in);
        int[] array = new int[26];
        String[] alphabets = {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", 
                "t", "u", "v", "w", "x", "y", "z"};

        //String lines = input.nextLine().toUpperCase();
        //String path = input.nextLine();
        // Prompt user for file name
        System.out.print("Enter a file name: ");
        String fileName = input.nextLine();
        //char lines = input.next().toUpperCase().charAt(0);
        // Open file
        File file = new File(fileName);

        // Ensure that file exist
        if(!file.exists()){
            System.out.println("The file " + fileName + " does not exist.");
            System.exit(0);
        }

        // Create a Scanner for file
        Scanner inputFile = new Scanner(file);

        String line = null;
        int count = 0;
        while(inputFile.hasNextLine()){
            line = inputFile.nextLine();
            count += line.length();
        }

            //System.out.println(inputFile)
        System.out.println("The file size is " + count + " characters");    
        // Close file
        inputFile.close();

        for(char c : line.toCharArray()){
            array[97 - 'a']++;
        }
        for(char c = 'a'; c <= 'z'; c++){
            if(array[c - 'a'] != 0){
                System.out.println(c + " => " + array[97 - 'a']);
            }
        }

    }
}
Pshemo
  • 122,468
  • 25
  • 185
  • 269
  • Why is this loop `for(char c : line.toCharArray()){ array[97 - 'a']++; }` outside of loop which is reading each line? – Pshemo Apr 21 '16 at 20:23
  • Also can file contain only a-z characters? What about uppercase, spaces, numbers or other characters? – Pshemo Apr 21 '16 at 20:24
  • No it has to just look for letters and yes Uppercase too. – Patel Aashiv Apr 21 '16 at 20:26
  • Then you probably need to add some methods which will check if it is alphabetic character so you could skip non-alphabetic ones, and method which will return lowercase form of character (assuming that both `A` and `a` should increment `a` counter). Or use `Character` class which already provides such methods. – Pshemo Apr 21 '16 at 20:28
  • I am so lost like I do not have any idea how would I do that – Patel Aashiv Apr 21 '16 at 20:43

2 Answers2

0

A simple, but possibly not so efficient way of counting the number of occurrences of each letter is to create an integer array of the same size as the alphabets string array. And increment every time the index matches.

        String alphabets = "abcdefghijklmnopqrstuvwxyz";

        String fileName = "somefilename";
        int[] charCount = new int[alphabets.length()];
        String[] nameArray = fileName.split("");

        for(int i = 0; i < nameArray.length; i++) {
            charCount[alphabets.indexOf(nameArray[i])]++;
        }

        for(int i = 0; i < nameArray.length; i++) {
            System.out.println(charCount[i]);
        }

There probably exist more sophisticated solutions, but this should give you an integer array of all the counts of corresponding alphabets in the alphabets string array.

viviboox3
  • 309
  • 1
  • 6
  • 20
0

Considering only lowercase chars:

public class FileOpener {

  public static void main(String[] args) throws IOException { 
    Scanner input = new Scanner(System.in);
    int[] array = new int[26];
    String[] alphabets = {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", 
            "t", "u", "v", "w", "x", "y", "z"};

    //String lines = input.nextLine().toUpperCase();
    //String path = input.nextLine();
    // Prompt user for file name
    System.out.print("Enter a file name: ");
    String fileName = input.nextLine();
    //char lines = input.next().toUpperCase().charAt(0);
    // Open file
    File file = new File(fileName);

    // Ensure that file exist
    if(!file.exists()){
        System.out.println("The file " + fileName + " does not exist.");
        System.exit(0);
    }

    // Create a Scanner for file
    Scanner inputFile = new Scanner(file);

    String line = null;
    int tmp;
    while(inputFile.hasNextLine()){
         line = inputFile.nextLine();
         for(int i=0; i<line.length(); i++) {
             tmp = line.charAt(i) - 97;
             if(tmp < 26 && tmp >= 0)
                 array[tmp]++;
         }
    }

    // Close file
    inputFile.close();

    for (int i = 0; i < alphabets.length; i++)
        if(array[i] > 0)
            System.out.println(alphabets[i] + " => " + array[i]);

  }
}

It's same idea if you want handle uppercase chars.

EDIT: working example

issathink
  • 1,220
  • 1
  • 15
  • 25