我正在开发一个程序,允许用户通过键盘输入指定三种功能之一。除了一个功能外,每个功能都能正常工作。当它退出到初始提示时,它会向控制台输出两次提示。
这是开场提示: “输入'R / r'来读取文件;'S / s'来搜索文件中的文本;'W / w'来写入文件;'E / e'来退出”
当我运行任何列出的函数(禁止退出)时,它们会正常执行并返回到打开提示符。但是,搜索功能会将打开提示两次打印到控制台而不是一次。
这是开启:
的代码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);
}
}
}
这是搜索:
的代码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.");
}
}
我期望的互动和输出是:
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
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
答案 0 :(得分:2)
呼叫scanner.next()
留下换行符,然后在scanner.nextLine()
调用中读取。在scanner.nextLine()
之后添加searchWord.next();
或更好地将searchWord.next();
更改为searchWord.nextLine();