-2

I'm trying to input a certain number of strings but the last input gets a new line as input automatically. I need to remove the input buffer plz help

Scanner input = new Scanner(System.in);
int n = input.nextInt();     //n is my input number
String[] arr = new String[n];
int i,j;
for(i=0;i<n;i++)
{
    arr[i] = input.nextLine();
}

If I enter n=5 as input and when i print the array my last input string goes missing and is replaced by a new line character

Dici
  • 22,025
  • 5
  • 34
  • 74
Ishaan Kanwar
  • 164
  • 1
  • 1
  • 10
  • Just to ask something, Is there any other way to input a string as I'm interested in only entering strings without spaces. I don't want to use nextLine() function if possible – Ishaan Kanwar Sep 09 '18 at 11:09

3 Answers3

0

Just consume the new line character after the first int:

Scanner input = new Scanner(System.in);
int n = input.nextInt();     //n is my input number
input.nextLine();

String[] arr = new String[n];
int i,j;
for(i=0;i<n;i++)
{
    arr[i] = input.nextLine();
}

Demo to make sure we agree on what the code should be doing:

public static void main(String[] args) {
    String text =
            "5\n" +
            "line 1\n" +
            "line 2\n" +
            "line 3\n" +
            "line 4\n" +
            "line 5\n" +
            "line 6\n";

    Scanner input = new Scanner(new ByteArrayInputStream(text.getBytes()));

    int n = input.nextInt();     //n is my input number
    input.nextLine();

    String[] arr = new String[n];
    for(int i = 0; i < n; i++) {
        arr[i] = input.nextLine();
    }
    System.out.println(Arrays.asList(arr)); // [line 1, line 2, line 3, line 4, line 5]
}
Dici
  • 22,025
  • 5
  • 34
  • 74
0

The call to nextInt() won't consume the new line you enter in the console, so you can consume the extra newline by using Scanner.nextLine() instead of nextInt() and converting it to an int.

public static void main(String[] args) {
       Scanner input = new Scanner(System.in);
       //Convert the int to a String
       int n = Integer.parseInt(input.nextLine());//n is my input number
       String[] arr = new String[n];
       int i, j;
       for (i = 0; i < n; i++) {
           arr[i] = input.nextLine();
       }
       for (String s : arr) {
           System.out.println(s);
       }
 }
Fullstack Guy
  • 14,425
  • 3
  • 18
  • 35
  • This solved my problem, Im new to java so could you explain in more detail this line `int n = Integer.parseInt(input.nextLine());` – Ishaan Kanwar Sep 09 '18 at 10:55
  • As others said in their answer, `nextInt()` doesn't consume the first newline character, which is why your first line was empty. Check out the demo snippet in my answer and try removing the first `nextLine` and see what happens. – Dici Sep 09 '18 at 10:56
  • `input.nextLine()` is returning a `String` which you need to convert to an `int` to use in the for loop. The conversion of an `String` to an `int` is done through `Integer.parseInt(String)`. When you enter the number in the console and hit enter, the extra newline is not consumed by the `nextInt()` by default so I used the `nextLine()` which will by default skip one new line after the number. – Fullstack Guy Sep 09 '18 at 10:58
0

Scanner.nextInt() only consumes the integer that is given during input. So, if you provide an integer as input say 5 and press enter, then Scanner.nextInt() will only consume 5 and the enter/return is passed onto nextLine. This is why you think, that you are able to input 1 less.

So, after Scanner.nextInt(), you should add a Scanner.nextLine() just to consume the enter/return key

Ashishkumar Singh
  • 3,402
  • 1
  • 18
  • 32