Java 为什么我的程序在退出一个功能时向控制台打印两次打开提示,而在退出其他功能时打印一次?

Java 为什么我的程序在退出一个功能时向控制台打印两次打开提示,而在退出其他功能时打印一次?,java,Java,我正在开发一个程序,允许用户通过键盘输入指定三个功能之一。除一个功能外,每个功能都正常工作。当它退出回到初始提示时,它会将提示输出到控制台两次 以下是开场白: “键入'R/R'读取文件;'S/S'搜索文件中的文本;'W/W'写入文件;'E/E'退出” 当我运行任何列出的功能(禁止退出)时,它们会正常执行并返回到打开提示。但是,搜索功能将打开提示打印到控制台两次,而不是一次 这是开头的代码: public static void main(String args[]) throws IOExcep

我正在开发一个程序,允许用户通过键盘输入指定三个功能之一。除一个功能外,每个功能都正常工作。当它退出回到初始提示时,它会将提示输出到控制台两次

以下是开场白: “键入'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.");
    }
}
这是搜索的代码:

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            

调用
scanner.next()
会留下一个换行字符,然后在调用
scanner.nextLine()
时读取该字符。在
searchWord.next()之后添加一个
scanner.nextLine()
或更好地更改
searchWord.next()
to
searchWord.nextLine()

通过在调试器中单步执行,您可以轻松回答此类问题。阅读单词时,请尝试使用
nextLine()
而不是
next()
。特别是,这一行
stringwordinput=searchWord.next()
您的
main
-方法中的while循环也有错误。您正在将扫描器与字符串“e”进行相等性比较,该字符串永远不会为真:
!inputChoice.equals(“e”)
@HendrikSimon我使用它,这样Main就不会在任何函数的一次迭代后终止。只有当suer指定时,inputChoice才会等同于“e”。@Joshua不,
inputChoice
永远不会等同于
'e'
。它是一个
扫描仪
,而不是
字符串
。您可能想引用
userChoice
,但此时尚未声明该变量。因此“.next()”提供了一个回车符,允许将提示读取为下一行?
。next()
将换行符保留在用户输入要搜索的术语时键入的行尾,因此,当主程序读取下一个
userChoice
@JoshuaNapier时,它仍然在缓冲区中-是的
next()
留下一个换行符,该换行符将由
nextLine()
读取。请通读用于将此问题标记为dup的问题。(当,在回答之前应该这样做:P)