Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I\'m getting \'List Full\' when I try to add 2nd student. //Driver.java import j

ID: 3790872 • Letter: I

Question

I'm getting 'List Full' when I try to add 2nd student.

//Driver.java

import java.util.Scanner;

public class Driver {

   public static void main(String[] args) {
       new Driver();

   }
  
   public Driver() {
       Scanner input = new Scanner(System.in);
       Student[] newStudent = new Student[3];
      
       System.out.println("Select option below");
       System.out.println("");
      
       while(true) {
           System.out.println("1: Add Student");
           System.out.println("2: Display Student");
           System.out.println("3: Exit");
          
           int userChoice = input.nextInt();
          
           switch(userChoice) {
          
           case 1:
               addStudent(newStudent);
               break;
           case 2:
               displayStudent(newStudent);
               break;
           case 3:
               System.out.println("Exit");
               System.exit(3);
           }
       }
   }
   //Student[] newStudent = new Student[3];
   //Flower[] flowerPack = new Flower[25];

   private void addStudent(Student[] newStudent) {
       Scanner input = new Scanner(System.in);
       String FirstName, LastName;
       double GPA;
      
       System.out.print("Enter First Name: ");
       FirstName = input.nextLine();      
       System.out.print("Enter Last Name: ");
       LastName = input.nextLine();      
       System.out.print("Enter GPA: ");
       GPA = input.nextDouble();
      
       for(int i = 0; i < 3; i++) {
           if(newStudent[i] == null) {
               newStudent[i] = new Student(FirstName, LastName, GPA);
               break;              
              
           } else if(newStudent[i] != null) {
               System.out.println("List Full");
               break;
           }
       }
   }
      
   private void displayStudent(Student[] newStudent) {
       int x = 0;
       for(int i = 0; i < 3; i++) {
           if(newStudent[i] != null)
           System.out.println(newStudent[i].toString());
       else
           x++;
       }
      
           if(newStudent == null)
           System.out.println("no record");
   }
      
      
   }

//Student.java

import java.util.Scanner;

public class Student {

   private String FirstName;
   private String LastName;
   private double GPA;
  
   public Student(String FirstName, String LastName, double GPA) {
       this.FirstName = FirstName;
       this.LastName = LastName;
       this.GPA = GPA;
      
   }
  
   public Student() {
      
   }
  
   public String getFirstName() {
       return FirstName;
   }
  
   public void setFirstName(String FirstName) {
       FirstName = FirstName;
   }
  
   public String getLastName() {
       return LastName;
   }
  
   public void setLastName(String LastName) {
       LastName = LastName;
   }
  
   public double getGPA() {
       return GPA;
   }
  
   public void setGPA(double GPA) {
       GPA = GPA;
   }
  
   public String toString() {
       return "First Name: " + FirstName + " Last Name: " + LastName
               + " GPA: " + GPA;
   }
}

Explanation / Answer

Note

This application will adds only 3 students data.And Dislpay those 3 students data.

I just modified some code which rectifies your problem.Thank You

___________________

Driver.java

import java.util.Scanner;

public class Driver {

   public static void main(String[] args) {
   new Driver();
   }
  
   public Driver() {
   Scanner input = new Scanner(System.in);
   Student[] newStudent = new Student[3];
  
   System.out.println("Select option below");
   System.out.println("");
  
   while(true) {
   System.out.println("1: Add Student");
   System.out.println("2: Display Student");
   System.out.println("3: Exit");
  
   int userChoice = input.nextInt();
  
   switch(userChoice) {
  
   case 1:
   addStudent(newStudent);
   break;
   case 2:
   displayStudent(newStudent);
   break;
   case 3:
   System.out.println("Exit");
   System.exit(3);
   }
   }
   }

   private void addStudent(Student[] newStudent) {
   Scanner input = new Scanner(System.in);
   String FirstName, LastName;
   double GPA;
  
   System.out.print("Enter First Name: ");
   FirstName = input.nextLine();
   System.out.print("Enter Last Name: ");
   LastName = input.nextLine();
   System.out.print("Enter GPA: ");
   GPA = input.nextDouble();
  
   for(int i = 0; i < 3; i++) {
   if(newStudent[i] == null) {
   newStudent[i] = new Student(FirstName, LastName, GPA);
   if(newStudent[2] != null)
       System.out.println("List Full");
   break;
  
   }
   }
   }
  
   private void displayStudent(Student[] newStudent) {
   int x = 0;
   for(int i = 0; i < 3; i++) {
   if(newStudent[i] != null)
   System.out.println(newStudent[i].toString());
   else if(newStudent[0] == null)
   {
      System.out.println("no record");
      break;
   }
   }
  

   }

}

____________________

Student.java

public class Student {
   private String FirstName;
   private String LastName;
   private double GPA;
  
   public Student(String FirstName, String LastName, double GPA) {
   this.FirstName = FirstName;
   this.LastName = LastName;
   this.GPA = GPA;
  
   }
  
   public Student() {
  
   }
  
   public String getFirstName() {
   return FirstName;
   }
  
   public void setFirstName(String FirstName) {
   FirstName = FirstName;
   }
  
   public String getLastName() {
   return LastName;
   }
  
   public void setLastName(String LastName) {
   LastName = LastName;
   }
  
   public double getGPA() {
   return GPA;
   }
  
   public void setGPA(double GPA) {
   GPA = GPA;
   }
  
   public String toString() {
   return "First Name: " + FirstName + " Last Name: " + LastName
   + " GPA: " + GPA;
   }
}

_______________________

Output:

Select option below

1: Add Student
2: Display Student
3: Exit
1
Enter First Name: Kane
Enter Last Name: Williams
Enter GPA: 8.8
1: Add Student
2: Display Student
3: Exit
1
Enter First Name: Ryan
Enter Last Name: Harris
Enter GPA: 7.8
1: Add Student
2: Display Student
3: Exit
1
Enter First Name: Paul
Enter Last Name: Thomson
Enter GPA: 6.5
List Full
1: Add Student
2: Display Student
3: Exit
2
First Name: Kane
Last Name: Williams
GPA: 8.8
First Name: Ryan
Last Name: Harris
GPA: 7.8
First Name: Paul
Last Name: Thomson
GPA: 6.5
1: Add Student
2: Display Student
3: Exit
3
Exit

____________thank you

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote