错误:Int类型无法转换为类

时间:2018-05-14 18:52:39

标签: java type-conversion binary-search

美国班级

 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);
    }



}
}

州级

 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行,我正在努力解决一个错误,它指出“不兼容的类型:int无法转换为State”。我知道错误是由方法的返回类型为State引起的。但是我无法将其更改为int,因为在第35行中,我的状态变量设置为Class类型,如果更改了binarySearch方法类型,它将返回错误。

1 个答案:

答案 0 :(得分:0)

问题是你有以下功能签名:

public State binarySearch(String key)

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

public int binarySearch(String key)

另一个解决方案是保留方法签名,并返回给定索引处的State对象。而不是

return midpoint;

DO

return states.get(midpoint);

而不是

return -1;

DO

return null;

虽然可以返回-1State作为binarySearch()的评论建议,但这不是一个好主意,因为它们有两种不同的类型。