简体   繁体   English

Java:通过JOptionPane对话框将数据存储在对象数组中

[英]Java : Storing Data in Array Of Object from JOptionPane Dialog

I need to know how I can insert data inside objects inside an array of objects using my already made Set methods. 我需要知道如何使用已经完成的Set方法将数据插入对象数组中的对象内。

i need to know how should i do it through user , i mean JOptionPane input dialog 我需要知道如何通过用户执行操作,我的意思是JOptionPane输入对话框

student[] s = new student[5];

for (int i=1 ; i <= s.length ;i++) {
    s[i] = new student(i,"AAA","Ecommerce",0.0);
}

for (int i=1; i<=s.length;i++) {
    name = JOptionPane.showInputDialog("Please Write Name for student n " + i);
    major = JOptionPane.showInputDialog("Please Write Major for student n " + i);
    gpa = Double.parseDouble(JOptionPane.showInputDialog("Please Write GPA for student n " +i));

    s[i] = new student(i,name,major,gpa);  
}

I tried to do vars here that get data from user by JOptionPane, but it seems that i only use my already made constructor , not the Set methods. 我试图在这里做vars,通过JOptionPane从用户那里获取数据,但是似乎我只使用了已经完成的构造函数,而不是Set方法。

I need to use the methods because it has some validation code inside it. 我需要使用这些方法,因为其中包含一些验证代码。

Any ideas? 有任何想法吗?

Don't use values as constructor parameters. 不要将值用作构造函数参数。

Use only implicit constructor that will allocate memory. 仅使用将分配内存的隐式构造函数。 As for set methods, write them like : 至于set方法,请像这样编写它们:

public void setName(String name) { this.name = name; }

In this set methods do your validation code . 在这套方法中,您需要验证代码 Your class needs to have private attiributes, which are exact same as your constructor parameters. 您的班级需要有私人服装,这些服装与构造函数参数完全相同。

Then change line s[i]=new student(i,name,major,gpa); 然后更改line s[i]=new student(i,name,major,gpa); with

s[i] = new Student();
s[i].setNumber(i);
s[i].setName(name);
s[i].setMajor(major);
s[i].setGpa(gpa);

I hope this is what you meant 我希望这就是你的意思

Edit : Or continue using constructors and do validaing with parameters BEFORE you make new instance of Student 编辑:或继续使用构造函数,并在制作Student的新实例之前对参数进行验证

It's not so clear but actually what you want to do is to inizialize a student without adding it to the array until you are sure that fields are correct 尚不清楚,但实际上您想要做的是在不确保字段正确的情况下,将一个学生激化,而不将其添加到数组中

Student[] s = new Student[5];
String name, major;
double gpa;
boolean isCorrect = false;
Student currentStudent;

// in your code there is i = 1, is it intended or mistake?
for (int i=0; i <= s.length; i++)
{
  currentStudent = new Student();

  while (!isCorrect)
  {
    name = JOptionPane.showInputDialog("Please Write Name for student n " + i);
    isCorrect = currentStudent.setName(name);

    if (!isCorrect)
      JOptionPane.showMessageDialog(null, "Errors in validating name!");
  }

  isCorrect = false;
  while (!isCorrect)
  {
    major = JOptionPane.showInputDialog("Please Write Major for student n " + i);
    isCorrect = currentStudent.setMajor(major);

    if (!isCorrect)
      JOptionPane.showMessageDialog(null, "Errors in validating major!");
  }

  isCorrect = false
  while (!isCorrect)
  {
    gpa = Double.parseDouble(JOptionPane.showInputDialog("Please Write GPA for student n " +i));
    isCorrect = currentStudent.setGPA(gpa);

    if (!isCorrect)
      JOptionPane.showMessageDialog(null, "Errors in validating GPA!");
  }

  s[i] = currentStudent;
}

This approach will keep asking the user the same field until it's correct.. of course your setters will need to return a boolean that is false if validation failed. 这种方法将一直询问用户相同的字段,直到正确为止。.当然,如果验证失败,您的设置者将需要返回一个布尔值,该布尔值将为false

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM