Java - Compile Error Method Can't be Called

2020-05-10 08:56发布

问题:

I have to compile my code with a Testing Tool, however, when that testing tool calls my method, I receive this error:

"method getCourseDetails in class Course cannot be applied to given types;

required: java.lang.String,int,java.lang.String,boolean,java.lang.String.java.lang.String,double

found: no arguments

reason: actual and formal argument lists differ in length.

The operator that you use here cannot be used for the type of value that you are using it for. You are either using the wrong type here, or the wrong operator."

This is my method:

   public static void getCourseDetails(String department, int number, String name, boolean isFull, 
       String SCHOOL_NAME, String motto, double price){
   if (department.length() != (0) && number != 0 && name.length() != (0) && price != 0) {
        System.out.print(department + " ");
    } else if (department.length() == (0)){
        System.out.print ("Sorry, there is an error with the course department.");
        return;
    }
   if (number == 0) {
        System.out.print("Sorry, there is an error with the course number.");
        return;
    } else if (number != 0 && department.length() != (0) && name.length() != (0) && price != 0){
        System.out.print(number + " ");
    }
   if (name.length() != (0) && number!= 0 && department.length() != (0) && price != 0) {
        System.out.println(name + ".");
    } else if (name.length() == (0)) {
        System.out.print("Sorry, there is an error with the course name.");
        return;
    }
   if (price  == 0){
        System.out.print("Sorry, there is an error with the course price.");
        return;
    } 
    //System.out.println(department + " " + number + " is " + name);
   if (isFull == true){
        System.out.println("The course is currently full.");
    } else if (isFull == false){
        System.out.println("The course is currently not full.");
    }
   System.out.println("The course is currently run at " + SCHOOL_NAME + 
   " where their motto is " + "\"" + motto + "\"");    

回答1:

Your problem is that you are not passing in the appropriate parameters into the method, hence it spits out that error.

public static void getCourseDetails(String department, int number, String name, boolean isFull, 
   String SCHOOL_NAME, String motto, double price){

For this code, you need to pass in all of those variables in that same order (1st string, 2nd int, etc.). You can't just all getCourseDetails() with nothing inside it and expect something to happen, because all the information you're trying to process in that method never actually goes inside of it.

So, for example, when you call this method, it may look like this

String department = "Math";
int number = 101;
String name = "Williams";
boolean isFull = false;
String SCHOOL_NAME = "Pinkerton High"
String motto = "We Never Sleep"
double price = 100.0;
//note that the variable names do not have to be the same here
//as they are in the method
getCourseDetails(department, number, name, isFull, SCHOOL_NAME, motto, price);