JAVA - 字符串args [0]的数组错误

时间:2018-01-26 20:14:23

标签: java

/ *需要帮助。线程" main"中的客户端类Exception出错。 java.lang.ArrayIndexOutOfBoundsException:0在Client.main(Client.java:14) 我强调了这条线。

驱动程序d1 =新驱动程序(args [0],Integer.parseInt(args [1]),Integer.parseInt(args [2]),Double.parseDouble(args [3]));

我无法弄清楚出了什么问题。 * /

/ *驱动程序类* /

public class Driver {

    private String lastName;
    private int age;
    private int licenseNo;
    private double yr;
    private static int id=0; 

    public Driver() {

        this.lastName = "No Name";
        this.age = 16;
        this.licenseNo = 11111;
        this.yr = 0.5;
        id++;
    }

    public Driver(String lastName, int age, int licenseNo, double yr) {
        this.lastName = lastName;
        this.age = age;
        this.licenseNo = licenseNo;
        this.yr = yr;
        id++;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        Driver other = (Driver) obj;
        if (lastName.equals(other.lastName) && licenseNo==other.licenseNo)
            return true;
        else  
            return false;
    }

    @Override
    public String toString() {
        return "The name of the Driver is:" + lastName + ", The age is:" + age + ", The license no is:" + licenseNo + ", Duration of driving:" + yr ;
    }
    public String getLastName() {
        return lastName;
    }
    public void setLastName(String lastName) {
        this.lastName = lastName;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public int getLicenseNo() {
        return licenseNo;
    }
    public void setLicenseNo(int licenseNo) {
        this.licenseNo = licenseNo;
    }
    public double getYr() {
        return yr;
    }
    public void setYr(double yr) {
        this.yr = yr;
    }
}

/ *客户端类* /

import java.util.Scanner;

public class Client {

    public static int licenseFee(Driver dr){

        double fee=((dr.getAge()*dr.getLicenseNo())/100000)+50;
        if(fee>100)
            return 100;
        else
            return (int)fee;
    }

    public static void main(String[] args) {
       // TODO Auto-generated method stub
        Driver d1 = new Driver(args[0], Integer.parseInt(args[1]), Integer.parseInt(args[2]), Double.parseDouble(args[3]));
        System.out.println(d1);
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter driver's last name:");
        String temp1 = sc.next();
        System.out.println("Enter age:");
        int temp2=sc.nextInt();
        System.out.println("Enter license:");
        int temp3=sc.nextInt();
        System.out.println("Enter Years:");
        double temp4=sc.nextDouble();

        Driver d2= new Driver(temp1, temp2, temp3,temp4);
        System.out.println(d1);
        System.out.println("License fee:"+licenseFee(d1));
        System.out.println(d2);
        System.out.println("License fee:"+licenseFee(d2));
        if(d1.equals(d2))
            System.out.println("Command line object is equal to keyboard object");
        else
            System.out.println("Command line object is NOT equal to keyboard object");

    }
}  

3 个答案:

答案 0 :(得分:1)

您的代码没有任何问题,但验证您收到的所有参数非常重要。

在主要方法中试试这个。

public static void main(String[] args) {
    if (args == null || args.length <4){
        System.out.println("All params are required. lastname, age, licenseNo and year");           
    }else{
         // TODO Auto-generated method stub
        Driver d1 = new Driver(args[0], Integer.parseInt(args[1]), Integer.parseInt(args[2]), Double.parseDouble(args[3]));
        System.out.println(d1);
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter driver's last name:");
        String temp1 = sc.next();
        System.out.println("Enter age:");
        int temp2=sc.nextInt();
        System.out.println("Enter license:");
        int temp3=sc.nextInt();
        System.out.println("Enter Years:");
        double temp4=sc.nextDouble();

        Driver d2= new Driver(temp1, temp2, temp3,temp4);
        System.out.println(d1);
        System.out.println("License fee:"+licenseFee(d1));
        System.out.println(d2);
        System.out.println("License fee:"+licenseFee(d2));
        if(d1.equals(d2))
            System.out.println("Command line object is equal to keyboard object");
        else
            System.out.println("Command line object is NOT equal to keyboard object");
    }
}

答案 1 :(得分:0)

args [0],args [1]等是在执行程序之前传递给main方法的命令行参数的索引。您似乎没有传递正确数量的参数(至少4个),或者您忘记传递所有参数。命令行参数用空格分隔。

您可能希望查看此链接以了解如何通过命令提示符或Eclipse或Netbeans传递命令行参数: http://java2career.com/2014/05/how-to-use-command-line-arguments-in.html

答案 2 :(得分:0)

C:\ Windows \ System32下&GT; CD ..

C:\ Windows和GT; CD ..

C:&gt; cd Users \ qle2 \ Downloads

C:\ Users \ qle2 \ Downloads&gt; java Client ali 32 55555 32.5

驱动程序的名称是:ali,年龄是:32,许可证号:55555,驾驶时间:32.5

输入驱动程序的姓氏: 阿里巴巴

输入年龄: 50

输入许可证: 1234567

输入年份: 12

驱动程序的名称是:ali,年龄是:32,许可证号:55555,驾驶时间:32.5

许可费:67 驱动程序的名称是:Alibaba,年龄是:50,许可证号:1234567,驾驶时间:12.0

许可费:100

命令行对象不等于键盘对象