错误消息&#34; java.lang.NoSuchMethodError:Customer。<init>(Ljava / lang / String; D)V&#34; </init>

时间:2014-03-26 01:12:46

标签: java class object

运行此程序时,我不断收到相同的错误消息。这就是我得到的:

Exception in thread "main" java.lang.NoSuchMethodError: Customer.<init>(Ljava/lang/String;D)V 
at Customer5.input(Customer5.java:35)<b>---(See below (code) to refer to lines 35 & 7)
at Customer5.main(Customer5.java:7)

另一件事是“客户”类显示一条消息,上面写着“已定义客户类型”。

import java.util.*;
public class Customer5 {

    public static void main(String[] args) {


        Customer[] customers=input();//This is line 7
        customers=sort(customers);
        output(customers);
    }


    public static Customer[] input(){

        Scanner input =new Scanner(System.in);
        int n;


        String name;
        double debt;


        System.out.print("Enter number of customers :");
        n=input.nextInt();

        Customer[] customers=new Customer[n];
        for(int i=0; i<n; i++){

            System.out.print("Enter name:");
            name=input.next();

            System.out.print("Enter debt:");
            debt=input.nextDouble();

            customers[i]=new Customer(name, debt);//This is line 35
        }
        return customers;
    }


    public static Customer[] sort(Customer[] customers){

        Customer temp;
        for(int i=0; i<customers.length; i++){

             for(int j=i+1; j<customers.length; j++){

                   if(customers[j-1].debt>customers[j].debt){

                         temp=customers[j-1];
                         customers[j-1]=customers[j];
                         customers[j]=temp;
                   }
             }
        }
              return customers;
    }



     public static void output(Customer[] customers){
            for(int i=0; i<customers.length; i++){
             System.out.println(customers[i].name+"\t" +customers[i].debt);
            }
     }



} 

class Customer{ //this line shows a message that says:The type Customer is already defined

    public String name;
    public Double debt;

    public Customer(String name, double debt){
       this.name=name;
       this.debt=debt;
    }

}

我不知道该怎么做才能解决它。我对这种类型的错误消息不是很熟悉。我非常感谢有关如何处理此问题的任何反馈或意见。谢谢!

2 个答案:

答案 0 :(得分:11)

经过测试,它对我来说很好。

查找&#34;清除并构建&#34; IDE中的选项应该可以解决问题。


另外请确保您的项目中不存在其他Customer类。

答案 1 :(得分:3)

这个类对我运行正常,问题是你的包或应用程序中必须有一些其他Customer类没有构造函数public Customer(String name, double debt)

尝试点击Ctrl + Shift + R并搜索客户或 按Ctrl + H,在所有java文件中搜索Customer