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

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

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

问题

  1. public class Station {
  2. String name;
  3. boolean pass;
  4. int distance;
  5. // 创建一个新的站点,接受名称、是否通行和距离作为参数
  6. public static Station createStation(String name, boolean pass, int distance) {
  7. Station station = new Station();
  8. station.name = name;
  9. station.pass = pass;
  10. station.distance = distance;
  11. return station;
  12. }
  13. // 包含伦敦所有站点的数组列表
  14. static Station[] StationList = {
  15. createStation("Stepney Green", false, 100),
  16. createStation("Kings Cross", true, 700),
  17. createStation("Oxford Circus", true, 200)
  18. };
  19. public static String infoAboutStation(Station station) {
  20. String message;
  21. if (station.pass)
  22. message = "This station, " + station.name + ", is a London Underground Station.";
  23. else
  24. message = "This station, " + station.name + ", is not a London Underground Station.";
  25. return message;
  26. }
  27. public static String checkInfo(String stationName) {
  28. String info = null;
  29. for (Station station : StationList) {
  30. if (stationName.equals(station.name)) {
  31. info = infoAboutStation(station);
  32. break; // Exit the loop once a matching station is found
  33. } else {
  34. info = stationName + " is not a London Underground Station.";
  35. }
  36. }
  37. return info;
  38. }
  39. public static void printInfo() {
  40. Scanner scanner = new Scanner(System.in);
  41. System.out.println("How many... ");
  42. int totalResponses = Integer.parseInt(scanner.nextLine());
  43. String choice;
  44. for (int i = 0; i < totalResponses; ++i) {
  45. System.out.println("Enter a station: ");
  46. choice = scanner.nextLine();
  47. System.out.println(checkInfo(choice));
  48. }
  49. }
  50. // psvm(String[] args)
  51. }

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

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

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

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

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

英文:
  1. public class Station {
  2. String name;
  3. boolean pass;
  4. int distance;
  5. // method to create a new Station taking the arguments of the name,pass and distance in the parameter
  6. public static Station createStation(String name, boolean pass, int distance) {
  7. }
  8. // Array list containing all the Stations in London we will use
  9. static Station[] StationList = {createStation(&quot;Stepney Green&quot;, false, 100), createStation(&quot;Kings Cross&quot;, true, 700), createStation(
  10. &quot;Oxford Circus&quot;, true, 200)};
  11. public static String infoAboutStation(Station station) {
  12. String message;
  13. if (station.pass)
  14. message = //string message
  15. else
  16. message = //string message
  17. return message;
  18. }
  19. public static String checkInfo(String stationName){
  20. String info = null;
  21. for(Station station : StationList){
  22. if(stationName == station.name) { //it does not recognise it to be the same for some reason
  23. info = stationName + infoAboutStation(station);
  24. }
  25. else
  26. info = stationName + &quot; message &quot;;
  27. }
  28. return info;
  29. }
  30. public static void printInfo(){
  31. Scanner scanner = new Scanner(System.in);
  32. System.out.println(&quot;How many... &quot;);
  33. int totalResponses = Integer.parseInt(scanner.nextLine());
  34. String choice;
  35. for(int i = 0; i &lt; totalResponses; ++i){
  36. System.out.println(&quot;Enter a station: &quot;);
  37. choice = scanner.nextLine();
  38. System.out.println(checkInfo(choice));
  39. }
  40. }
  41. // psvm(String[] args)
  42. }

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。将这段代码进行替换:

  1. if(stationName == station.name)

改为:

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

You should compare String using equals()
Replace this

  1. if(stationName == station.name)

by

  1. if(stationName.equals(station.name))

答案2

得分: 0

  1. **FIXED** 更改为

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

  1. 谢谢您的帮助
英文:

FIXED changed to

  1. for(Station station : StationList){
  2. if(stationName.equals(station.name)) {
  3. info = stationName + infoAboutStation(station);
  4. break;
  5. }
  6. }

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:

确定