编译器无法识别类字符串值与另一个字符串值相等。

huangapple go评论70阅读模式
英文:

Compiler not recognizing that class string value is equal to another String value

问题

public class Station {
    String name;
    boolean pass;
    int distance;

    // 创建一个新的站点,接受名称、是否通行和距离作为参数
    public static Station createStation(String name, boolean pass, int distance) {
        Station station = new Station();
        station.name = name;
        station.pass = pass;
        station.distance = distance;
        return station;
    }

    // 包含伦敦所有站点的数组列表
    static Station[] StationList = {
            createStation("Stepney Green", false, 100),
            createStation("Kings Cross", true, 700),
            createStation("Oxford Circus", true, 200)
    };

    public static String infoAboutStation(Station station) {
        String message;
        if (station.pass)
            message = "This station, " + station.name + ", is a London Underground Station.";
        else
            message = "This station, " + station.name + ", is not a London Underground Station.";
        return message;
    }

    public static String checkInfo(String stationName) {
        String info = null;

        for (Station station : StationList) {
            if (stationName.equals(station.name)) {
                info = infoAboutStation(station);
                break; // Exit the loop once a matching station is found
            } else {
                info = stationName + " is not a London Underground Station.";
            }
        }
        return info;
    }

    public static void printInfo() {
        Scanner scanner = new Scanner(System.in);
        System.out.println("How many... ");
        int totalResponses = Integer.parseInt(scanner.nextLine());

        String choice;
        for (int i = 0; i < totalResponses; ++i) {
            System.out.println("Enter a station: ");
            choice = scanner.nextLine();
            System.out.println(checkInfo(choice));
        }
    }
    // psvm(String[] args)
}

请注意,我已经对代码进行了一些修改:

  1. createStation 方法中,我创建了一个新的 Station 对象并设置了其属性,然后返回该对象。

  2. infoAboutStation 方法中,我更改了消息的构建方式,以便更清晰地显示站点信息。

  3. checkInfo 方法中,我使用了 equals 方法来比较字符串,因为在Java中,使用 == 比较字符串时,比较的是引用而不是内容。另外,我添加了一个 break 语句,以便在找到匹配站点后退出循环。

这些修改应该解决你遇到的问题,并提高了代码的可读性和效率。

英文:
public class Station {
    String name;
    boolean pass;
    int distance;

    // method to create a new Station taking the arguments of the name,pass and distance in the parameter
    public static Station createStation(String name, boolean pass, int distance) {  
    }

    // Array list containing all the Stations in London we will use
    static Station[] StationList = {createStation(&quot;Stepney Green&quot;, false, 100), createStation(&quot;Kings Cross&quot;, true, 700), createStation(
            &quot;Oxford Circus&quot;, true, 200)};

    public static String infoAboutStation(Station station) {
        String message;
        if (station.pass)
            message = //string message
        else
            message = //string message
        return message;
    }

    public static String checkInfo(String stationName){
        String info = null;

        for(Station station : StationList){     
            if(stationName == station.name) { //it does not recognise it to be the same for some reason
                info = stationName + infoAboutStation(station);
            }
            else
                info = stationName + &quot; message &quot;;
        }
        return info;
    }

    public static void printInfo(){
        Scanner scanner = new Scanner(System.in);
        System.out.println(&quot;How many... &quot;);
        int totalResponses = Integer.parseInt(scanner.nextLine());

        String choice;
        for(int i = 0; i &lt; totalResponses; ++i){
            System.out.println(&quot;Enter a station: &quot;);
            choice = scanner.nextLine();
            System.out.println(checkInfo(choice));
        }
    }
    // psvm(String[] args)
}

So the program runs fine but when we use "Stepney Green", "Kings Cross", "Oxford Circus" as the input for choice at checkinfo(choice), it does not recognise it to be the same for stationName == station.name in the method checkInfo(stationName //choice)

Intellij debugger reads this at stationName == station.name

-StationList = {Station[3]@980} -> 0 = {Station@994} 1 = {Station@997} 2 = {Station@998}

-static -> StationList = {Station[3]@980}

-stationName = "Stepney Green" value = {byte[13]@996} coder = 0 hash = 0 hashisZero = 0

-info = null

-station = {Station@994} -> name = "Stepney Green", pass = false, distance = 100

-station.name = "Stepney Green" -> value = {byte[13]@999] rest is zero

Beside the statement it says stationName: "Stepney Green" station: Station@994. Instead of printing out stationName + infoAboutStation it goes to the else station and prints "Stepney Green is not a London Underground Station". I am so puzzled as to why. Also if you don't mind helping me as to making this code a little more efficient and better.

答案1

得分: 0

你应该使用 equals() 方法来比较 String。将这段代码进行替换:

if(stationName == station.name)

改为:

if(stationName.equals(station.name))
英文:

You should compare String using equals()
Replace this

if(stationName == station.name)

by

if(stationName.equals(station.name))

答案2

得分: 0

**FIXED** 更改为

for(Station station : StationList){
if(stationName.equals(station.name)) {
info = stationName + infoAboutStation(station);
break;
}
}

谢谢您的帮助
英文:

FIXED changed to

for(Station station : StationList){
    if(stationName.equals(station.name)) {
        info = stationName + infoAboutStation(station);
        break;
    }
}

Thanks for the help

huangapple
  • 本文由 发表于 2020年9月28日 02:45:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/64092073.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定