I have been working a program that allow the user to perform the following task, add student, display student, delete student, and find student. I need help with add student name, gpa..etc to the arraylist. But I keep getting either compiler messages or other error message. I have been trying many different way to make it, but it all failed.
This is the constructors I create(I don't get any error here):
import java.util.Scanner;
public class StudentArrayList
{
static int count;
String fName;
String lName;
String major;
double gpa;
int sNumber;
public StudentArrayList()
{
}
public StudentArrayList(String fN, String lN, String mj, double ga, int sN)
{
fName = fN;
lName = lN;
major = mj;
gpa = ga;
sNumber = sN;
count++;
}
public void setFName (String fN)
{
fName = fN;
}
public void setlName (String lN)
{
lName = lN;
}
public void setmajor (String mj)
{
major = mj;
}
public void setGa (double ga)
{
gpa = ga;
}
public void setSnumber (int sN)
{
sNumber = sN;
}
public void setCount (int ct)
{
count = ct;
}
public String getFname()
{
return fName;
}
public String getLname()
{
return lName;
}
public String getMajor()
{
return major;
}
public double getGpa()
{
return gpa;
}
public int getSNumber()
{
return sNumber;
}
public int getCount()
{
return count;
}
}
Here is class I create to perform the task, but keep getting errors:
import java.util.Scanner;
import java.util.ArrayList;
public class StudentList
{
public static void pause()
{
try
{
System.out.print("Press <Enter> to continue...");
System.in.read();
}
catch (Exception e)
{
System.err.printf("Error %s%c\n",e.getMessage(),7);
}
}
public static void main(String[] args)
{
ArrayList <StudentArrayList> record = new record <StudentArrayList>();
Scanner keyBd = new Scanner( System.in );
char selection;
do{
System.out.println("\n--------------");
System.out.println("1. Add Student");
System.out.println("2. Remove Studnet");
System.out.println("3. Find Studnet");
System.out.println("4. Display All Studnet");
System.out.println("5. Exit\n");
System.out.print ("Selection: ");
selection = keyBd.next().charAt(0);
switch (selection){
case '1':
Scanner keybd = new Scanner( System.in );
System.out.println("First Name: ");
String fName = keybd.nextLine();
System.out.println("Last Name: ");
String lName = keybd.nextLine();
System.out.println("Major: ");
String major = keybd.nextLine();
System.out.println("gpa: ");
double gpa = keybd.nextDouble();
System.out.println("S number: ");
int sNumber = keybd.nextInt();
String s = String.format("Studnet Name: %s %s. Major: %s. GPA: SNumber: ", fName, lName, major);
record.add(s);
pause();
break;
case '2':
pause();
break;
case '3':
pause();
break;
case '4':
pause();
break;
case '5':
break;
default :
System.out.println("Invalid Selection");
}//end switch
}while( selection != '6');
/*public static StudentArrayList addStudent()
{
Scanner keyBd = new Scanner( System.in );
System.out.println("First Name: ");
String fName = keyBd.nextLine();
System.out.println("Last Name: ");
String lName = keyBd.nextLine();
System.out.println("Major: ");
String major = keyBd.nextLine();
System.out.println("gpa: ");
double gpa = keyBd.nextLine();
System.out.println("S number: ");
int sNumber = keyBd.nextLine();
return new StudentArrayList(String fN, String lN, String mj, double ga, int sN);
}*/
}
}
at Line 75-90 is the method I try to create to add the name, but for some reasons I'm getting compiler errors. So I comment it out and try to use string.format to store the information and add it to the arraylist which is line 40-52. I still keep gettign compiler error.
Is there other way to do it or should I just change some parts of my code and everything will just work fine? Thank you so much