/home/caleb/ASDV-Java/Semester 2/Assignments/lab7_CalebFontenot/src/main/java/com/calebfontenot/lab7_calebfontenot/occurrencesOfEachCharacter.java |
nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt
nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java
package com.calebfontenot.lab7_calebfontenot;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class occurrencesOfEachCharacter {
public static void main(String[] args) {
System.out.println((int) 'a' + ", " + (int) 'z');
int[] letterCount = new int[26];
Scanner input = new Scanner(System.in);
System.out.print("Enter a path to a file to scan: ");
File filePath = new File(input.nextLine());
try (Scanner fileScanner = new Scanner(filePath);) {
char currentChar;
int arrayIndex = 0;
fileScanner.useDelimiter("");
while (fileScanner.hasNext()) {
currentChar = fileScanner.next().toLowerCase().charAt(0);
arrayIndex = ((int) currentChar - 97);
if (currentChar > 'a' & currentChar < 'z') {
letterCount[arrayIndex]++;
}
}
} catch (FileNotFoundException ex) {
System.out.println(ex);
}
for (int i = 0; i < letterCount.length; ++i) {
if (letterCount[i] > 0) {
System.out.println("Number of " + Character.toUpperCase((char) (i + 97)) + "'s: " + letterCount[i]);
}
}
}
}