Problems with Scanner - Java

老子叫甜甜 提交于 2020-01-01 15:07:17

问题


I'm trying to have the option of reading a string with multiple words, ie. Los Angeles or New York City. Using scanner.next() for "Departure" and "Arrival" would only read the first one if there were two words and split them between variables. nextLine() has not been much luck either. Here's my code:

            System.out.print("\nEnter flight number: ");
            int flightNumber = Integer.valueOf(scanner.nextLine());
            System.out.print("\nEnter departing city: ");
            String departingCity = scanner.nextLine();
            System.out.print("\nEnter arrival city: ");
            String arrivalCity = scanner.nextLine();

I know it's something simple but I haven't figured it out.

Here's the input/output w/ the code above:

Enter flight number: 29

Enter departing city: (immediately it skips to the next line)

Enter arrival city:

---- What I'm really going for ----

Enter flight number: 29

Enter departing City: Los Angeles (be able to type multiple words without it skipping the next input)

Enter arrival city: Kansas City


回答1:


Your problem is that next() does not read the carriage return and it gets automatically read by your next next() or nextLine(). Use nextLine() all time and convert input to integer:

public static void main(String[] args) throws Exception {
    Scanner scanner = new Scanner(System.in);
    System.out.print("\nEnter flight number: ");
    int flightNumber = Integer.valueOf(scanner.nextLine());
    System.out.print("\nEnter departing city: ");
    String departingCity = scanner.nextLine();
    System.out.print("\nEnter arrival city: ");
    String arrivalCity = scanner.nextLine();

}



回答2:


Integer.parseInt(scanner.nextLine()) would also work--it returns an int, while Integer.valueOf(scanner.nextLine()) returns an Integer.

As an alternative to @Edwin Dalorzo's suggestion, you can call nextInt() to grab the next token from the input stream and try to convert it to an int. This method will throw an InputMismatchException if conversion to an int was unsuccessful. Otherwise, it will grab only the int value entered. Calling nextLine(), will then grab anything else that was entered in the line after the int. In addition, nextLine() will consume the newline character added when the user pressed "enter" to submit the input (it will advance past it and discard it).

If you want to be sure that the user didn't enter anything except an int before pressing "Enter," call nextInt() first and then make sure the value of nextLine() is empty. If you don't care about anything entered in the line after the int, you can ignore what nextLine() returns but should still call that method to consume the newline character.

Search StackOverflow for "java scanner next" or "java scanner nextLine" to find threads on this subject.



来源:https://stackoverflow.com/questions/5784540/problems-with-scanner-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!