how can i pass an array of String (contains: , letters numbers and space) while the class i made requiers an int array?

array java
java create array of objects with constructor
write statements to declare the array and assign the values to the corresponding positions.
arrays processing
array of objects java
array of objects in java simple example program
processing add to array
string array processing

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! 

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)

;

How to use an array of objects and call them in methods in Java , How do you pass an array of objects in Java? I am new to java programming. My question is this I have a string array but when I am trying to convert it to an int array I keep getting java.lang.NumberFormatException My code is private void

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! ");
          }

Is it possible to create an array of objects in Java?, by default, the following code is a good example. Teams. Q&A for Work. Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information.

Day 5 -- Arrays, Conditionals, and Loops, How do you create an array of classes in Java? Java String - Programming Examples. Advertisements. Previous Page. Next Page . Learn how to play with strings in Java programming. Here are most commonly used

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 + '}';
        }


    }

Arrays in Java, The third phase simply outputs the contents of the array by displaying each element You display each string by passing s as the argument to the println() method. you with an easier way to do this—using the split() method in the String class. is an integer value that is a count of the maximum number of times the pattern  Top-down analyzers are very easy to write, provided that you allow parsing to be made via TWO generation points, instead of one: the first one converts and accumulates the tokens in an array as long as it is not able to be processable as a whole; this requiers a first parsing loop.

Ivor Horton's Beginning Java 2, This is done adaptively through use of a while loop that continues to execute (​or numbers in this case) that make up the line into the string vector container words. to a character array (or C-string) using the c.str function for the string class. of delimiters has been chosen to be a white space on line 36 of Listing 10.5. But  " Vimball Archiver by Charles E. Campbell, Jr., Ph.D. UseVimball finish ftplugin/ATP_files/LatexBox_common.vim [[[1 289 " Author: David Munger (latexbox vim plugin

Statistical Computing in C++ and R, There can be arrays of numbers, characters, sentences, boolean values, and so on. element is at position [1] because it is offset one place from the beginning. Because the chart has ten data points, inputting this data into a program requires either When an array is declared, the type of data it stores must be specified. This banner text can have markup.. web; books; video; audio; software; images; Toggle navigation

Arrays \ Processing.org, In Chapter 13.1, we define a Deck class that encapsulates an array of cards, and we One possibility is a String containing things like "Spade" for suits and "​Queen" for ranks. We mean to define a mapping between a sequence of numbers and the When you create a new class, the first step is to declare the instance  This banner text can have markup.. web; books; video; audio; software; images; Toggle navigation

Comments
  • are you not getting correct output?
  • 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
  • Possible duplicate of Converting a String array into an int Array in java
  • Related: stackoverflow.com/questions/5585779/…
  • @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
  • thank you very much for helping but I thought that parseInt() is for string numbers, does it work for string letters?
  • by the way the user will enter course name as: CPCS990,CPCS001
  • Then please modify Student.java. change private int[] course to private String [] course. check the modified answer
  • 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
  • @deshi the erroe occurs here