im trying to make a method where the user is asked to type in the student id and
ID: 671380 • Letter: I
Question
im trying to make a method where the user is asked to type in the student id and if the student id matches a students id in the StudentArray then that student will get added into the course arrray. i keep getting errors.
heres what i have:
public void enroll(){
int stuID = 0;
OUpeople ou = new OUpeople();
Scanner scan = new Scanner(System.in);
System.out.println("please enter a student id" + stuID);
stuID = scan.nextInt();
if(stuID == ou.StudentArray.get(0).IDnumber)
s.add(ou.StudentArray.get(0));
else
if(stuID == ou.StudentArray.get(1).IDnumber)
s.add(ou.StudentArray.get(1));
else
if(stuID== ou.StudentArray.get(2).IDnumber)
s.add(ou.StudentArray.get(2));
else
if(stuID == ou.StudentArray.get(3).IDnumber)
s.add(ou.StudentArray.get(3));
else
System.out.println("Does not match the students.");
}
Explanation / Answer
import java.util.Scanner;
import java.util.ArrayList;
public class Enroll {
public static void main(String[] args)
{
// Declaration of Student Array in ArrayList
ArrayList<Integer> studentWithIDs = new ArrayList<>();
// Add three students id's.
studentWithIDs.add(1);
studentWithIDs.add(2);
studentWithIDs.add(3);
int stuID = 0;
Scanner scan = new Scanner(System.in);
System.out.println("please enter a student id");
stuID = scan.nextInt();
//check a input id is existed in StudentWithIDs list
if(studentWithIDs.contains(stuID))
{
System.out.println("Student with ID "+stuID +" is existed");
}
else
{
System.out.println("Student with ID "+stuID +" is not existed");
}
}
}
If student id is existed , you need to create object to desired class and insert the student id into the method or array (as per your requirement) .
Note: In given input code i avoid if.. else logic and wrote boolean logic to achevie your requirement
If any other issues , please give reply back to me.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.