1

I'm sorry if a similar question has been posted, I just couldn't find it. Here is my code:

import java.util.*;

public class InputArraysNextLine
{
    static Scanner q = new Scanner(System.in);

    public static void main(String[] args)
    {
        System.out.print("n = ");
        int n = q.nextInt();

        int[] a = new int[n];
        String[] b = new String[n];

        for (int i = 0; i < n; i++)
        {
            System.out.print("Enter student name: ");
            b[i] = q.nextLine();

            q.next();
            System.out.print("Enter student number: ");

            a[i] = q.nextInt();
        }


        for (int i : a)
            System.out.print(i + " ");
        System.out.println();
        for (String j : b)
            System.out.print(j + " ");
    }
}

I intended to display on the console both arrays a and b, but only a is shown. I believe the nextLine() method has something to do with this; even though I can actually input something when the console displays "Enter student name:", the program doesn't store the input into the b Strings.

Iskar Jarak
  • 5,136
  • 4
  • 38
  • 60
user1762169
  • 29
  • 2
  • 4

3 Answers3

1

Scanner.nextInt() does not consume the newline, so you need to call nextLine() after grabbing the int to skip over it. You also don't need to call next() after calling nextLine().

for (int i = 0; i < n; i++)
{
    System.out.print("Enter student name: ");
    b[i] = q.nextLine().trim(); // b is the whole line, trim() removes leading and trailing whitespace

    System.out.print("Enter student number: ");
    a[i] = q.nextInt();
    q.nextLine(); // nextInt() does not 
}

You should also be aware that just running a loop that expects to be able to grab a number of things is also a bit dodgy - you really should check for things before you try to grab them to avoid crashing (e.g. if the input stream terminates early). Checking can be done using the has functions - hasNext() for tokens, hasNextLine() for lines, hasNextInt() for integers, and so on.

Iskar Jarak
  • 5,136
  • 4
  • 38
  • 60
0

You should try and replace:

b[i] = q.nextLine();
q.next();

with:

b[i] = q.next();

Since q.nextLine() returns the input that was skipped in the stream, not the case here.

dan
  • 13,132
  • 3
  • 38
  • 49
  • Down voter, please explain your action and help us learn too. – dan Nov 13 '12 at 21:23
  • Oops, sorry about that. Didn't see my own q.next(); BTW thanks very much. – user1762169 Nov 13 '12 at 21:24
  • ...um, actually nextLine returns the next... Line... I think – tckmn Nov 13 '12 at 21:24
  • @PicklishDoorknob You are rigth, I was trying to say that in his case he is not having a line in the stream at that moment. Will update my answer. – dan Nov 13 '12 at 21:25
  • `Scanner.nextLine()` does not return if there is a line in the stream. That is `Scanner.hasNextLine()`. `next()` gets the next token and moves past it, `nextLine` gets the next line and moves past it. – Iskar Jarak Nov 13 '12 at 21:26
  • @dan By default `next()` will not give the desired behaviour on something like "Iskar Jarak", either - it would return "Iskar". The issue here is that he needs to skip the newline remaining after he uses `nextInt()`. – Iskar Jarak Nov 13 '12 at 21:31
  • @IskarJarak You are right, but the OP didn't mentioned that he needs the full name or a surname, but probably he needs the full name. – dan Nov 13 '12 at 21:35
  • here, the downvoter seems to be gone so I'll upvote to balance that out :P – tckmn Nov 13 '12 at 22:32
0

q.nextLine javadoc states "Advances this scanner past the current line and returns the input that was skipped." Your functionality to get the input that user enters.

So you need to reverse what is captured in b[i]

b[i] = q.nextLine(); q.next()

to

b[i] = q.next(); q.nextLine();

CodeDreamer
  • 444
  • 2
  • 8