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

标签 java

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

以下是打开提示: “输入‘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            

最佳答案

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

关于java - 为什么我的程序在退出一个功能时将打开提示打印到控制台两次,而在退出其他功能时却将打开提示打印到控制台一次?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33656695/

相关文章:

java - 实现我自己的字符串比较

java - 将 java.util.Map[String, Object] 转换为 scala.collection.immutable.Map[String, Any]

java - 如何在Java EE环境中使用MySql处理外来字符

java - 正则表达式匹配用多个逗号分隔的数字

java - 如何将 DSL 语法转换为脚本语法

java - 在日期选择器中显示两次的日期

java - 致命异常 : main java. lang.ArrayIndexOutOfBoundsException:

Java 加盐密码哈希

java - NullPointerException,是什么导致了我的错误?

java - 通过 Android 上的 TCP 套接字流式传输音频