Home > Enterprise >  Finding Highest and Lowest instance of letters from a textfile
Finding Highest and Lowest instance of letters from a textfile

Time:04-13

Writing this program that reads a textfile with bunch of names. I need to track the instance of each letter. I don't know if theres a simpler way of doing this than what I have without using anything like hashMap. My problem here is that by doing this the long way I'm storing the frequencies in a String list rather than a more logical integer list? I'm not sure how I would get the max min if the index in my list contains the string and the integer value assigned to it.

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


public class CountInstances {
public static void main(String [] args) throws IOException{

    ArrayList<String> list = new ArrayList<String>();//stores textfile into a list

    ArrayList<String> frequencies = new ArrayList<String>(); //stores the number of instances for each letter
    
    list = readFile("test.txt");
   // System.out.println(list);
    
    frequencies = letterInstances("test.txt");
   // System.out.println(frequencies);
    
    outputFile("test.txt", frequencies);
    
    
}//end of main

public static ArrayList<String> readFile(String file) throws IOException{
    ArrayList<String> list  = new ArrayList<String>();
    Scanner reader = new Scanner(new File("test.txt"));//scanner accesses file       
   
    while(reader.hasNext()){//loops till end of all lines
        String line = reader.nextLine();//scanner reads each line
        list.add(line);//adds each line to the arrayList
    }
    
    return(list);
    
}//end of readFile method


public static ArrayList<String> letterInstances(String file)throws IOException{
    ArrayList<String> instanceList = new ArrayList<String>();
  

    Scanner reader = new Scanner(new File("test.txt"));//scanner accesses file 
    int frequencyA = 0;
    int frequencyB = 0;
    int frequencyC = 0;
    int frequencyD = 0;
    int frequencyE = 0;
    int frequencyF = 0;
    int frequencyG = 0;
    int frequencyH = 0;
    int frequencyI = 0;
    int frequencyJ = 0;
    int frequencyK = 0;
    int frequencyL = 0;
    int frequencyM = 0;
    int frequencyN = 0;
    int frequencyO = 0;
    int frequencyP = 0;
    int frequencyQ = 0;
    int frequencyR = 0;
    int frequencyS = 0;
    int frequencyT = 0;
    int frequencyU = 0;
    int frequencyV = 0;
    int frequencyW = 0;
    int frequencyX = 0;
    int frequencyY = 0;
    int frequencyZ = 0;
    
    
    while(reader.hasNext()){
       String line = reader.nextLine();
       for(int i = 0; i< line.length(); i  ){
           if(line.charAt(i) == 'A' || line.charAt(i) == 'a'){//if letter is found in line, increase counter by 1
               frequencyA  ;   
           }else if(line.charAt(i) == 'B' || line.charAt(i) == 'b'){
               frequencyB  ;

           }else if(line.charAt(i) == 'C' || line.charAt(i) == 'c'){
               frequencyC  ;
           }else if(line.charAt(i) == 'D' || line.charAt(i) == 'd'){
               frequencyD  ;
           }else if(line.charAt(i) == 'E' || line.charAt(i) == 'e'){
               frequencyE  ;
           }else if(line.charAt(i) == 'F' || line.charAt(i) == 'f'){
               frequencyF  ;
           }else if(line.charAt(i) == 'G' || line.charAt(i) == 'g'){
               frequencyG  ;
           }else if(line.charAt(i) == 'H' || line.charAt(i) == 'h'){
               frequencyH  ;
           }else if(line.charAt(i) == 'I' || line.charAt(i) == 'i'){
               frequencyI  ;
           }else if(line.charAt(i) == 'J' || line.charAt(i) == 'j'){
               frequencyJ  ;
           }else if(line.charAt(i) == 'K' || line.charAt(i) == 'k'){
               frequencyK  ;
           }else if(line.charAt(i) == 'L' || line.charAt(i) == 'l'){
               frequencyL  ;
           }else if(line.charAt(i) == 'M' || line.charAt(i) == 'm'){
               frequencyM  ;
           }else if(line.charAt(i) == 'N' || line.charAt(i) == 'n'){
               frequencyN  ;
           }else if(line.charAt(i) == 'O' || line.charAt(i) == 'o'){
               frequencyO  ;
           }else if(line.charAt(i) == 'P' || line.charAt(i) == 'p'){
               frequencyP  ;
           }else if(line.charAt(i) == 'Q' || line.charAt(i) == 'q'){
               frequencyQ  ;
           }else if(line.charAt(i) == 'R' || line.charAt(i) == 'r'){
               frequencyR  ;
           }else if(line.charAt(i) == 'S' || line.charAt(i) == 's'){
               frequencyS  ;
           }else if(line.charAt(i) == 'T' || line.charAt(i) == 't'){
               frequencyT  ;
           }else if(line.charAt(i) == 'U' || line.charAt(i) == 'u'){
               frequencyU  ;
           }else if(line.charAt(i) == 'V' || line.charAt(i) == 'v'){
               frequencyV  ;
           }else if(line.charAt(i) == 'W' || line.charAt(i) == 'w'){
               frequencyW  ;
           }else if(line.charAt(i) == 'X' || line.charAt(i) == 'x'){
               frequencyX  ;
           }else if(line.charAt(i) == 'Y' || line.charAt(i) == 'y'){
               frequencyY  ;
           }else if(line.charAt(i) == 'Z' || line.charAt(i) == 'z'){
               frequencyZ  ;
           }
           
          
          
           
       }
      


}

(Below) Storing this section into a String Array List won't let me get the individual values that I need to access in a separate method. Not storing it into a String array list would just list the values but I would not know for what letter it represents.

     instanceList.add("A"  frequencyA);
     instanceList.add("B = "  frequencyB);
     instanceList.add("C = " frequencyC);
     instanceList.add("D = " frequencyD);
     instanceList.add("E = " frequencyE);
     instanceList.add("F = " frequencyF);
     instanceList.add("G = " frequencyG);
     instanceList.add("H = " frequencyH);
     instanceList.add("I = " frequencyI);
     instanceList.add("J = " frequencyJ);
     instanceList.add("K = " frequencyK);
     instanceList.add("L = " frequencyL);
     instanceList.add("M = " frequencyM);
     instanceList.add("N = " frequencyN);
     instanceList.add("O = " frequencyO);
     instanceList.add("P = " frequencyP);
     instanceList.add("Q = " frequencyQ);
     instanceList.add("R = " frequencyR);
     instanceList.add("S = " frequencyS);
     instanceList.add("T = " frequencyT);
     instanceList.add("U = " frequencyU);
     instanceList.add("V = " frequencyV);
     instanceList.add("W = " frequencyW);
     instanceList.add("X = " frequencyX);
     instanceList.add("Y = " frequencyY);
     instanceList.add("Z = " frequencyZ);

  
    
    return(instanceList);
  

}//end of letter instance method

Not sure what to go about here for getting the highest frequency and in descending order

public static void outputFile(String file, ArrayList<String> list)throws IOException{
    PrintWriter writer = new PrintWriter(new File("frequencies.txt"));//prints output to new file
   
    
    writer.println(letterInstances("test.txt"   "\n"));//prints the frequency of each letter
    System.out.println("An output file was created");
  
    int largest = list.indexOf(1);//this index output would get the whole line ex. A = 9 (but I just need to track the number)
    for(int i = 0; i < list.size(); i  ){
        
    

    
    writer.println("Top 5 Letters: ");
      writer.close();
    
   

    
}
    
}

}//end of class

