I am reading a randomly entered file from the user in command line (args[0]). Ri
ID: 3727210 • Letter: I
Question
I am reading a randomly entered file from the user in command line (args[0]). Right now I am reading all letters and spaces and adding to my array. But the only ones that are of interest to me are the uppercase letters A through G inclusive. I dont care about any of the other letters or spaces. I only want to add the capital letters A through G to my array. How do I do this (only add the uppercase letters A through G to my array and ignore all the other letters and spaces)??
import java.io.*;
import java.util.ArrayList;
public class HuffApp {
// static Node node;
// static Node root;
// static String codedString = "";
public static void main(String[] args) throws IOException {
System.out.println("Reading " + args.length + " file ");
String path = args[0];
BufferedReader in = new BufferedReader(new FileReader(path));
String text = "";
String line = in.readLine();
while (line != null) {
text += line;
line = in.readLine();
}
//System.out.println(text);
char[] msgLetters = text.toCharArray();
System.out.println(msgLetters);
ArrayList<Character> characters = new ArrayList<Character>();
for (int i = 0; i < msgLetters.length; i++) {
if (!(characters.contains(msgLetters[i]))) {
characters.add(msgLetters[i]);
}
}
// System.out.println(characters);
int[] countLetters = new int[characters.size()];
for (int x = 0; x < countLetters.length; x++) {
countLetters[x] = 0;
}
for (int i = 0; i < characters.size(); i++) {
char counter = characters.get(i);
for (int x = 0; x < msgLetters.length; x++) {
if (counter == msgLetters[x]) {
countLetters[i]++;
}
}
}
// bubblesort to sort in descending order
for (int i = 0; i < countLetters.length - 1; i++) {
for (int j = 0; j < countLetters.length - 1; j++) {
if (countLetters[j] < countLetters[j + 1]) {
int temp = countLetters[j];
countLetters[j] = countLetters[j + 1];
countLetters[j + 1] = temp;
char tempLetter = characters.get(j);
characters.set(j, characters.get(j + 1));
characters.set(j + 1, tempLetter);
}
}
}
//Printing the frequencies
for(int i = 0; i< countLetters.length; i++) {
System.out.println(characters.get(i)+ "="+countLetters[i]);
}
//FormTree
}
}
Explanation / Answer
Here, we are reading from a file. From your code, I can understand that you are reading line by line from the file and appending it to the ‘text’ variable.
So, in order to read only characters from ‘A’ to ‘G’. Instead of reading the file line by line, read the file character by character and check whether the character is in between ‘A’ and ‘G’. If so then append it to the text variable.
Two question arise here:
How do you read from file character by character?
We have a function in java “in.read()” which return the ASCII value of the character read. else it returns -1 if it is the end of the file.
How do you check if the character is in between ‘A’ and ‘G’?
you can check with ASCII value. ASCII value of ‘A’ is 65 and ‘G’ is 71. you just need to check whether the returned ASCII value is in between them. if so then append the character to the text.
Here is the modification for your code:
import java.io.*;
import java.util.ArrayList;
public class HuffApp {
// static Node node;
// static Node root;
// static String codedString = "";
public static void main(String[] args) throws IOException {
System.out.println("Reading " + args.length + " file ");
String path = args[0];
BufferedReader in = new BufferedReader(new FileReader(path));
String text = "";
//Here is the modified part
int n=0; //to get the return value
//use in.read() to get the next character from file
while((n=in.read())!=-1){
//compare n with ASCII values of ‘A’ and ‘G’
if(n>=65 && n<=71)
text += (char)n;
}
//modification ends here
char[] msgLetters = text.toCharArray();
System.out.println(msgLetters);
ArrayList<Character> characters = new ArrayList<Character>();
for (int i = 0; i < msgLetters.length; i++) {
if (!(characters.contains(msgLetters[i]))) {
characters.add(msgLetters[i]);
}
}
// System.out.println(characters);
int[] countLetters = new int[characters.size()];
for (int x = 0; x < countLetters.length; x++) {
countLetters[x] = 0;
}
for (int i = 0; i < characters.size(); i++) {
char counter = characters.get(i);
for (int x = 0; x < msgLetters.length; x++) {
if (counter == msgLetters[x]) {
countLetters[i]++;
}
}
}
// bubblesort to sort in descending order
for (int i = 0; i < countLetters.length - 1; i++) {
for (int j = 0; j < countLetters.length - 1; j++) {
if (countLetters[j] < countLetters[j + 1]) {
int temp = countLetters[j];
countLetters[j] = countLetters[j + 1];
countLetters[j + 1] = temp;
char tempLetter = characters.get(j);
characters.set(j, characters.get(j + 1));
characters.set(j + 1, tempLetter);
}
}
}
//Printing the frequencies
for(int i = 0; i< countLetters.length; i++) {
System.out.println(characters.get(i)+ "="+countLetters[i]);
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.