I'm working on a program that allows the user to specify one of three functions via keyboard input. Each of the functions works properly except for one. When it exits back to the initial prompt it outputs the prompt to the console twice.
Here's the opening prompt: "Type 'R/r' to read a file; 'S/s' to search for text within a file; 'W/w' to write to a file; 'E/e' to exit"
When I run any of the listed functions (barring exit) they execute normally and return to the opening prompt. The search function however prints the opening prompt twice to the console instead of once.
This is the code for the Opening:
public static void main(String args[]) throws IOException{
//Initialize scanner and a string variable to hold the value of scanner variable
Scanner inputChoice = new Scanner(System.in); //iChoice - inputChoice
while(!inputChoice.equals("e")){
//Prompt user to provide input in accordance with desired function
System.out.println("Type 'R/r' to read a file; 'S/s' to search for text within a file; 'W/w' to write to a file; 'E/e' to exit");
String userChoice = inputChoice.nextLine();
//If user specifies "r" go to fileReader class
if(userChoice.equalsIgnoreCase("r")){
SimpleDBReader sdbrObject = new SimpleDBReader();
sdbrObject.sdbReader();
//If user specifies "s" go to textSearch class
}else if(userChoice.equalsIgnoreCase("s")){
SimpleDBSearch sdbsObject = new SimpleDBSearch();
sdbsObject.sdbSearch(inputChoice);
//If user specifies "w" go to fileWriter class
}else if(userChoice.equalsIgnoreCase("w")){
SimpleDBWriter sdbwObject = new SimpleDBWriter();
sdbwObject.sdbWriter(inputChoice);
//If user specifies "e" terminate program
}else if(userChoice.equalsIgnoreCase("e")){
inputChoice.close();
System.exit(0);
}
}
}
This is the code for the Search:
public void sdbSearch(Scanner searchWord) throws IOException{
//Prompt user for input
System.out.println("Please input the word you wish to find:");
//Init string var containing user input
String wordInput = searchWord.next();
//Specify file to search & init Scanner containing file
File file = new File("C:/Users/Joshua/Desktop/jOutFiles/SimpleDb.txt");
Scanner fileScanner = new Scanner(file);
//Set flag - for below loop - to false
boolean stringFound = false;
//Loops through every line looking for lines containing previously specified string.
while(fileScanner.hasNextLine()){
String line = fileScanner.nextLine();
if(line.contains(wordInput)){ //If desired string is found set flag true and print line containing it to console
stringFound = true;
System.out.println("I found the word you're looking for here: " + line);
}
}
//Check if flag false, prompt user for new input
if(!stringFound){
System.out.println("The word you were looking for does not exist.");
}
}
The interaction and output I expect is:
Type 'R/r' to read a file; 'S/s' to search for text within a file; 'W/w' to write to a file; 'E/e' to exit //SysOut
s //User
Please input the word you wish to find: //SysOut
someString //User
I found the word you're looking for here: lineWithString OR The word you were looking for does not exist. //SysOut
Type 'R/r' to read a file; 'S/s' to search for text within a file; 'W/w' to write to a file; 'E/e' to exit //SysOut
What I get is:
Type 'R/r' to read a file; 'S/s' to search for text within a file; 'W/w' to write to a file; 'E/e' to exit //SysOut
s //User
Please input the word you wish to find: //SysOut
someString //User
I found the word you're looking for here: lineWithString OR The word you were looking for does not exist. //SysOut
Type 'R/r' to read a file; 'S/s' to search for text within a file; 'W/w' to write to a file; 'E/e' to exit //SysOut
Type 'R/r' to read a file; 'S/s' to search for text within a file; 'W/w' to write to a file; 'E/e' to exit //SysOut