Scanner for string does not work in Java

When I use scan.nextLine (), the input fields do not work properly. If it scan.next () works fine. But why? Shouldn't I use scan.nextLine () for a string?

import java.util.Scanner; public class Test{ public static void main(String[] args){ Scanner scan = new Scanner(System.in); int x = scan.nextInt(); System.out.println("p"); String p = scan.nextLine(); System.out.println("q"); String q = scan.next(); System.out.println("m"); String m = scan.next(); } } 
+5
source share
3 answers

Before using them, try checking the documents.
Cause:

Scanner.nextLine: The java.util.Scanner.nextLine () method moves this scanner past the current line and returns a missed input. This method returns the rest of the current line, with the exception of the line separator at the end. The position is set to the beginning of the next line. Since this method continues searching through an input looking for a line separator, it can buffer the entire input line search to skip if there are no line separators.

While this does not apply to Scanner.nextInt

Therefore, the Scanner.nextInt method does not use the last character of the newline of your input, and thus this line is used in the next call to Scanner.nextLine.

The main solution would be to use the empty Scanner.nextLine after Scanner.nextInt just to consume the rest of this line, including a new line.

For example

 int myVal1 = input.nextInt(); input.nextLine(); String myStr1 = input.nextLine(); 
+1
source

You should use nextLine and then convert it to the expected types.

In the above script, read the line, then translate it to an integer, because next and nextInt just read the input before the space occurs. Therefore, when you call nextInt , it just consumes the number and leaves the newLine character that will be consumed in nextLine .

From the question, it seems like this is how you are going to read the input.

  • The first integer.
  • Second line.
  • The third line will be two words separated by a space.

This is your code.

  Scanner scan = new Scanner(System.in); int x = Integer.parseInt(scan.nextLine()); //read line and then cast to integer System.out.println("p"); String p = scan.nextLine(); System.out.println("qm"); String[] linesParts = scan.nextLine().split(" "); // read the whole line and then split it. String q = linesParts[0]; String m = linesParts[1]; 
0
source

This is a solution to the problem that I would use. Tahir Hussein's above comment may be causing the problem

 import java.util.ArrayList; import java.util.Scanner; public class app { public static void main(String[] args) { // declare scanner Scanner scan = new Scanner(System.in); // what ever number you need, it could be calculated int numberOfInputLines = 3; // the list of the lines entered ArrayList<String[]> list = new<String[]> ArrayList(); // add each line to the list for (int i = 0; i < numberOfInputLines; i++) { // get entire line as a single string String input = scan.nextLine(); // split the line into tokens, and store the array in the array list String[] result = input.split("\\s"); list.add(result); } // iterate through each line for (int i = 0; i < list.size(); i++) { // iterate through the line for (int j = 0; j < list.get(i).length; j++) { if (isInteger(list.get(i)[j]) == true) { // do what you want if the input is an int //to show it works System.out.println("int: " + list.get(i)[j]); } else { // do what you want if a the token inputed is a string //to show it works System.out.println("String: " + list.get(i)[j]); } } } } // greasy way to check if is an int private static boolean isInteger(String s) { try { Integer.parseInt(s); } catch (NumberFormatException e) { return false; } catch (NullPointerException e) { return false; } // only got here if we didn't return false return true; } } 
0
source

Source: https://habr.com/ru/post/1260322/


All Articles