1. Engineering
  2. Computer Science
  3. java i have done the program but something needs to...

Question: java i have done the program but something needs to...

Question details

Java: I have done the program, but something needs to be fixed, please read the instructions below. Assume I have a file name "inputfile.txt" that contains words: "The central processing unit is the computer's brain, it retrieves instructions from memory and executes them. " I don't know how to make the words in the index unique (see highlighted words below), cuz there are two "the" in the sentence, my output suppose to have one "the" instead of two. and I failed to do step 2 as well. (my code was pasted at the buttom)

Write a Java program called BookIndex to keep track of an index of words that appears in a text file. Although an index typically references the page number of a word in a text, we will reference the word number of the first occurrence of a word since your input file will probably be short (and not paginated). Your program will read the text from a text file and create the index. The index will consist of 2 parallel arrays, one of type String and one of type int. The words in the index must be unique! Never store the same word twice. After populating the arrays, your program should do the following:

1. Sort both arrays in alphabetical order according to the words. Each time a pair of words is swapped, the corresponding pair of numbers must be swapped.

2. Allow the user to lookup a number by entering a word. Also, allow the user to enter the start of a word (at least one character) and print all of the words in the index that begin with that prefix, and their numbers.

Implementation Details:

 You may read one word at a time or read the entire file into one String and call the split method of the String class.

 In both cases, you will need to trim the punctuation marks from the words before they are placed in the index. You may use the replaceAll method in the String class for this.  Convert all strings to lowercase before placing in the index.

 Remember to use .equals instead of == and compareTo instead of when comparing String objects.

 You may use the sequential search for the lookup, but you are can also program the binary search since the array of words is sorted.

import java.util.Scanner;
import java.io.*;
public class BookIndex1 {
public static void main(String [] args) throws IOException{
File myfile = new File("inputfile.txt");
if (!myfile.exists()){
System.out.println("The file hw8inputfile.txt is not found.");
System.exit(0); //this will terminate the program if the file does not exist.
}
final int SIZE =100; //assume the array has size of 100
Scanner input = new Scanner(myfile);
String[] words = new String[SIZE];
int[] index = new int[SIZE];
System.out.printf("%-12s%15s\n", "words", "Index");
System.out.println("---------------------------");
int size=0;
while(input.hasNext()){
words[size] = input.next();
size++;
}
for(int i=0; i<size; i++){
index[i]=i;
}
convertLowerCase(words, size); //convert all words to lowercase
removePun(words, size); //remove all punctuation
sortArrays(words, index, size); //sort array alphabetically
displayArrays(words, index, size);
searchWords(words, index, size); //allow user to type a keyword and find its index   
}
public static void convertLowerCase(String[] words, int size){
for(int i=0; i<size; i++){
words[i]=words[i].toLowerCase();
}
}
public static void removePun(String[] words, int size){
for(int i=0; i<size; i++){
words[i]=words[i].replaceAll("[, . ? : ; !]", " ");
}
}
  
public static void sortArrays(String words[], int wordIndex[], int arraySize){
for(int i=0; i<arraySize; i++){
for(int j=0; j<arraySize-i-1; j++){
if(words[j].compareTo(words[j+1])>0){
String temp = words[j];
words[j]=words[j+1];
words[j+1]=temp;
int tempIndex=wordIndex[j];
wordIndex[j]=wordIndex[j+1];
wordIndex[j+1]=tempIndex;
}
}
}
}
public static void displayArrays(String words[], int[] wordIndex, int arraySize){
for(int i=0; i<arraySize; i++){
System.out.printf("%-12s%15d\n", words[i],wordIndex[i]);
}
}
public static void searchWords(String words[], int[] wordIndex, int arraySize){

//this is what I have tried in step 2: set the while loop 1000 tie to allow the user to enter the keyword 1000 times,

//is there any other ways to allow the user to search a keyword without limits?

// there is a minor bug in this program segment when I enter "them" which is index 15 in the array, it doesn't display anything. can you figure out what's wrong?

//Also, can you write some code to allow the user to enter the start of a word (at least one character) and print all of the words in the index that begin with that prefix, and their numbers.
Scanner sc = new Scanner(System.in);
int searching = 0;
while(searching<1000){
System.out.println("Enter keyword: ");
String word = sc.next();
int index;
for(int i=0; i<arraySize; i++){
if (words[i].equals(word)){
index=wordIndex[i];
System.out.println(word+" found at index "+index);
}
  
}
}
}
}


Solution by an expert tutor
Blurred Solution
This question has been solved
Subscribe to see this solution