数组返回一个String而不是一个Object

时间:2013-04-15 12:08:51

标签: java

您好我必须首先制作一个程序我要求登录代码我放了try/catch因为我想密码只是数字,如果给一个字符来刺激异常 并且secont没有显示对象Product的n个数组,而是一个字符串Productl@pos51sd,我无法找到为什么告诉我这个。

import java.rmi.Naming;
import java.util.Scanner;


public class RMIClient {

    /**
     * @param args
     */
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
                int pin=0;
        try {   

            String  name    =   "//Localhost/RMI_Server";
            Functions lp    =(Functions)Naming.lookup(name);
            try{
            System.out.println("Please give your Personal Code (PIN)");

                        pin=in.nextInt();
             }catch (NumberFormatException nfe) {
                    System.out.println("Input must be a number.");
                            }
            if(lp.validate(pin)){
            System.out.println("Give description of item");
            String desc=in.next();
                        int sizep=lp.search(desc).length;
                        Product [] p =new Product[sizep];
                        System.arraycopy(lp.search(desc),0,p,0,sizep);
                        for(int i=0;i<sizep;i++){
            System.out.println(p[i].toString());}
            System.out.println("Give numberCode of item you want to buy");
            String code=in.next();
            System.out.println(lp.purchase(code));
            }else{
            System.out.println("Your code is False");}
            lp.close();
        }catch (Exception   e)  {
             System.out.println("SumClient  err:    " + e); 
             System.exit(1);
        }   
    }   
}

3 个答案:

答案 0 :(得分:0)

它打印toString()方法的结果。

您可以在System.out上找到有关打印例外的一些详细信息。在java这里http://alvinalexander.com/blog/post/java/java-exception-printing-log4j

答案 1 :(得分:0)

不幸的是,您没有向我们展示课程Product,但您实际想要做的是根据您的要求在此课程中实施方法toString()。我不知道pos51sd是什么,但只是讨论让我们调用包含此值code的变量。

在这种情况下,您的班级Product应如下所示:

class Product {
    .....
    private String code;
    .......

    public String toString() {
        return getClass().getSimpleName() + "@" + code;
    }
} 

答案 2 :(得分:0)

类Product'import java.io.Serializable;

public class Product实现Serializable {

private String description;
private String serialNum;
private float price;
private String company;

public Product(String description,String serialNum,float price,String company){
this.description=description;
this.serialNum=serialNum;
this.price=price;
this.company=company;       
}

public void set_description(String description){this.description=description;}
public String get_description(){return description;}
public void set_serialNum(String serialNum){this.serialNum=serialNum;}
public String get_serialNum(){return serialNum;}
public void set_price(float price){this.price=price;}
public float get_price(){return price;}
public void set_company(String company){this.company=company;}
public String get_company(){return company;}



public String toSting(){
    return "Item "+ serialNum +" is product from "+ company +" price " + price +"E Description: ( "+ description +" )";

}

} “