I need help with my program which is not yet finished. I think there is a problem with my coding on searchArray(String key) method.
My Main Class:
import java.util.Scanner;
public class StudentArray {
static Scanner sc = new Scanner(System.in);
static Student[] stud = new Student[100];
static int count = 0;
public static void main(String[] args) {
while (true) {
int select;
System.out.println("1. Add Student Record");
System.out.println("2. View Student Record");
System.out.println("3. Update Student Record");
System.out.println("4. Delete Student Record");
System.out.println("0. Exit");
select = sc.nextInt();
switch (select) {
case 1:
addStud();
break;
case 2:
viewStud();
break;
case 3:
break;
case 4:
break;
case 0:
return;
default:
System.out.println("Invalid Option");
}
}
}
public static void addStud() {
int numID, year;
String userName, course;
int addMore;
do {
System.out.println("1. Enter Student ID: ");
numID = sc.nextInt();
sc.nextLine();
System.out.println("2. Enter Student Name");
userName = sc.nextLine();
System.out.println("3. Enter Student Course");
course = sc.nextLine();
System.out.println("4. Enter Student Year");
year = sc.nextInt();
stud[count] = new Student(numID, year, userName, course);
++count;
System.out.println("To add another Student Record Press 1");
addMore = sc.nextInt();
} while (addMore == 1);
}
public static void viewStud() {
int select;
System.out.println("1. View Record by ID number: " );
System.out.println("2. View Record by Course: " );
System.out.println("3. View Record by Course and Year: " );
System.out.println("4. View All: " );
select = sc.nextInt();
while (select < 1 || select > 4){
System.out.println("Please enter values 1-4 only: ");
select = sc.nextInt();
}
switch (select){
case 1:
int view1;
System.out.println("Please enter Student ID Number: ");
view1 = sc.nextInt();
sc.nextLine();
searchArray(view1);
break;
case 2:
String view2;
System.out.println("Please enter Student Course: ");
view2 = sc.nextLine();
searchArray(view2);
break;
case 3:
break;
case 4:
for (Student element : stud) {
if (null != element) {
System.out.println("1. Student ID: " + element.getNumID());
System.out.println("2. Student Name: " + element.getUserName());
System.out.println("3. Student Course: " + element.getCourse());
System.out.println("4. Student Year: " + element.getYear() + "\n");
}
}
break;
}
}
public static void searchArray(int key){
boolean isExist = false;
int temp = 0;
for(int x = 0; x < count; ++x){
if(key == stud[x].getNumID()){
temp = x;
isExist = true;
break;
}
}
if(isExist){
System.out.println("1. Student ID: " + stud[temp].getNumID());
System.out.println("2. Student Name: " + stud[temp].getUserName());
System.out.println("3. Student Course: " + stud[temp].getCourse());
System.out.println("4. Student Year: " + stud[temp].getYear() +"\n");
}
else
System.out.println("The Student ID: " +key+ " is invalid");
}
public static void searchArray(String key){
boolean isExist = false;
for(int x = 0; x < count; ++x){
if(key.equalsIgnoreCase(stud[x].getCourse())){
System.out.println("1. Student ID: " + stud[x].getNumID());
System.out.println("2. Student Name: " + stud[x].getUserName());
System.out.println("3. Student Course: " + stud[x].getCourse());
System.out.println("4. Student Year: " + stud[x].getYear() +"\n");
isExist = true;
}
}
if(isExist == false){
System.out.println("The Student ID: " +key+ " is invalid");
}
}
}
My Student Class:
public class Student {
private int numID, year;
private String userName, course;
public Student(int numID, int year, String userName, String course) {
this.numID = numID;
this.year = year;
this.userName = userName;
this.course = course;
}
public int getNumID() {
return numID;
}
public void setNumID(int numID) {
this.numID = numID;
}
public int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getCourse() {
return course;
}
public void setCourse(String course) {
this.course = course;
}
}
Error when asked Please enter Student Course: it will always show The Student ID: is invalid
Aucun commentaire:
Enregistrer un commentaire