java - 当我创建的类需要一个 int 数组时,我如何传递一个 String 数组(包含 : , 字母、数字和空格)?

标签 java arrays netbeans netbeans-8

我创建了一个学生类,它有两个不同的构造函数,一个是默认构造函数Student() 另一个是有参数的 学生(字符串studentID,字符串studentName,字符串research_intrest,字符串主题,int []类(class), boolean 批准,字符串supervisorID) 对象 Student 存储我的程序将读取的文件中的数据 该程序要求用户从console输入一些数据,因此如果id与我在Student类中存储的数据不相似,程序将存储该数据 在检查 id 是否与存储的 ID 不同后,我开始通过将领事馆的新数据设置为学生对象来存储新数据,直到我停止添加类(class)数组,因为学生类参数有一个 int[] 类(class),并且我必须存储字符串值我正在考虑将字符串数组转换为字符数组,但我不确定它是否有效

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

        }

这是学生类(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 + '}';
    }


}

预期的输出是这样的:

    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! 

最佳答案

在发送之前您必须将其转换为 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)

;

关于java - 当我创建的类需要一个 int 数组时,我如何传递一个 String 数组(包含 : , 字母、数字和空格)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58130845/

相关文章:

java - 如何为 Netbeans 配置 Grails?

java - 解析嵌套 avro 文件

java - 检查电子邮件是否在数据库中,否则将用户名和密码存储在数据库中

java - 企业应用程序 build-impl.xml 抛出错误

java - NetBeans Matisse 中的表单加载错误

java - 数组不断将列表循环中的相同值添加到所有索引

java - Mallet:OutOfMemoryError:Java 堆空间

java - 使用 maven-assembly-plugin 包含所需的驱动程序时未找到合适的驱动程序

javascript正则表达式来获取始终跟随城市和逗号的2个字符的状态

java - 从 Java 方法返回数组