In: Computer Science
Does anyone know how to find if a certain String is available in an ArrayList?
I was asked by my instructor to make a certain program about Student Record
In the area where I need to add some student in the ArrayList it won't add if there is the same name in the ArrayList
at first I used the equals method and contains method but later I found it was prohibited to use such method.
Is there a way to find if a String is Available in the ArrayList without using the method
Please check on else if(response==2)
import java.util.*;
public class performanceTask {
public static void main(String[] args) {
int response;
ArrayList<String>
listOfStudent = new ArrayList<String>();
Scanner aScanner = new
Scanner(System.in);
do {
System.out.println("[Selection Bar]");
System.out.println("[1] List of Students.");
System.out.println("[2] Add Student.");
System.out.println("[3] Edit Student.");
System.out.println("[4] Delete Student.");
System.out.println("[5] Clear list of Students.");
System.out.println("[6] Exit Program.");
System.out.println("--------------------------");
System.out.println("Select an option: ");
response =
aScanner.nextInt();
if(response ==
1) {
int size = listOfStudent.size();
if(size == 0) {
System.out.println("No
Records Found!\n");
} else {
Collections.sort(listOfStudent);
for(int i = 0;
i<listOfStudent.size(); i++) {
System.out.println(i + ". " + "[" + listOfStudent.get(i) +
"]");
}
System.out.println("\n");
}
}else if
(response == 2) {
System.out.println("Enter Name of a Student:
");
String addStudent = aScanner.next();
if (listOfStudent.contains(addStudent)) {
System.out.println(addStudent
+" already existed! \n");
} else {
System.out.println("Do you
want to save " + addStudent + "? [y/n]: ");
String saveIt =
aScanner.next();
if(saveIt.equals("y") || saveIt.equals("Y") ) {
listOfStudent.add(addStudent);
System.out.println(addStudent +" has been
successfully added! \n ");
}else if
(saveIt.equals("n") || saveIt.equals("n")) {
System.out.println("\n");
continue;
}else
{
System.out.println("It must be y or n
only!");
}
}
Hi,
Please find the answer below:
To check a duplicate student in the ArrayList without using any inbuilt methods, you need to iterate over the list and see if the student name repeats.
The simple way is to set one flag if the student name is present in the Array list:
boolean foundFlag=false;
// Iterate over the array list
for(String name: listOfStudent)
{
if(name.equalsIgnoreCase(addStudent)){
foundFlag=true;
break;
}
}
if (foundFlag) {
System.out.println(addStudent +" already existed! \n");
-------------------------------------------------------------------------------
Remove the contains code and add the above loop to check existing student name in the
else if(response==2) block.
Other improvements in the code:
Coding standard: Always start the class name with CamelCase letter. For example
PerformanceTask
---------------------------------------------------------
Sample Output:
[Selection Bar]
[1] List of Students.
[2] Add Student.
[3] Edit Student.
[4] Delete Student.
[5] Clear list of Students.
[6] Exit Program.
--------------------------
Select an option:
2
Enter Name of a Student:
John
Do you want to save John? [y/n]:
y
John has been successfully added!
[Selection Bar]
[1] List of Students.
[2] Add Student.
[3] Edit Student.
[4] Delete Student.
[5] Clear list of Students.
[6] Exit Program.
--------------------------
Select an option:
1
0. [John]
[Selection Bar]
[1] List of Students.
[2] Add Student.
[3] Edit Student.
[4] Delete Student.
[5] Clear list of Students.
[6] Exit Program.
--------------------------
Select an option:
2
Enter Name of a Student:
Sarah
Do you want to save Sarah? [y/n]:
y
Sarah has been successfully added!
[Selection Bar]
[1] List of Students.
[2] Add Student.
[3] Edit Student.
[4] Delete Student.
[5] Clear list of Students.
[6] Exit Program.
--------------------------
Select an option:
1
0. [John]
1. [Sarah]
[Selection Bar]
[1] List of Students.
[2] Add Student.
[3] Edit Student.
[4] Delete Student.
[5] Clear list of Students.
[6] Exit Program.
--------------------------
Select an option:
2
Enter Name of a Student:
John
John already existed!
[Selection Bar]
[1] List of Students.
[2] Add Student.
[3] Edit Student.
[4] Delete Student.
[5] Clear list of Students.
[6] Exit Program.
--------------------------
Select an option:
Screenshot:
Let me know if you need further help on this.
Hope this helps.