0
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("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 = //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 + " message ";
        }
        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)
}

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.

GigaHiga
  • 39
  • 1
  • 7
  • Don't compare strings with `==` - that compares `stationName` and `station.name` reference the same string object. Since one is a compile time constant and the other a value read from a scanner they are **not** the same string object. Instead use `stationName.equals(station.Name)` – Thomas Kläger Sep 27 '20 at 18:57
  • @ThomasKläger no, it only works with Oxford Circus now but not for the other two – GigaHiga Sep 27 '20 at 19:16

2 Answers2

0

You should compare String using equals()
Replace this

if(stationName == station.name)

by

if(stationName.equals(station.name))
IQbrod
  • 2,060
  • 1
  • 6
  • 28
  • this does not work. It still gives the same error but this time it only works for "Oxford Circus" but not for "Kings Cross" and "Stepney Green" – GigaHiga Sep 27 '20 at 19:15
0

FIXED changed to

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

Thanks for the help

GigaHiga
  • 39
  • 1
  • 7