I'm creating a program that perform store data and retrieve data from the arraylist. I have no clue make the program find the data in Arraylist and display the data that user is looking for. If data is not in the list, the program would print the line say that whatever user enter is not in the list. There is the code I create:
constructor:
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;
}
@Override
public String toString()
{
return String.format("Name: %s %s. Major: %s. GPA: %.1f sNumber: %d\n", fName, lName, major, gpa, sNumber);
}
}
There is the class that perform the task:
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 ArrayList <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':
record.add(addStudent());
pause();
break;
case '2':
removeStudent(record);
pause();
break;
case '3':
pause();
break;
case '4':
displayAllStudent(record);
pause();
break;
case '5':
break;
default :
System.out.println("Invalid Selection");
}//end switch
}while( selection != '5');
}
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.nextDouble();
System.out.println("S number: ");
int sNumber = keyBd.nextInt();
return new StudentArrayList(fName, lName, major, gpa, sNumber);
}
public static void removeStudent(ArrayList <StudentArrayList> record)
{
Scanner keyBd = new Scanner( System.in );
displayAllStudent(record);
System.out.println("Enter the number you want to remove: ");
int number = keyBd.nextInt();
record.remove(number);
}
public static void displayAllStudent(ArrayList <StudentArrayList> record)
{
int d =0;
for( StudentArrayList s: record)
{
System.out.print(d+". "+s.toString());
d++;
}
}
public static StudentArrayList findStudnet(ArrayList <StudentArrayList> record)
{
Scanner keyBd = new Scanner( System.in );
System.out.println("Please enter the Student Name: ");
String k = keyBd.nextLine();
}
}
I want the user type in the student name, and the program would search in arraylist and if the name is in the list, it display the name and other information. If not, that display whatever user is not in the list. I do thought about use "contains" to check to see if the data is in the list or not. But if it return true, I don't know how to make it display the other information about that student.
How do I make the program search in the arraylist and display the information is the data is in the list?
Thank You so much