-1

I would like to accept 3 integers input from user. How can I ignore anything after the first integer in a line? For example, when I input 1 2 3 or 1 abc 3, the int test[] variable will only accept 1 and the remaining part of the line will be ignored.

Goal: Ignore(or clear) anything after the first integer (starting from the first white space), including whatever whitespace or char. If possible, it will be great to have warning to user to warn user that whitespace cannot be enter in the input. I am not finding solution for reading multiple integers from the same line.

This is what I have:

private final int[] test = new int[4]; // I just use 1-3
Scanner input = new Scanner(System.in);
System.out.print("1st Integer: ");
test[1] = input.nextInt();
System.out.print("2nd Integer: ");
test[2] = input.nextInt();
System.out.print("3rd Integer: ");
test[3] = input.nextInt();

For the above code, if I simply input an integer e.g. 1 enter 2 enter 3 enter, it is okay. But when I input something like 1 2 3 (with white space between 3 integers), it simply output something like this:

1st Integer: 1 2 3
2nd Integer: 3rd Integer: 

I want my code to be like this:

1st Integer: 1
2nd Integer: 2
3rd Integer: 3
PzrrL
  • 43
  • 9
  • Read the `Scanner` documentation and see if there is a method which can read the remaining stuff of a line. If so, then use it. – Tom Sep 20 '16 at 08:42
  • use `Scanner.readLine()` instead and implement your validation from there. – Samuel Kok Sep 20 '16 at 08:44
  • Possible duplicate of [How to read integer value from the standard input in Java](http://stackoverflow.com/questions/2506077/how-to-read-integer-value-from-the-standard-input-in-java) – Julien Lopez Sep 20 '16 at 08:58

3 Answers3

0

with this simple way, you can get numbers into string array and you can convert them to integer.

Scanner scanner = new Scanner(System.in);
String[] array = scanner.nextLine().split(" ");
int[] intArray = new int[array.length];

for( int i = 0; i < array.length; i++ ){
   intArray[i] = Integer.parseInt(array[i]);
}

And you can find lots of good answers here; Read integers separated with whitespace into int[] array

Community
  • 1
  • 1
  • I only need the first integer for a line of input, the rest of the input should be ignore(or clear). User will need to input new value in next appeared prompt. That's why I need to ignore everything after the first white space. – PzrrL Sep 20 '16 at 08:59
0

This will work fine.

private final int[] test = new int[4]; // I just use 1-3
Scanner input = new Scanner(System.in);
System.out.print("1st Integer: ");
test[1] = input.nextInt();
input.nextLine();

System.out.print("2nd Integer: ");
test[2] = input.nextInt();
input.nextLine();

System.out.print("3rd Integer: ");
test[3] = input.nextInt();
input.nextLine();
Athul Sukumaran
  • 310
  • 3
  • 14
  • It works too. But what actually `input.nextLine();` did? – PzrrL Sep 20 '16 at 09:44
  • input.nextLine() advances this scanner past the current line and returns the input that was skipped. Further reading can be done at https://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html#nextLine() – Athul Sukumaran Sep 20 '16 at 09:50
0

Hey use this code this will generate your required output,

    int[] tes = new int[4];// I just use 1-3
    System.out.println("Warning : Whitespace cannot be enter in the input");
    Scanner input = new Scanner(System.in);
    System.out.println("1st Integer: ");
    tes[1] = Integer.parseInt(input.nextLine().replaceAll("\\s.+", ""));
    System.out.println("2nd Integer: ");
    tes[2] = Integer.parseInt(input.nextLine().replaceAll("\\s.+", ""));
    System.out.println("3rd Integer: ");
    tes[3] = Integer.parseInt(input.nextLine().replaceAll("\\s.+", ""));
    System.out.println("Output : "+tes[2]); 

Output :

Warning : Whitespace cannot be enter in the input
1st Integer: 
456 ddf 477
2nd Integer: 
33 dfgf ddddds rrsr 566
3rd Integer: 
2 4 4 4
Output : 33

Working:

  • Initially it read a single line as a String.
  • Then remove all the characters after the white space using regex.
  • Finally the string is converted to integer.

Hope this helps, Please leave a comment below in case of any clarifications.

Karthikeyan KR
  • 970
  • 1
  • 14
  • 32