Error: Int type cannot be converted to class

Error: Int type cannot be converted to class

美国Class

 public class UnitedStates
{
// instance variables
private ArrayList <State> states;

public UnitedStates()
{
   states = new ArrayList <State> ();


   readFile();
   printStates();

   searchStates();  
}

public void searchStates()
{
    Scanner keyboard = new Scanner(System.in);
    String ans = "y";
    System.out.println();
    System.out.println("=====================");
    System.out.println("     Searching");
    System.out.println("=====================");

    while(ans.equalsIgnoreCase("y"))
    {
        System.out.println();
        System.out.print("Enter state -->");
        String stateName = keyboard.nextLine();

                    State state = binarySearch(stateName);

        if(state == null)
           System.out.println("State not found");
        else
        {
          System.out.println();
          System.out.println("State Name       = " + state.getName());
          System.out.println("State Capital    = " + state.getCapital());
          System.out.println("State Nickname   = " + state.getNickname());
          System.out.println("State Population = " + state.getPopulation());
          System.out.println(); 
        }


        System.out.println();
        System.out.print("Search again[Y/N]?");
        ans = keyboard.nextLine();

    }
}

// Performs a binarySearch on states searching for key
// If key is found it returns the State object that
// corresponds to key; otherwise it returns -1
public State binarySearch(String key)
{
        int left = 0;
        int right = states.size() - 1;

        while(left <= right)
        {
            int midpoint = (left + right) / 2;

            int result = states.get(midpoint).compareTo(key);

            if(result == 0)
            {    
                return midpoint;
            }
            else if(result < 0)
            {
                left = midpoint + 1;
            }
            else
            {
                right = midpoint - 1;
            }

}
        return -1;
    }

public void printStates()
{
    for(State s : states)
    {
        System.out.printf("%-15s", s.getName());
        System.out.printf("%-15s", s.getCapital());
        System.out.printf("%-25s", s.getNickname());
        System.out.printf("%10s\n", s.getPopulation()); 
    }
}

public void readFile()
{
    Scanner scan = null;
    try
    {
        scan = new Scanner(new File("states.txt"));
    }
    catch(FileNotFoundException ex)
    {
        System.out.println("File not Found!");
    }

    String name;
    String capital;
    String nickname;
    int population;
    while(scan.hasNextLine())
    {
        name = scan.nextLine();
        capital = scan.nextLine();
        nickname = scan.nextLine();
        population = scan.nextInt();
        if(scan.hasNextLine())
        {
          String dummy = scan.nextLine();   
        }


        State state = new State(name, capital, nickname, population);
        states.add(state);
    }



}
}

状态class

 public class State implements Comparable
 {
// instance variables
private String name;
private String capital;
private String nickname;
private int population;

public State(String n, String c, String nn, int p)
{
    name = n;
    capital = c;
    nickname = nn;
    population = p;
}


    public String getName()
{
    return name;
}

public String getCapital()
{
    return capital;
}

public String getNickname()
{
    return nickname;
}

public int getPopulation()
{
    return population;
}

// Comparable Interface method
// Casts obj to a String, then calls the String class's
// compareTo method to compare the name of this state
// to the name passed in the parameter list. It returns
// either a positive number, negative number, or zero.

@Override
public int compareTo(Object otherState) 
{
   String otherName = ((State)otherState).getName();

   return name.compareTo(otherName);
}

}

我知道格式很糟糕。然而,在第 73 和 85 行,我正在努力解决一个错误,其中指出 "Incompatible types: int cannot be converted to State"。我知道错误是因为方法的return类型是State。但是我不能将它更改为 int,因为在第 35 行中,我的状态变量设置为 Class 类型,如果更改 binarySearch 方法类型,这将 return 出错。

问题是您具有以下函数签名:

public State binarySearch(String key)

说你必须 return 类型为 State 的对象,但是你 return 类型为 intreturn midpoint; 和 return -1;`.最简单的解决方法是将签名更改为

public int binarySearch(String key)

另一种解决方案是保持方法签名不变,return State 对象位于给定索引处。而不是

return midpoint;

return states.get(midpoint);

而不是

return -1;

return null;

虽然可以 return a -1State 正如 binarySearch() 的评论所建议的那样,但这不是一个好主意,因为它们有两个不同的类型。