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.