Im getting List Full when I try to add 2nd student.Driver..pdf
1. 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);
}
}
2. }
//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++;
}
3. 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;
}
4. 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;
}
}
Solution
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");
5. 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++) {
7. }
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;
}
}
_______________________
8. 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