ok I poseted about an assignment that i had to do for my Java programming class. Now i have attempted it and i have kind of gotten it to work but its not working properly ... i have no idea what i'm doing wrong ... please help me fix my program.
Here is what it is supposed to do:
Your program must:
- Not declare any class attributes. You will not need them in a carefully designed class.
- Read a single speech text file into an array of words, and then print to the screen the total number of words found in the file. For example, the Fort Bragg speech contains 3654 words.
- Display to the screen the number of unique words as well as the percentage of unique words to total words. For example, the Fort Bragg speech contains 995 unique words, representing 27% of the total.
- Prompt the user for any word, and then display the number of occurrences of that word in the speech.
- Output a text file that consists of a listing of the unique words in the speech with the number of occurrences of each word following the word. The file lists the words starting with those occurring most often and ending with those occurring once only. If you have words that occur the same number of times list them in alphabetical order.
Here is what i have done so far
import javax.swing.JOptionPane;
import javax.swing.JFileChooser;
import java.util.Scanner;
import java.util.StringTokenizer;
import java.io.*;
public class Assn2_5jnv {
public static String bigAssStringMaker (Scanner txtFile){
String bigAssString = new String();
while(txtFile.hasNextLine()){
bigAssString = bigAssString + txtFile.nextLine();
}
return(bigAssString);
}
public static int wordCount (Scanner fileText){
String bigString;
int count = 0;
bigString = bigAssStringMaker(fileText);
StringTokenizer temp = new StringTokenizer(bigString, " 0123456789,.;:–-!$?()[]&#\'\"\t\n");
while(temp.hasMoreTokens()){
count++;
temp.nextToken();
}
return(count);
}
public static void fileToArray (Scanner textFile, String[] importString){
int k = 0;
String largeString;
largeString = bigAssStringMaker(textFile);
StringTokenizer toke = new StringTokenizer(largeString, " 0123456789,.;:–-!$?()[]&#\'\"\t\n");
while(toke.hasMoreTokens()){
importString[k] = toke.nextToken();
k++;
}
}
public static void sortArray(String[] alphabeticalArray){
int index, indexOfNextSmallest;
int numberUsed = alphabeticalArray.length;
for(index = 0; index < numberUsed; index++){
indexOfNextSmallest = indexOfSmallest(index, alphabeticalArray, numberUsed);
interchange(index, indexOfNextSmallest, alphabeticalArray);
}
}
public static int indexOfSmallest(int startIndex, String[] speechArray, int numberUsed){
String min = speechArray[startIndex];
int indexOfMin = startIndex;
int index;
for(index = startIndex + 1; index < numberUsed; index++){
if(speechArray[index].compareToIgnoreCase(min)<0){
min = speechArray[index];
indexOfMin = index;
//min s the smallest of speechAray[startIndex] through speechArray[index]
}
}
return(indexOfMin);
}
public static void interchange(int i, int j, String[] array){
String temp = null;
temp = array[i];
array[i] = array[j];
array[j] = temp;//original value of array[i]
}
public static int countNumberUnique(String[] arrayInAlpha){
int counter = 1;
for(int i = 0; i < arrayInAlpha.length - 1; i++){
if(arrayInAlpha[i].compareToIgnoreCase(arrayInAlpha[i + 1]) < 0){
counter++;
}
}
return(counter);
}
public static void createUniqueWordArray(String[] arrayAlpha, String[] UniqueArray, int[] numEachElem){
int k = 0;
int j = 0;
for(int i = 0; i < arrayAlpha.length - 1; i++){
j++;
if(arrayAlpha[i].compareToIgnoreCase(arrayAlpha[i + 1]) < 0){
numEachElem[k] = j;
j = 0;
UniqueArray[k] = arrayAlpha[i];
k++;
}
}
}
public static void sameNumberSearch (String[] arrayUni, int[] numEachEl){
int start = 0;
int finish = 0;
int j = 0;
for(int k = 0; k < arrayUni.length -1; k++){
if(numEachEl[k] == numEachEl[k+1])
{
j = k;
start = k;
finish = k+1;
while(numEachEl[j+1] == numEachEl[j+2])
{
finish++;
j++;
}
k = finish;
}
AlphaSort(start, finish, arrayUni);
}
}
public static void AlphaSort(int start, int finish, String[] arrayUni)
{
int index, IndexofNextSmallest;
for(index = 0; index < finish; index++){
IndexofNextSmallest = IndexofSmall(start, finish, arrayUni);
interchange(index, IndexofNextSmallest, arrayUni);
}
}
public static int IndexofSmall(int start, int finish, String[] ArrayUni)
{
String min = ArrayUni[start];
int indexOfMin = start;
int index;
for(index = start + 1; index < finish; index++){
if(ArrayUni[index].compareToIgnoreCase(min)<0){
min = ArrayUni[index];
indexOfMin = index;
//min s the smallest of speechAray[startIndex] through speechArray[index]
}
}
return(indexOfMin);
}
public static void numericalSort(String[] uniqueString, int[] numberArray){
int index, indexOfNextLargest;
int numberUsed = numberArray.length;
for(index = 0; index < numberUsed; index++){
indexOfNextLargest = indexOfLargestNum(index, numberArray, numberUsed);
interchange(index, indexOfNextLargest, uniqueString);
interchangeNum(index, indexOfNextLargest, numberArray);
}
}
public static int indexOfLargestNum(int startIndex, int[] numArr, int numberUsed){
int max = numArr[startIndex];
int indexOfMax = startIndex;
int index;
for(index = startIndex + 1; index < numberUsed; index++){
if(numArr[index] > max){
max = numArr[index];
indexOfMax = index;
//min s the smallest of speechAray[startIndex] through speechArray[index]
}
}
return(indexOfMax);
}
public static void interchangeNum(int i, int j, int[] array){
int temp = 0;
temp = array[i];
array[i] = array[j];
array[j] = temp;//original value of array[i]
}
public static int wordSearch(String userWord, String[] uniqueArr, int[] numUnique){
int k = 0;
int j = 0;
if(uniqueArr[0].compareToIgnoreCase(userWord) == 0)
return k;
else{
while(j == 0){
k++;
if(uniqueArr[k].compareToIgnoreCase(userWord) == 0)
j = 1;
if(k == uniqueArr.length - 2){
j = 1;
k = -1;
}
}
}
return k;
}
public static void printArray(String[] uniqueArr, int[] numElem){
PrintWriter outputStream = null;
try{
outputStream = new PrintWriter(new FileOutputStream("Output.txt"));
}
catch(FileNotFoundException e){
System.out.println("Error opening the File.");
System.exit(0);
}
System.out.println("Writing to File.");
for(int k = 0; k < uniqueArr.length - 1; k++){
outputStream.println(uniqueArr[k] + ": " + numElem[k]);
}
outputStream.close();
}
public static void main(String[] args) {
File textFile;
Scanner fileInput = null;
Scanner fileInput2 = null;
JFileChooser chooser = new JFileChooser();
// Open file chooser dialog box
int returnVal = chooser.showOpenDialog(null);
// If user has selected a file, continue, exit with message if not
if(returnVal == JFileChooser.APPROVE_OPTION) {
// Get File object from dialog
textFile = chooser.getSelectedFile();
// Try instantiating Scanner object with File object provided
try {
fileInput = new Scanner(textFile);
} catch (FileNotFoundException e) {
// Maybe you do not have read privilige for file?
JOptionPane.showMessageDialog(null, "Cannot open file!");
System.exit(0);
} // end try/catch
// Try instantiating Scanner object with File object provided
try {
fileInput2 = new Scanner(textFile);
} catch (FileNotFoundException e) {
// Maybe you do not have read privilige for file?
JOptionPane.showMessageDialog(null, "Cannot open file!");
System.exit(0);
} // end try/catch
} // end if
else
// User might have clicked "Cancel" in file chooser dialog box
JOptionPane.showMessageDialog(null, "File not chosen.");
int numWords = wordCount(fileInput);
System.out.println("The total number of words is: " + numWords);
String[] array = new String[numWords];
fileToArray(fileInput2, array);
sortArray(array);
int numUniqueWords = countNumberUnique(array);
System.out.println("The total number of unique words is: " + numUniqueWords);
double percent = (double)numUniqueWords/numWords * 100;
System.out.println("The percentage of unique words to total words is: " + percent + "%");
String[] uniqueArray = new String[numUniqueWords];
int[] numEachUnique = new int[numUniqueWords];
createUniqueWordArray(array, uniqueArray, numEachUnique);
numericalSort(uniqueArray, numEachUnique);
/*
sameNumberSearch(uniqueArray, numEachUnique);
*/
String userInput;
System.out.println("Enter a word to search for: ");
Scanner keyboard = new Scanner(System.in);
String word = keyboard.nextLine();
int index = wordSearch(word, uniqueArray, numEachUnique);
if(index >= 0)
System.out.println("The word you have chosen occurs " + numEachUnique[index] + " Times");
else
System.out.println("The word you have chosen does not occur in this speech.");
printArray(uniqueArray, numEachUnique);
fileInput.close();
System.out.println("End of Program Bitches!!");
} // end main
}