1

你好,我必须首先制作一个程序我要求登录代码我放了一个try/catch因为我希望密码只有数字,如果给一个字符来刺激异常,第二个不是显示对象数组Product而是一个字符串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);
        }   
    }   
}
4

3 回答 3

0

类产品'import java.io.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 +" )";

}

} '

于 2013-04-15T12:35:18.660 回答
0

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

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

于 2013-04-15T12:11:55.007 回答
0

不幸的是,您没有向我们展示该类Product,但您真正想要做的是toString()根据您的要求在该类中实现方法。我不知道是什么pos51sd,但只是为了讨论让我们调用包含这个值的变量code

在这种情况下,您的课程Product应如下所示:

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

    public String toString() {
        return getClass().getSimpleName() + "@" + code;
    }
} 
于 2013-04-15T12:15:04.240 回答