【问题标题】:How can I create a instance of a class with an array of Strings as the only instance variable?如何创建一个以字符串数组作为唯一实例变量的类的实例?
【发布时间】:2017-07-15 21:34:21
【问题描述】:

我写了一个班级表格。 10 个字符串的数组是构造函数中的唯一参数。如何创建类的实例?我确信我的 setter 和 getter 中还有其他问题,但在我解决这个问题之前我无法测试它们。

public class FormLab {



public static void main(String[]args){

    Form f1 = new Form(String webForm);

    System.out.println(print(String[] webForm));
    System.out.println("Any Empty Strings? " + f1.getEmptyFields);
    System.out.println("Number of characters in userID? " + f1.getCharNum);
    System.out.println("Do password fields match? " + f1.getPwCheck);
    System.out.println("Does email contain correct characters? " + f1.getEmailCheck);




}

}


public class Form {

String[] webForm = new String[10];

private String userID;
private String pw;
private String pw2;
private String email;
private String name;
private String address;
private String city;
private String state;
private String zip;
private String telephone;

//constructor 
public Form(String[] webForm){


    //filling array with field values
    webForm[0] = "0123456789";
    webForm[1] = "java123";
    webForm[2] = "java123";
    webForm[3] = "luke.skywalker@jedi.com";
    webForm[4] = "Luke Skywalker";
    webForm[5] = "1234 The Force Way";
    webForm[6] = "Rome";
    webForm[7] = "GA";
    webForm[8] = "30161";
    webForm[9] = "7065551234";


}

public boolean getEmptyFields() {
    //boolean empty = false;
    for(int i = 0; i < webForm.length; i++){
        if(webForm[i]!= null){
            return true;
        }
    }

    return false;
}

public String getUserID(){

    return userID;

}

public void setUserId(String userID){
    this.userID = userID;
}

public String getPw(){

    return pw;

}

public void setPw(String pw){
    this.pw = pw;
}

public String getPw2(){

    return pw2;

}

public void setPw2(String pw2){
    this.pw2 = pw2;
}

public String getEmail(){

    return email;

}

public void setEmail(String email){
    this.email = email;
}

public String getName(){

    return name;

}

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

public String getAddress(){

    return address;

}

public void setAddress(String address){
    this.address = address;
}

public String getCity(){

    return city;

}

public void setCity(String city){
    this.city = city;
}

public String getState(){

    return state;

}

public void setState(String state){
    this.state = state;
}

public String getZip(){

    return userID;

}

public void setZip(String zip){
    this.zip = zip;
}

public String getTelephone(){

    return telephone;

}

public void setTelephone(String telephone){
    this.telephone = telephone;
}

public int getCharNum(String userID) {
    int userLength = 0;

    //userID.length();

    return userLength;

}

public boolean getPwCheck() {
    boolean check = pw.equalsIgnoreCase(pw2);
//  pw.equalsIgnoreCase(pw2);

    return check;

}

public boolean getEmailCheck(String email) {

    if(email.contains("@") && email.contains(".")){
        return true;
    }
    return false;
    }

public static void getPrint(String[] webForm) {

System.out.println(webForm.toString());

}

}

【问题讨论】:

  • Form 构造函数不应该有任何参数。它应该自己创建包含 10 个元素的数组,而不是强制所有调用者提供构造函数将覆盖的包含 10 个元素的数组。谷歌“Java 教程数组”以了解如何创建数组。谷歌“Java 教程方法”了解如何调用方法。

标签: java arrays string class


【解决方案1】:

使用方法arraycopy。

public Form(String[] webForm){
    System.arraycopy(webForm, 0, this.webForm, 0, webForm.length);
}

【讨论】:

    【解决方案2】:

    构造函数接受一个字符串数组作为参数,但你传递给它的是一个字符串,试试:

    myList = new String[];
    myForm = new Form(myList);
    

    【讨论】:

    • 他显然有更多问题,因为传入的数组没有做任何事情......传入一个实际上什么都不做的数组是没有用的。他的构造函数参数也会导致与全局数组的歧义。
    【解决方案3】:

    您需要在将其作为参数之前声明您的数组,如下所示:

     public class FormLab {
    
     public static void main(String[]args){
    
          String webForm = new String[10];
          Form f1 = new Form(webForm);
    
          /*System.out.println(print(String[] webForm)); THIS IS WRONG*/
          /*TO PRINT THE VALUES IN THE ARRAY YOU NEED TO WRITE A METHOD IN UR 
            FORM CLASS THAT WILL LOOP THRU UR VARIABLE AND PRINT*/
          System.out.println("Any Empty Strings? " + f1.getEmptyFields);
          System.out.println("Number of characters in userID? " + f1.getCharNum);
          System.out.println("Do password fields match? " + f1.getPwCheck);
          System.out.println("Does email contain correct characters? " + f1.getEmailCheck); 
    }
    

    【讨论】:

    • 我在Form类中声明的时候为什么还要在main方法中声明呢?
    • main 方法对表单类中的变量一无所知。在你的构造函数中,你传递一个参数,因此你需要在你的类实例中的 main 方法中传递一个参数。如果您想使用表单类中的变量,请删除表单类构造函数中的参数
    • 我已经尝试了所有建议,但仍然出现错误。构建过程中发生错误。在项目“实验室”上运行构建器“Java Builder”时出错。 java.lang.NullPointerException
    • also: 标记 "]" 的语法错误,:: 应在此标记之后
    • 实际上你调用的方法需要一个参数才能在数组上执行。因此,只需注释掉所有 system.out.println 并使用 for 循环打印值
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-09
    • 1970-01-01
    • 2013-03-05
    • 1970-01-01
    相关资源
    最近更新 更多