gpt4 book ai didi

java - 错误: int cannot be converted to class?

转载 作者:行者123 更新时间:2023-12-02 11:15:41 26 4
gpt4 key购买 nike

UnitedStates类是二分查找类

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(states, 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 null
public int binarySearch(ArrayList<State> states, 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 stateName)
{
State state = (State)stateName;

String otherStateName = state.getName();

return name.compareTo(otherStateName);
}

}

我目前正在开发一个二进制搜索项目,并且在解决我确实知道如何解决的错误时遇到问题。我试图在 UnitedStates 类中调用 ("State state = binarySearch(states, stateName);") 行的 binarySearch 方法,但它给出的错误是“不兼容的类型:int 无法转换为 State”。我不知道如何将 int 类型转换为类,如果有人能告诉我如何解决此错误,我将非常感激。

最佳答案

binarySearch 返回一个 int

public int binarySearch(ArrayList<State> states, String key)

binarySearch 的注释是错误的。

// If key is found it returns the State object that
// corresponds to key; otherwise it returns null

也许应该是这样

// If key is found it returns the index of the State
// object that corresponds to key; otherwise it returns -1.

您正在尝试将生成的 int 分配给一个状态

State state = binarySearch(...);

也许分配

int stateIndex = binarySearch(...);
State state = null;
if (stateIndex >= 0) {
state = states.get(stateIndex);
...
} else {
// deal with error.
}

关于java - 错误: int cannot be converted to class?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50297432/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com