Not sure where I've messed up?
I'm trying to do my final project for a class and I can't seem to get part of my code to work properly. The first loop only prompts for data the first time through; after that it skips straight into the nested loop. It's supposed to ask for 5 names and 5 scores for each name but I can only input 1 name.
public class StudentAverageTestScore
public static void main(String asdf)
Scanner sc = new Scanner(System.in);
String students = new String;
int scores = new int;
for(int i=0; i<students.length; i++)
out.println("Enter the name of student " + (i+1));
students[i] = sc.nextLine();
for(int j=0; j<scores.length; j++)
out.println("Enter student's score for test " + (j+1));
scores[i][j] = sc.nextInt();
if(scores[i][j]<0 || scores[i][j]>100)
out.println("Enter a valid test score.");
Re: Not sure where I've messed up?
sc.nextInt() leaves a linefeed in the input buffer which is then taken as input for the next sc.nextLine() call. Flush the buffer and throw it away with a sc.nextLine() call after the nested for loop.