开发者

conflict b/w string and string[]

Basically I have a class that has methods which use String arrays and i'm writing a method in the application class to read a file and update an array of object of class Customer. I get errors like:

Line 83: set_address(java.lang.String[]) in Customer cannot be applied to (java.lang.String) at the line review[i].set_address(st[1]). I understand that it is looking for a string[] and it is receiving a string but is there any way to fix this? Here's the code I'm working with.

    import java.io.*;
import开发者_运维技巧 java.util.*;

class Customer {

int account_id;
char[] ch1 = new char[20];
String name = new String (ch1);
char[] ch2 = new char[80];
String address = new String (ch2);
char[] ch3 = new char[10];
String phone_number = new String (ch3);
char[] ch4 = new char[8];
String date_of_birth = new String (ch4);
double account_balance;

public int get_accountid(){
       return account_id;
}

public String get_address(){
       return address;
}

public String get_phone_number(){
       return phone_number;
}

public String get_date_of_birth(){
       return date_of_birth;
}

public double get_balance(){
       return account_balance;
}

public void set_account_id(int num){
       account_id = num;
}

public void set_address(String add){
       address = add;
}

public void set_phone_number(String phone){
       phone_number = phone;
}

public void set_date_of_birth(String dob){
       date_of_birth = dob;
}

public void set_balance(double bal){
       account_balance = bal;
}
Customer(){ // default constructor
}

// parametrized constructor
Customer(int id, String name, String add, String dob, String num, double bal){
    this.account_id = id;
    this.name = name;
    this.address = add;
    this.date_of_birth = dob;
    this.phone_number = num;
    this.account_balance = bal;
}

}


     public class lab2{
     public static void main(String args[]){

     System.out.println("testing this shit");    
      }
      public static void readFile(String filename){
          Customer[] review = new Customer[30];
          int i=0; 
          Scanner scan = new Scanner (new File (filename));
          while (scan.hasNext()){

               while(i<30){

                      review[i].set_account_id(scan.nextInt());
                  String[] st = scan.nextLine().split("=");
                  review[i].set_address(st[1]);
                  st = scan.nextLine().spilt("=");
                      review[i].set_phone_number(st[1]);
                      st = scan.nextLine().split("=");
                      review[i].set_date_of_birth(st[1]);
                      //st = scan.nextLine().split("=");
                      review[i].set_balance(scan.nextDouble());
                      scan.nextLine();
                      i=i+1;
             }
        }
     }

   }


Your class Customer looks like a Java bean. I find these declaration suspicious:

String[] name = new String [20];
String[] address = new String [80];
String[] phone_number = new String [10];
String[] date_of_birth = new String [8];

Why do you want a Customer to have 20 names, 80 addresses, 10 phone numbers, and 8 date of birth? I suspect that your intention is saying that a Customer name is at most 20 characters long, his/her address is at most 80 characters long, etc. If this is the case, than you don't want a String[], you may want a char[]!

However, think about making those fields simply String: it seems more natural. I don't see reason why you may want to limit their size.


Just change your method signature:

public void set_address(String add){
       address = add;
}

Or other choice: You create a new String[] object based on your String object an pass this:

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