-2

import java.util.*;

public class StudentRegister
{

    public static void main (String[] args)
    {
    
        String studentID = "0";
        String studentFname = "";
        String studentLname = "";
        String studentGrade = "0";
        int noOfStudents = 0;
        
        String[] studentArray;
        
        //Create the Registry
        
        Scanner input = new Scanner(System.in);
        System.out.println("Enter the Number of Students: ");
        noOfStudents = input.nextInt();
        
        studentArray = new String[100];
        
        for (int i=0; i<noOfStudents; i++)
        {
        
            System.out.println("Enter details for student number " + i + "\n");
            
            System.out.println("Enter Student ID: ");
            studentID = input.nextLine();
            
            System.out.println("Enter Student First Name: ");
            studentFname = input.nextLine();
            
            System.out.println("Enter Student Last Name: ");
            studentLname = input.nextLine();
            
            System.out.println("Enter Student Grade: ");
            studentGrade = input.nextLine();
            
            studentArray[i] = ("Student ID: " + studentID + "\n" + " First Name: " + studentFname + "\n" + " Last Name: " + studentLname + "\n" + " Student Grade: " + studentGrade + "\n");
        
        }
        
        //Print the Registry
        
        for (int j=0; j<=(studentArray.length)-1; j++)
        {
        
            System.out.println(studentArray[j]);
        
        }
    
    }

}

So what I need to do is to print all Student info to an array and then print it out. But when I try to do that I get two issues.

1.) StudentID and StudentFname doesn't come as two separate inputs, I get something like this:

Enter Student ID: 
Enter Student First Name: 

So I cannot enter ID I have to enter the name directly

2.) After the array outputs my inputs AND THEN it also displays a whole set of null values like this:.

null
null
null
null
null
null
null
null
null
null
null

I'm new to java so any help appreciated

  • If you don't want to print the null elements, stop your printing loop at `noOfStudents`. – khelwood May 12 '21 at 18:22
  • 1
    Either what @khelwood said or simply replace `studentArray = new String[100]` with `studentArray = new String[noOfStudents]`. – EricSchaefer May 12 '21 at 18:23

0 Answers0