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
 
}

What are the errors you are recieving if any, or is it just not doing what you want it to do.

Its just not doing what i want it to do.

When i am creating the array of all unique words it loses the very last word and i dont know how to fix that.

And i also dont know how to sort the last array properly ... i can put it in order from occurs most often to occurs least but i cant get the words that occur the same amount of times in alphabetical order.

I am just really lost and stuck ... my assignment is due at 7 pm tonight so in less than 6 hours ... if you can maybe possibly just fix my progam to do what the assignment asks and send it back to me because i have no idea how to do it.

thanks

Here is what i have now ... i fixed the problem where i was losing words ... now all i have left to do is sort my array from occurs most often to occurs least often with the words with the same number of occurences in alphabetical order .... if i can get that i will be done.

here's my code

[B]import[/B] javax.swing.JOptionPane;
[B]import[/B] javax.swing.JFileChooser;
[B]import[/B] java.util.Scanner;
[B]import[/B] java.util.StringTokenizer;
[B]import[/B] java.io.*;
 
[B]public[/B][B]class[/B] Assn2_5jnv {
 
[B]public[/B] [B]static[/B] String bigAssStringMaker (Scanner txtFile){
 
String bigAssString = [B]new[/B] String();
txtFile.radix();
 
[B]while[/B](txtFile.hasNextLine()){
bigAssString = bigAssString + txtFile.nextLine();
}
 
[B]return[/B](bigAssString);
}
 
[B]public[/B] [B]static[/B] [B]int[/B] wordCount (Scanner fileText){
 
String bigString;
[B]int[/B] count = 0;
 
 
bigString = [I]bigAssStringMaker[/I](fileText);
 
StringTokenizer temp = [B]new[/B] StringTokenizer(bigString, " 0123456789,.;:–-!$?()[]&#\'\"\t\n");
 
[B]while[/B](temp.hasMoreTokens()){
 
count++;
temp.nextToken();
 
}
 
[B]return[/B](count);
 
}
 
[B]public[/B] [B]static[/B] [B]void[/B] fileToArray (Scanner textFile, String[] importString){
 
[B]int[/B] k = 0;
String largeString;
 
largeString = [I]bigAssStringMaker[/I](textFile);
 
StringTokenizer toke = [B]new[/B] StringTokenizer(largeString, " 0123456789,.;:–-!$?()[]&#\'\"\t\n");
 
[B]while[/B](toke.hasMoreTokens()){
 
importString[k] = toke.nextToken();
k++;
 
}
 
}
 
[B]public[/B] [B]static[/B] [B]void[/B] sortArray(String[] alphabeticalArray){
 
[B]int[/B] index, indexOfNextSmallest;
[B]int[/B] numberUsed = alphabeticalArray.length;
 
[B]for[/B](index = 0; index < numberUsed; index++){
 
indexOfNextSmallest = [I]indexOfSmallest[/I](index, alphabeticalArray, numberUsed);
[I]interchange[/I](index, indexOfNextSmallest, alphabeticalArray);
 
}
 
}
 
[B]public[/B] [B]static[/B] [B]int[/B] indexOfSmallest([B]int[/B] startIndex, String[] speechArray, [B]int[/B] numberUsed){
 
String min = speechArray[startIndex];
[B]int[/B] indexOfMin = startIndex;
[B]int[/B] index;
 
[B]for[/B](index = startIndex + 1; index < numberUsed; index++){
[B]if[/B](speechArray[index].compareToIgnoreCase(min)<0){
 
min = speechArray[index];
indexOfMin = index;
//min s the smallest of speechAray[startIndex] through speechArray[index]
 
}
}
 
[B]return[/B](indexOfMin);
 
}
 
[B]public[/B] [B]static[/B] [B]void[/B] interchange([B]int[/B] i, [B]int[/B] j, String[] array){
 
String temp = [B]null[/B];
temp = array[I];[/I]
[i]array[I] = array[j];[/I]
[I]array[j] = temp;//original value of array[/I]
 
[i][I]}[/I]
 
[I][B]public[/B] [B]static[/B] [B]int[/B] countNumberUnique(String[] arrayInAlpha){[/I]
 
[I][B]int[/B] counter = 1;[/I]
 
[I][B]for[/B]([B]int[/B] i = 0; i < arrayInAlpha.length - 1; i++){[/I]
 
[i][B]if[/B](arrayInAlpha[I].compareToIgnoreCase(arrayInAlpha[i + 1]) < 0){[/I]
 
[I]counter++;[/I]
 
[I]}[/I]
 
[I]}[/I]
 
[I][B]return[/B](counter);[/I]
 
[I]}[/I]
 
[I][B]public[/B] [B]static[/B] [B]void[/B] createUniqueWordArray(String[] arrayAlpha, String[] UniqueArray, [B]int[/B][] numEachElem){[/I]
 
[I][B]int[/B] k = 0;[/I]
[I][B]int[/B] j = 0;[/I]
 
[I][B]for[/B]([B]int[/B] i = 0; i < arrayAlpha.length - 1; i++){[/I]
 
[I]j++;[/I]
 
[i][B]if[/B](arrayAlpha[I].compareToIgnoreCase(arrayAlpha[i + 1]) < 0){[/I]
 
[I]numEachElem[k] = j;[/I]
[I]j = 0;[/I]
 
[i]UniqueArray[k] = arrayAlpha[I];[/I]
[I]k++;[/I]
 
[I]}[/I]
 
[I]}[/I]
 
[I]UniqueArray[k] = arrayAlpha[arrayAlpha.length - 1];//Needed to enter last element of UniqueArray[/I]
[I]numEachElem[k] = 1;[/I]
 
[I][B]for[/B]([B]int[/B] h = 0; h < UniqueArray.length; h++)[/I]
[i]System.[I]out[/I].println(UniqueArray[h]);
 
}
 
[B]public[/B] [B]static[/B] [B]void[/B] numericalSort(String[] uniqueString, [B]int[/B][] numberArray){
 
[B]int[/B] index, indexOfNextLargest;
[B]int[/B] numberUsed = numberArray.length;
 
[B]for[/B](index = 0; index < numberUsed; index++){
 
indexOfNextLargest = [I]indexOfLargestNum[/I](index, numberArray, numberUsed);
[I]interchange[/I](index, indexOfNextLargest, uniqueString);
[I]interchangeNum[/I](index, indexOfNextLargest, numberArray);
 
 
}
 
}
 
[B]public[/B] [B]static[/B] [B]int[/B] indexOfLargestNum([B]int[/B] startIndex, [B]int[/B][] numArr, [B]int[/B] numberUsed){
 
[B]int[/B] max = numArr[startIndex];
[B]int[/B] indexOfMax = startIndex;
[B]int[/B] index;
 
[B]for[/B](index = startIndex + 1; index < numberUsed; index++){
[B]if[/B](numArr[index] > max){
 
max = numArr[index];
indexOfMax = index;
//min s the smallest of speechAray[startIndex] through speechArray[index]
 
}
}
 
[B]return[/B](indexOfMax);
 
}
 
[B]public[/B] [B]static[/B] [B]void[/B] interchangeNum([B]int[/B] i, [B]int[/B] j, [B]int[/B][] array){
 
[B]int[/B] temp = 0;
temp = array[I];[/I]
[i]array[I] = array[j];[/I]
[I]array[j] = temp;//original value of array[/I]
 
[i][I]} [/I]
 
[I][B]public[/B] [B]static[/B] [B]int[/B] wordSearch(String userWord, String[] uniqueArr, [B]int[/B][] numUnique){[/I]
 
[I][B]int[/B] k = 0;[/I]
[I][B]int[/B] j = 0;[/I]
 
[I][B]if[/B](uniqueArr[0].compareToIgnoreCase(userWord) == 0)[/I]
[I][B]return[/B] k;[/I]
 
[I][B]else[/B]{[/I]
 
[I][B]while[/B](j == 0){[/I]
 
[I]k++;[/I]
 
[I][B]if[/B](uniqueArr[k].compareToIgnoreCase(userWord) == 0)[/I]
[I]j = 1;[/I]
 
[I][B]if[/B](k == uniqueArr.length){[/I]
 
[I]j = 1;[/I]
[I]k = -1;[/I]
 
[I]}[/I]
 
[I]}[/I]
[I]}[/I]
[I][B]return[/B] k;[/I]
 
[I]}[/I]
 
[I][B]public[/B] [B]static[/B] [B]void[/B] printArray(String[] uniqueArr, [B]int[/B][] numElem){[/I]
 
[I]PrintWriter outputStream = [B]null[/B];[/I]
 
[I][B]try[/B]{[/I]
 
[I]outputStream = [B]new[/B] PrintWriter([B]new[/B] FileOutputStream("Output.txt"));[/I]
 
[I]}[/I]
 
[I][B]catch[/B](FileNotFoundException e){[/I]
 
[i]System.[I]out[/I].println("Error opening the File.");
System.[I]exit[/I](0);
 
}
 
System.[I]out[/I].println("Writing to File.");
 
[B]for[/B]([B]int[/B] k = 0; k < uniqueArr.length - 1; k++){
 
outputStream.println(uniqueArr[k] + ": " + numElem[k]);
 
}
 
outputStream.close();
 
}
 
[B]public[/B] [B]static[/B] [B]void[/B] main(String[] args) {
 
File textFile;
Scanner fileInput = [B]null[/B];
Scanner fileInput2 = [B]null[/B];
 
 
 
JFileChooser chooser = [B]new[/B] JFileChooser();
 
// Open file chooser dialog box
[B]int[/B] returnVal = chooser.showOpenDialog([B]null[/B]);
 
// If user has selected a file, continue, exit with message if not
[B]if[/B](returnVal == JFileChooser.[I]APPROVE_OPTION[/I]) {
 
// Get File object from dialog
textFile = chooser.getSelectedFile();
 
// Try instantiating Scanner object with File object provided
[B]try[/B] {
fileInput = [B]new[/B] Scanner(textFile);
} [B]catch[/B] (FileNotFoundException e) {
// Maybe you do not have read privilige for file?
JOptionPane.[I]showMessageDialog[/I]([B]null[/B], "Cannot open file!");
System.[I]exit[/I](0);
} // end try/catch
 
// Try instantiating Scanner object with File object provided
[B]try[/B] {
fileInput2 = [B]new[/B] Scanner(textFile);
} [B]catch[/B] (FileNotFoundException e) {
// Maybe you do not have read privilige for file?
JOptionPane.[I]showMessageDialog[/I]([B]null[/B], "Cannot open file!");
System.[I]exit[/I](0);
} // end try/catch
 
} // end if
 
[B]else[/B]
// User might have clicked "Cancel" in file chooser dialog box
JOptionPane.[I]showMessageDialog[/I]([B]null[/B], "File not chosen.");
 
[B]int[/B] numWords = [I]wordCount[/I](fileInput);
 
System.[I]out[/I].println("The total number of words is: " + numWords);
 
String[] array = [B]new[/B] String[numWords];
 
[I]fileToArray[/I](fileInput2, array);
 
[I]sortArray[/I](array);
 
[B]int[/B] numUniqueWords = [I]countNumberUnique[/I](array);
 
System.[I]out[/I].println("The total number of unique words is: " + numUniqueWords);
[B]double[/B] percent = ([B]double[/B])numUniqueWords/numWords * 100;
System.[I]out[/I].println("The percentage of unique words to total words is: " + percent + "%");
 
String[] uniqueArray = [B]new[/B] String[numUniqueWords];
 
[B]int[/B][] numEachUnique = [B]new[/B] [B]int[/B][numUniqueWords];
 
[I]createUniqueWordArray[/I](array, uniqueArray, numEachUnique);
 
[I]numericalSort[/I](uniqueArray, numEachUnique);
 
System.[I]out[/I].println("Enter a word to search for: ");
Scanner keyboard = [B]new[/B] Scanner(System.[I]in[/I]);
String word = keyboard.nextLine();
 
[B]int[/B] index = [I]wordSearch[/I](word, uniqueArray, numEachUnique);
 
[B]if[/B](index >= 0)
System.[I]out[/I].println("The word you have chosen occurs " + numEachUnique[index] + " Times");
[B]else[/B]
System.[I]out[/I].println("The word you have chosen does not occur in this speech.");
 
[I]printArray[/I](uniqueArray, numEachUnique);
 
fileInput.close();
 
System.[I]out[/I].println("End of Program Bitches!!");
 
} // end main
 
}

Please someone help me i have to hand in my program in like 5 hours

All i need is a method to sort the array that has already been sorted from occurs most to occurs least so that all the words with the same number of occurences are in alphabetical order.

This article has been dead for over six months. Start a new discussion instead.