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']);
}
}
}
}