I made a class of student that has two different constructors one is the default constructor Student()
and the other is with arguments
Student(String studentID, String studentName, String research_intrest, String Topic, int[] course, boolean approval, String supervisorID)
the object student stores data from a file which my program will read
the program requires the user to enter some the data from the consule so the program will store the data if the id is not similar to the data I stored in Student class
after I checked if the id is not identical to the stored ones I started to store the new data from the consule by setting them to student object till I stopped to adding an array of courses because student class argument has an int[] course and I have to store a string values I am thinking to convert the string array to char array but I am not sure it will work
the file I am reading from student.txt contains:
1723, Asma, artificial intelligent, How the machine thinks: intelligent learning,1,1,1,1,1,1,true,00023#
1777,Rania, database, nan,1,1,1,0,0,0,false,0#
1003,Sara, network, nan,1,1,1,1,1,1,true,00013#
1743,Roaa, artificial intelligent, nan,1,1,1,0,0,0,false,0#
Here is the code that I have tried so far:
public static void main(String[] args)throws Exception {
File studentFile=new File("student.txt");
if (!studentFile.exists()) {
System.out.println("Sorry the file is not found!"); //checks if the file exists if no it terminates the program
System.exit(0);
}
String[]array=null;
Student stu=null;
Scanner stuRead=new Scanner(studentFile);
Scanner input=new Scanner(System.in);
while(stuRead.hasNext()){
array = stuRead.nextLine().split("#|,\\s|,");
stu=addStudent(array);
System.out.println(stu.toString());
}
System.out.println("Please enter the student information: ");
System.out.println("");
System.out.print("Student ID: ");
String id=input.next();
Student st=new Student(id);
if(st.check_std(id)){
System.out.print("Student Name: ");
String name=input.nextLine();
stu.setStudentName(name);
System.out.println("");
System.out.print("Research interest: ");
String res=input.nextLine();
stu.setResearch_intrest(res);
System.out.println("");
System.out.println("Courses: ");
String cor=input.nextLine();
String[] c=cor.split(",\\s");
stu.setCourse(c); //here the error occurs
System.out.println("");
System.out.println("The student is added!");
}else{
System.out.println("The student already exists! ");
}
public static Student addStudent(String[]arr)throws Exception{
String id=arr[0];
String name=arr[1];
String researchIntrst=arr[2];
String suggestTopic=arr[3];
String s="";
if(suggestTopic.equals("nan")){
s=" ";
}else{
s+=suggestTopic;
}
int[]courses=new int[6];
int num1=Integer.parseInt(arr[4]);
int num2=Integer.parseInt(arr[5]);
int num3=Integer.parseInt(arr[6]);
int num4=Integer.parseInt(arr[7]);
int num5=Integer.parseInt(arr[8]);
int num6=Integer.parseInt(arr[9]);
for (int i = 0; i <courses.length ; i++) {
courses[0]=num1;
courses[1]=num2;
courses[2]=num3;
courses[3]=num4;
courses[4]=num5;
courses[5]=num6;
}
String apprv=arr[10];
boolean approve=false;
if(arr[10].equals("true")){
approve=true;
}else{
approve=false;
}
String superId=arr[11];
return new Student(id,name,researchIntrst,s,courses, approve,superId);}
}
here is the Student class:
public class Student {
private String studentID;
private String studentName;
private String research_intrest;
private String Topic;
private int [] course;
private boolean approval; //true or false
private String supervisorID;
private Student next;
public Student(){
studentID = null;
studentName = null;
research_intrest = null;
Topic = null;
course = null;
approval = false;
supervisorID = null;
next=null;
}
public Student(String studentID, String studentName, String research_intrest, String Topic, int[] course, boolean approval, String supervisorID) {
this.studentID = studentID;
this.studentName = studentName;
this.research_intrest = research_intrest;
this.Topic = Topic;
this.course = course;
this.approval = approval;
this.supervisorID = supervisorID;
}
public Student(String studentID) {
this.studentID = studentID;
}
public boolean check_std(String id){
if(id.equals(this.studentID)){
return false;
}
return true;
}
public String getStudentID() {
return studentID;
}
public void setStudentID(String studentID) {
this.studentID = studentID;
}
public String getStudentName() {
return studentName;
}
public void setStudentName(String studentName) {
this.studentName = studentName;
}
public String getResearch_intrest() {
return research_intrest;
}
public void setResearch_intrest(String research_intrest) {
this.research_intrest = research_intrest;
}
public String getTopic() {
return Topic;
}
public void setTopic(String Topic) {
this.Topic = Topic;
}
public int[] getCourse() {
return course;
}
public void setCourse(int[] course) {
this.course = course;
}
public boolean isApproval() {
return approval;
}
public void setApproval(boolean approval) {
this.approval = approval;
}
public String getSupervisorID() {
return supervisorID;
}
public void setSupervisorID(String supervisorID) {
this.supervisorID = supervisorID;
}
public Student getNext() {
return next;
}
public void setNext(Student next) {
this.next = next;
}
@Override
public String toString() {
return "student{" + "studentID=" + studentID + ", studentName=" + studentName + ", research_intrest=" + research_intrest + ", Topic=" + Topic + ", course=" + Arrays.toString(course) + ", approval=" + approval + ", supervisorID=" + supervisorID + '}';
}
}
the expected output is like this:
Please enter the student information:
student ID:1333 Student Name: Areej research interest: software engineering courses: CPCS223, CPIS334, CPCS351, CPCS241, CPCS361, CPCS331
The student is added!
---------------------------------------------------
Please enter the student information:
student ID:1723
The student already exists!