Java代码报错:int无法转换为State类型的问题求助
Let's break down exactly what's causing those errors at lines 73 and 85, and fix them step by step:
The Core Problem
Your binarySearch method is declared to return a State object, but you're trying to return integer values (midpoint at line 73 and -1 at line 85) — that's a direct type mismatch. Plus, there's a hidden issue with how you're comparing values in the search that would cause problems even after fixing the return type.
Step 1: Fix the Binary Search Return Logic
Instead of returning an index or -1, you need to return the matching State object when found, and null when not found (since State is a reference type, null is the correct "not found" signal here, and your existing searchStates method already checks for null to handle that case).
Step 2: Fix the Comparison Logic
Your State class's compareTo method expects another State object, but you're passing a String (your search key). Instead, you should compare the search key directly to the name of the State at the midpoint.
Step 3: Clean Up the Comparable Implementation
Using raw Comparable is unsafe — adding a generic type makes it type-safe and eliminates unnecessary casting.
Modified Code Snippets
1. Updated binarySearch Method in UnitedStates
// 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 State binarySearch(String key) { int left = 0; int right = states.size() - 1; while(left <= right) { int midpoint = (left + right) / 2; State midState = states.get(midpoint); // Compare search key directly to the midpoint state's name int result = midState.getName().compareToIgnoreCase(key); // Case-insensitive for better UX if(result == 0) { // Return the matching State object, not the index return midState; } else if(result < 0) { left = midpoint + 1; } else { right = midpoint - 1; } } // Return null instead of -1 when no match is found return null; }
2. Updated State Class with Generic Comparable
// Use generic Comparable to avoid raw types and unsafe casting public class State implements Comparable<State> { // 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; } // Getters remain unchanged public String getName() { return name; } public String getCapital() { return capital; } public String getNickname() { return nickname; } public int getPopulation() { return population; } // Type-safe compareTo method (no more casting!) @Override public int compareTo(State otherState) { return name.compareToIgnoreCase(otherState.getName()); } }
Quick Extra Tip
I used compareToIgnoreCase in both places to make the search case-insensitive — users might type "texas" instead of "Texas", and this small change makes your app much more user-friendly without breaking functionality.
内容的提问来源于stack exchange,提问作者LudiCrudi




