After every word on a line is spellchecked, it should be written to an output file called checked.txt. The arrangement of words on lines should be the same in the document and in checked.txt. The difference between the document and checked.txt is that in checked.txt each word is in lowercase and there is no punctuation. Also, some words might be changed by the spellcheck process. For example, suppose the document contains the following: The Hunter Moon waxed round in the night sky, and put to flight all the lesser stars. But low in the South one star shone red. Then checked.txt will contain: the hunter moon waxed round in the night sky and put to flight all the lesser stars but low in the south one star shone red (In a real spellchecker, the output will be in the same case as the original, and all punctuation will be retained. For simplicity, we will not worry about case and punctuation.) How to Process Words Which Are Not in the Dictionary The spellcheck method also needs to handle the processing for words that are not found. If a word is not found in the dictionary, the program will print a message indicating that the word was not found. Then it will ask the user whether to add the word to the dictionary, whether the word should be changed, or whether processing should continue with no change. yikes not found in dictionary 1 : replace in text file 2 : add to dictionary 3 : continue ==> 3 [yikes will remain and the dictionary is unchanged] If user chooses to change the word, then the user will be prompted for the new word and the new word will replace the original word. If the new word is not in the dictionary, it will be added to the dictionary. For example, yikes not found in dictionary 1 : replace in text file 2 : add to dictionary 3 : continue ==> 1 enter word to replace yikes ==> yippee [yippee will appear instead of yikes yippee will be added to the dictionary if it is not already there] If the user chooses to add the word to the dictionary, the word is added to the dictionary and it is not changed. yikes not found in dictionary 1 : replace in text file 2 : add to dictionary 3 : continue ==> 2 yikes added to dictionary [yikes will remain] If the user chooses to continue, no change is made. The word is unchanged and the dictionary is unchanged. In the first case, yikes will be replaced by yippee in the output file checked.txt. In the second case yikes will appear in checked.txt and it will be added to the dictionary. In the third case yikes will appear in checked.txt but will not be added to the dictionary. Do not write one giant method to do all the spellchecking! Create some private methods that the spellcheck method will call to do parts of the processing. Client Code The main method will prompt for the name of the file containing the dictionary and the name of the input file to be spellchecked. Create the Scanner for the dictionary file and pass it to the Dictionary constructor. Create the Scanner for the input file, perform the spellcheck, and write out the updated dictionary to the file newDict.txt. Use exception handling in the main method to ensure the input files were found and opened properly. If not handle the situation by prompting the user again for the correct file name. Do not use any set or get methods. Hints Write your Dictionary class first. Create a driver program to test the Dictionary. Create a file that has only a few words (say 25) to use when testing your Dictionary class. Write your client code next. Write your Spellcheck class next. Start with a spellCheck method that doesn't do any spellchecking. Write the code to use split and the other String methods to separate and clean up the words and then write them to checked.txt. Write the code to implement option 3: if a word is not in the dictionary, don't change the word and don't change the dictionary. Test it with your small dictionary file. Add the code for option 2: if a word is not in the dictionary, add the word to the dictionary. Test it with your small dictionary file so that it's easy to check whether the word is added to the dictionary. Start with just one word that's not in the dictionary, then test with more than one, including more than one on the same line. Lastly, add the code for option 1: if a word is not in the dictionary, prompt for a replacement word, add the replacement word to the dictionary and change the original word to the replacement word in the input. Again, test it with your small dictionary file so that it's easy to check whether the word is added to the dictionary. Also make sure that the word is replaced in checked.txt. Test with the full dictionary. Lab Standards No global variables. Ever. When prompting a user in a loop, tell him/her what to enter to get out of the loop. Don't give an error message after the user enters a code that should exit from a loop. Use a priming read to avoid this. Use meaningful variable names. Names like i, j, or k should only be used for loop counters. Don't write infinite loops with a break inside
After every word on a line is spellchecked, it should be written to an output file called checked.txt. The arrangement of words on lines should be the same in the document and in checked.txt. The difference between the document and checked.txt is that in checked.txt each word is in lowercase and there is no punctuation. Also, some words might be changed by the spellcheck process. For example, suppose the document contains the following:
The Hunter Moon waxed round in the night sky, and put to flight all the lesser stars. But low in the South one star shone red.
Then checked.txt will contain:
the hunter moon waxed round in the night sky and put to flight all the lesser stars but low in the south one star shone red
(In a real spellchecker, the output will be in the same case as the original, and all punctuation will be retained. For simplicity, we will not worry about case and punctuation.)
How to Process Words Which Are Not in the Dictionary
The spellcheck method also needs to handle the processing for words that are not found.
If a word is not found in the dictionary, the program will print a message indicating that the word was not found. Then it will ask the user whether to add the word to the dictionary, whether the word should be changed, or whether processing should continue with no change.
yikes not found in dictionary 1 : replace in text file 2 : add to dictionary 3 : continue ==> 3 [yikes will remain and the dictionary is unchanged]
- If user chooses to change the word, then the user will be prompted for the new word and the new word will replace the original word. If the new word is not in the dictionary, it will be added to the dictionary. For example, yikes not found in dictionary 1 : replace in text file 2 : add to dictionary 3 : continue ==> 1 enter word to replace yikes ==> yippee [yippee will appear instead of yikes yippee will be added to the dictionary if it is not already there]
- If the user chooses to add the word to the dictionary, the word is added to the dictionary and it is not changed. yikes not found in dictionary 1 : replace in text file 2 : add to dictionary 3 : continue ==> 2 yikes added to dictionary [yikes will remain]
- If the user chooses to continue, no change is made. The word is unchanged and the dictionary is unchanged.
In the first case, yikes will be replaced by yippee in the output file checked.txt. In the second case yikes will appear in checked.txt and it will be added to the dictionary. In the third case yikes will appear in checked.txt but will not be added to the dictionary.
Do not write one giant method to do all the spellchecking! Create some private methods that the spellcheck method will call to do parts of the processing.
Client Code
The main method will prompt for the name of the file containing the dictionary and the name of the input file to be spellchecked. Create the Scanner for the dictionary file and pass it to the Dictionary constructor. Create the Scanner for the input file, perform the spellcheck, and write out the updated dictionary to the file newDict.txt.
Use exception handling in the main method to ensure the input files were found and opened properly. If not handle the situation by prompting the user again for the correct file name.
Do not use any set or get methods.
Hints
- Write your Dictionary class first. Create a driver program to test the Dictionary. Create a file that has only a few words (say 25) to use when testing your Dictionary class.
- Write your client code next.
- Write your Spellcheck class next. Start with a spellCheck method that doesn't do any spellchecking. Write the code to use split and the other String methods to separate and clean up the words and then write them to checked.txt.
- Write the code to implement option 3: if a word is not in the dictionary, don't change the word and don't change the dictionary. Test it with your small dictionary file.
- Add the code for option 2: if a word is not in the dictionary, add the word to the dictionary. Test it with your small dictionary file so that it's easy to check whether the word is added to the dictionary. Start with just one word that's not in the dictionary, then test with more than one, including more than one on the same line.
- Lastly, add the code for option 1: if a word is not in the dictionary, prompt for a replacement word, add the replacement word to the dictionary and change the original word to the replacement word in the input. Again, test it with your small dictionary file so that it's easy to check whether the word is added to the dictionary. Also make sure that the word is replaced in checked.txt.
- Test with the full dictionary.
Lab Standards
- No global variables. Ever.
- When prompting a user in a loop, tell him/her what to enter to get out of the loop.
- Don't give an error message after the user enters a code that should exit from a loop. Use a priming read to avoid this.
- Use meaningful variable names. Names like i, j, or k should only be used for loop counters.
- Don't write infinite loops with a break inside
Trending now
This is a popular solution!
Step by step
Solved in 6 steps with 1 images