CodePudding user response:

If you can not use a HashMap, you can try this.

Comments in the code.

Code

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 *
 * @author Sedrick(sedj601)
 */
public class Main
{

    public static void main(String[] args)
    {
        //Fake file data
        String dataToRead = 
        """
        Lorem Ipsum is simply dummy text of the 
        printing and typesetting industry Lorem 
        Ipsum has been the industrys standard dummy 
        text ever since thes when an unknown printer 
        took a galley of type and scrambled it to make a 
        type specimen book It has survived not only five 
        centuries but also the leap into electronic typesetting 
        remaining essentially unchanged It was popularised in the 
        a with the release of Letraset sheets containing 
        Lorem Ipsum passages and more recently with desktop 
        publishing software like Aldus PageMaker including 
        versions of Lorem Ipsum
        """;        
        List<String> data = Arrays.asList(dataToRead.toUpperCase().split("\n"));//Capatilize the data as you read it.
        
        List<String> keyCapitalLetters = Arrays.asList("ABCDEFGHIJKLMNOPQRSTUVWXYZ ".split(""));//Use this List like you would use the key part of a HashMap.
        //data.forEach(System.out::println);
        List<Integer> valuescounters = new ArrayList();//Use this list like you would use the value part of a HashMap
        //Set all the counters values to zero.
        for(int i = 0; i < 27; i  )
        {
            valuescounters.add(0);
        }
        
        //Process the fake data line by line
        for(String line : data)
        {
            System.out.println("Line: "   line);
            for(int i = 0; i < line.length(); i  )//Read each character of each line.
            {                
                int index = keyCapitalLetters.indexOf(String.valueOf(line.charAt(i)));//find the index in the key List of the characters that is being read.
                valuescounters.set(index, valuescounters.get(index)   1);//Increment the counter value in the value list.
                System.out.println("Char: "   line.charAt(i)   "\tIndex: "   index);
            }
        }
        
        //Print the output of the key and value list.
        for(int i = 0; i < keyCapitalLetters.size(); i  )
        {
            System.out.println(keyCapitalLetters.get(i)   ": "   valuescounters.get(i));
        }
    }    
}

OutPut

A: 30
B: 5
C: 10
D: 16
E: 59
F: 6
G: 11
H: 14
I: 38
J: 0
K: 7
L: 22
M: 19
N: 38
O: 25
P: 19
Q: 0
R: 24
S: 38
T: 43
U: 17
V: 5
W: 6
X: 2
Y: 13
Z: 0
 : 78

CodePudding user response:

Yes. You are right. You can use a hashmap. Try this code ->

public static Hashmap<String, Integer> letterInstances(String file)throws IOException{
    Hashmap<String, String> instanceMap = Hashmap<String, String>;
  

    Scanner reader = new Scanner(new File("test.txt"));//scanner accesses file
    
    
    while(reader.hasNext()){
       String line = reader.nextLine();
       for(int i = 0; i< line.length(); i  ){
           String character = line.get(i).toString().toLowerCase();
           int count = 0;
           if(instanceMap.containsKey(character)) count = instanceList.get(character);
           instanceMap.put(character, count)
       }
      
       return instanceMap;

}

Or if you also want to sort them to get the first 5 results, use this ->

public List<Entry<String, Integer>> getTop5(Hashmap map){

    Set<Entry<String, Integer>> set = map.entrySet();

    List<Entry<String, Integer>> list = new ArrayList<Entry<String, Integer>>(
            set);

    Collections.sort(list, new Comparator<Map.Entry<String, Integer>>() {

        @Override
        public int compare(Entry<String, Integer> o1,
                Entry<String, Integer> o2) {

            return o2.getValue().compareTo(o1.getValue());
        }

    });
    System.out.println(list.subList(0, 5)); // this is the list of largest 5 characters in count
    return list.subList(0, 5);
}

  • Related