Java 我如何传递一个字符串数组(包含:、字母、数字和空格),而我创建的类是一个int数组?

Java 我如何传递一个字符串数组(包含:、字母、数字和空格),而我创建的类是一个int数组?,java,arrays,netbeans,netbeans-8,Java,Arrays,Netbeans,Netbeans 8,我制作了一个有两个不同构造函数的学生类,一个是默认构造函数学生 另一个是争论 StudentString studentID、String studentName、String research\u intrest、String主题、int[]课程、布尔批准、String supervisorID 对象student存储程序将读取的文件中的数据 该程序要求用户从领事馆输入一些数据,因此如果id与我在学生课堂上存储的数据不相似,该程序将存储数据 在检查id是否与存储的id不一致后,我开始存储来自领

我制作了一个有两个不同构造函数的学生类,一个是默认构造函数学生 另一个是争论 StudentString studentID、String studentName、String research\u intrest、String主题、int[]课程、布尔批准、String supervisorID 对象student存储程序将读取的文件中的数据 该程序要求用户从领事馆输入一些数据,因此如果id与我在学生课堂上存储的数据不相似,该程序将存储数据 在检查id是否与存储的id不一致后,我开始存储来自领事馆的新数据,方法是将它们设置为student对象,直到我停止添加课程数组,因为student类参数具有int[]当然,我必须存储一个字符串值,我正在考虑将字符串数组转换为字符数组,但我不确定它是否能工作

我正在从student.txt读取的文件包含:

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#
以下是我迄今为止尝试过的代码:

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

        }
预期输出如下所示:

    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! 

发生错误的行:

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

在这里,您可以尝试以下操作:

//stu.setCourse(c); //here the error occurs  comment this
// add this
stu.setCourse(convertStringArryToIntArray(c))
声明此方法:

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;
    }
以下是对代码的完整修改:

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;
    }
您不能将字符串[]分配给int[],因此您必须以某种方式将自己从字符串[]转换为int[]

问题是这些是如何关联的。对于学生1723,课程数组仅包含1,对于学生1777,它包含三个1和三个0

这些值与您试图为学生1733添加的CPCS223、CPIS334、CPCS351、CPCS241、CPCS361、CPCS331有何关系

课程[0]中的1是否意味着该学生学习CPCS223课程? 课程[0]中的1是否表示该学生选修CPIS334课程? 课程[0]中的1是否表示该学生选修任何课程?那么数组中1的数量意味着课程的数量?
在发送之前,必须将其转换为int数组

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)

)

你没有得到正确的输出吗?最好用原始数据保存你的课程数组,特别是如果课程内容(如数学、统计、代数等)合理的话。。。否则,您将必须在课程字符串和其代码值之间创建一个对应关系,作为学生类中的int。相关:@deshi的可能重复项。它表示不兼容类型字符串[]无法转换为int[]数组,行:System.out.printlncources:;字符串cor=input.nextLine;字符串[]c=cor.split,\\s;斯图·塞特库塞克//这里是错误occurs@deshi此处出现错误非常感谢您的帮助,但我认为parseInt表示字符串数字,它是否适用于字符串字母?顺便说一下,用户将输入课程名称为:CPCS990,cpcs001,然后请修改Student.java。将私有int[]课程更改为私有字符串[]课程。检查修改后的答案,如果@deshi是正确的。如果您知道int[]课程不是字符串编号,则必须将其更改为String[]课程
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 + '}';
        }


    }
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)