i can get the class working, but how should i order the array by the highest
to lowest word count. is it possible to order it in alphabetical order if all it prints is multiple of Xs....
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.lang.reflect.Array;
import java.util.Scanner;
public class TranscriptAnalysis {
private Word[] words = new Word[5000];
private Scanner scan;
private int largestIndexUsed;
public TranscriptAnalysis(String filePath)
{
try {
scan = new Scanner(new File(filePath));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
public void readFileToArray()
{
String temp;
while(scan.hasNext())
{
temp = scan.next();
temp = stripAndConvertString(temp);
int index = this.isAlreadyInArray(temp);
if(index != -1)
words[index].incrementCount();
else
{
words[largestIndexUsed] = new Word(temp);
this.largestIndexUsed++;
}
}
}
* This method takes the String parameter, makes it lowercase
* and strips it of:
* periods, colons, apostrophes, open parrentheses, and close
* parentheses. Once stripped it returns it
* @param str
* @return
*/
private String stripAndConvertString(String str)
{
str=str.toLowerCase();
str.trim();
str.replace("." ,"");
str.replace(";" ,"");
str.replace("'" ,"");
str.replace("(" ,"");
str.replace(")" ,"");
return str;
}
* This method moves through the array checking each indexes
* Word obejct to see if the word stored in the array is the word
* we are checking to see if it exists. If you find a match return
* the index of the word; if you don't find a match return -1.
* @param str The word we are checking to see if it exists
* @return the index of the word if found; -1 if not.
*/
private int isAlreadyInArray(String str)
{
{for(int x=0;x<largestIndexUsed;x++)
{String word=words[x].getWord();
if(word.equals(str))
return x;
}
return -1;
}
}
public void writeArrayToFile(String path)
{
try {FileWriter Writer= new FileWriter(path);
for(int x=0;x<largestIndexUsed;x++)
Writer.write(words[x].toString()+"\n");
Writer.close();
}catch(IOException e){
System.out.println("File path is incorrect");
}
}
* This method orders the array by the highest
* to lowest word count.
public void orderByFrequencyArray()
{
}
public void graphIt(){
String temps;
int temp;
int offset;
int longest = 0;
for(int l = 0; l< this.largestIndexUsed; l++){
temps = words[l].getWord();
temp = words[l].getWord().length();
if(longest<temp){
longest = temp;
}
}
for(int l = 0; l< this.largestIndexUsed; l++){
temps = words[l].getWord();
temp = temps.length();
offset = longest - temp;
System.out.print(temps);
for(int lolers = 0; lolers<offset+3; lolers++){
System.out.print(" ");
}
for(int xes = 0; xes<words[l].getCount(); xes++)
{
System.out.print("X");
}
System.out.println();
}
}
public Word[] getWords() {
return words;
}
public int getLargestIndexUsed() {
return largestIndexUsed;
}
}
here is the word class
public class Word implements Comparable<Word>{
private String word;
private int count;
public Word(String w)
{
this.word = w;
count = 1;
}
public void incrementCount()
{
count++;
}
public int compareTo(Word w)
{
if(this.count > w.getCount())
return -1;
else if(this.count < w.getCount())
return 1;
else
return 0;
}
public String toString()
{
return word + " :: " + count + "\n";
}
public String getWord() {
return word;
}
public int getCount() {
return count;
}
}