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.
Aucun commentaire:
Enregistrer un commentaire