0

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! 
Hassam Abdelillah
  • 2,246
  • 3
  • 16
  • 37
  • are you not getting correct output? – madhepurian Sep 27 '19 at 08:56
  • It's better to keep your courses array with raw data especially if the make sens in terms of content (e.g maths, stats, algebra ect..). Otherwise, you will have to create a correspendance between a course (string) and his "code" value as an int in your Student class – Hassam Abdelillah Sep 27 '19 at 08:56
  • Possible duplicate of [Converting a String array into an int Array in java](https://stackoverflow.com/questions/6881458/converting-a-string-array-into-an-int-array-in-java) – Alireza Bideli Sep 27 '19 at 08:59
  • Related: https://stackoverflow.com/questions/5585779/how-do-i-convert-a-string-to-an-int-in-java – mcalex Sep 27 '19 at 09:01
  • @deshi yea I stopped when the error occurred till now I did not get the output –  Sep 27 '19 at 09:01
  • @HassamAbdelillah the output has an array of subjects but I want to print it in student class toString() because I need int[]course to compare and print –  Sep 27 '19 at 09:03
  • @J.A Could you please share the error details. simple copy paste of console would be fine if possible – madhepurian Sep 27 '19 at 09:04
  • @mcalex the problem is that the consule will enter string letters and I can't convert them to int value –  Sep 27 '19 at 09:05
  • 1
    @deshi it says incompatible types String[] cannot be converted to int[] array in line: System.out.println("Courses: "); String cor=input.nextLine(); String[] c=cor.split(",\\s"); stu.setCourse(c); //here the error occurs –  Sep 27 '19 at 09:09

4 Answers4

1

You will have to convert it into int array before sending

String[] c=cor.split(",\\s");
int[] intArray = new int[c.length];
for(int i=0; i < c.length; i++){
        intArray[i] = Integer.parseInt(c[i])
}
        stu.setCourse(intArray)

;

midhun mathew
  • 333
  • 1
  • 9
  • thank you very much for helping but I thought that parseInt() is for string numbers, does it work for string letters? –  Sep 27 '19 at 11:46
  • by the way the user will enter course name as: CPCS990,CPCS001 –  Sep 27 '19 at 11:50
  • Then please modify Student.java. change private int[] course to private String [] course. check the modified answer – madhepurian Sep 27 '19 at 20:49
  • In that case @deshi is right. You will have to change int[] course to a String[] course if you know that they won't be string numbers – midhun mathew Sep 28 '19 at 05:07
0

the line where the error occurs:

 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);//incompatible types String[] cannot be converted to int[]  
              System.out.println("");
              System.out.println("The student is added!");
          }else{
              System.out.println("The student already exists! ");
          }
0

Here you can try as below:

//stu.setCourse(c); //here the error occurs  comment this
// add this
stu.setCourse(convertStringArryToIntArray(c))

declare this method:

static int[] convertStringArryToIntArray(String[] c){
        int[] courses = new int[c.length];
        int i = 0;
        for(String str : c) {
            courses[i++] = Integer.parseInt(str);
        }
        return courses;
    }

Here is the complete modification in your code :

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
              stu.setCourse(convertStringArryToIntArray(c))
              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);} 

        }

 static int[] convertStringArryToIntArray(String[] c){
        int[] courses = new int[c.length];
        int i = 0;
        for(String str : c) {
            courses[i++] = Integer.parseInt(str);
        }
        return courses;
    }

Another form of Student.java

class Student {
     private String studentID;
     private String studentName;
     private String research_intrest;
     private String Topic;
     private String [] 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, String[] 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 String[] getCourse() {
            return course;
        }

        public void setCourse(String[] 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 + '}';
        }


    }
madhepurian
  • 271
  • 1
  • 13
0

You can't assign a String[] to a int[], so you have to somehow convert yourself from String[] to int[].

The question is how these are related. For student 1723 the courses array contains only 1s, for student 1777 it contains three 1 and three 0.

How are these values related to CPCS223, CPIS334, CPCS351, CPCS241, CPCS361, CPCS331 that you are trying to add for student 1733?

  • Does a 1 in courses[0] mean that the student takes course CPCS223?
  • Does a 1 in courses[0] mean that the student takes course CPIS334?
  • Does a 1 in courses[0] mean that the student take any course? (so the number of 1s in the array means the number of courses?
Thomas Kläger
  • 17,754
  • 3
  • 23
  • 34