Hello guys
Here I am again, now with a problem in I/O.
The objective of this program is to input a word in the terminal/console and then the scanner would search the word in a specific txt file and would return with the result if the word exists or not.
import comp102x.IO; import java.io.File; import java.io.IOException; import java.util.Scanner; public class SearchRecordDemo { // for loading OCR libraries private static Loader loader = new Loader(); /** * Askes user for an input word and searches the word from a record. * Prints whether the word exist in the record or not. * */ public void searchWord() throws IOException { // Input from console String word = IO.inputString(); /* Input from image by performing Optical Character Recognition(OCR) //String word = IO.inputTextImage(); word = removeExtraSpace(word); IO.outputln(word);*/ boolean exist = searchFromRecord("record.txt", word); if (exist) { System.out.println("The word \"" + word + "\" is in the record."); } else { System.out.println("The word \"" + word + "\" is not in the record."); } } /** * Removes the extra spaces from a word. * * @param word The String to be processed. */ private String removeExtraSpace(String word) { return word.replace("\n", "").replace("[ ]{2, }", ""); } /** * Searches the record and returns if a specified word is in the record. * * @param recordName The name of the record text file. * @param word The word to be searched. * @return true if the word presents in the record, false otherwise. */ [B]private boolean searchFromRecord(String recordName, String word) throws IOException[/B] { //method with problems Scanner scanner=new Scanner("record.txt"); if(recordName.contains(word)){ return true; } else{ // not found return false; } } }
After running the method searchWord(), I input in the terminal the word "ME" (it exists in the text file) and it return with false statement. Actually it return false with any word I input.
Debugger is saying that recordName="record.txt". It's correct, no ? recordName is the variable that will store the words that exists in the text file.
I need help