The issue is, when i use sc.nextLine in the code (in the line I marked) the for loop consumes the first input and then accepts the others. Using sc.next instead fixes this issues but I cannot accept a 2 word input which I need to. What can I do to fix both of these issues at the same time? (I am a beginner). Here is my current code:
import java.util.Scanner;
public class Marks {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("How many students are there? ");
int n = sc.nextInt();
String[] Names = new String[n];
int[] Marks = new int[n];
for (int i = 0; i < n; i++){
System.out.print("Enter name of student " + (i+1) + ": ");
Names[i] = sc.next(); // this line is the issue
System.out.print("Enter mark " + (i+1) +": ");
Marks[i] = sc.nextInt();
}
}
}
I have basically just tried variations on these 2 bits of code, I am a beginner so I do not know what else I can really do. Someone online said to just write a blank input to consume the new line character which I tried. The issue was that this made my Names array not actually store any values.