-1

I have been working on a program.

I keep getting these errors:

StationInformation.java:65: error: non-static variable this cannot be referenced from a static context
      Station mile_end = new Station();
                         ^
StationInformation.java:66: error: non-static variable this cannot be referenced from a static context
      Station oxford_circus  = new Station();
                               ^
StationInformation.java:67: error: non-static variable this cannot be referenced from a static context
      Station kings_cross = new Station();
                            ^
StationInformation.java:68: error: non-static variable this cannot be referenced from a static context
      Station stepney_green = new Station();
                              ^
4 errors

I want to fix the program.

  • What do you want to fix about it? Does it currently do something wrong? – Hulk Nov 03 '20 at 09:52
  • 1
    It seems you try to initialize Strings via out-parameters in your `create_messages` - that is not possible in Java. References are passed by value, and cannot be modified within a method in a way that would be visible from outside the method. – Hulk Nov 03 '20 at 09:54
  • See https://stackoverflow.com/questions/40480/is-java-pass-by-reference-or-pass-by-value – Hulk Nov 03 '20 at 09:55

2 Answers2

0

Edit: This answer might seem outdated since the OP decided to edit the question, removing his code in the process.

There are 2 errors in your code:

  1. Your inner class Station is not static, meaning you cannot instantiate it in a static context. This is producing the error messages you see.
  2. You are thinking that Java is pass-by-reference, and are trying to override the value the variable(s) are pointing to (which you cannot do in Java).

You can fix your mistakes by making your Station-class static (static class Station), and by making the stations you use class-variables, and using their fields to create a String.
You could also implement a getInfo()-method for the Station-class that prepares its info on its own. With this, you can just call System.out.println(STATION_YOU_WANT.getInfo()).

I have taken a bit of my time to write a commented solution to the question.
The most confusing part of it is probably the use of varargs (String... in the code below). They basically allow you to pass any number of arguments to a method, which will inherently be converted to an array by Java.

import java.util.HashMap;
import java.util.Locale;
import java.util.Scanner;

public class StationInformation {
  private static class Station {
    private String name;
    private int distanceToPlatform;
    private boolean stepFree;
    private String[] alternateNames;
    
    Station(String name, int distanceToPlatform, boolean stepFree, String...alternateNames) {
      this.name = name;
      this.distanceToPlatform = distanceToPlatform;
      this.stepFree = stepFree;
      this.alternateNames = alternateNames; // 'String...' makes that parameter optional, resulting in 'null' if no value is passed
    }
    
    String getInfo() {
      return name + " does" + (stepFree ? " " : " not ")
        + "have step free access.\nIt is " + distanceToPlatform + "m from entrance to platform.";
    }
  }
  
  private static HashMap<String, Station> stations = new HashMap<String, Station>();
  
  public static void main(String[] args) {
    createStations();
    
    // The Program
    Scanner scanner = new Scanner(System.in);
    
    // Keep requesting input until receiving a valid number
    int num;
    for (;;) { // 'for (;;) ...' is effectively the same as 'while (true) ...'
      System.out.print("How many stations do you need to know about? ");
      String input = scanner.nextLine();
      
      try {
        num = Integer.parseInt(input);
        break;
      } catch (Exception exc) {
        // Do nothing
      }
      
      System.out.println("Please enter a correct number.");
    }
    
    for (int i = 0; i < num; ++i) {
      System.out.print("\nWhat station do you need to know about? ");
      String input = scanner.nextLine();
      
      // If available, show Station-information
      if (stations.containsKey(input.toLowerCase(Locale.ROOT))) {
        System.out.println(stations.get(input.toLowerCase(Locale.ROOT)).getInfo());
      } else {
        System.out.println("\"" + input + "\" is not a London Underground Station.");
      }
    }
    
    scanner.close(); // Close Scanner; Here actually not needed because program will be closed right after, freeing its resources anyway
  }
  
  private static void createStations() {
    // Add new Stations here to automatically add them to the HashMap
    Station[] stations = new Station[] {
      new Station("Stepney Green", 100, false),
      new Station("King's Cross", 700, true, "Kings Cross"),
      new Station("Oxford Circus", 200, true)
    };
    
    for (Station station : stations) {
      StationInformation.stations.put(station.name.toLowerCase(Locale.ROOT), station);
      
      // Alternative names will be mapped to the same Station
      if (station.alternateNames == null) continue;
      for (String altName : station.alternateNames)
        StationInformation.stations.put(altName.toLowerCase(Locale.ROOT), station);
    }
  }
}
Oskar Grosser
  • 2,804
  • 1
  • 7
  • 18
0

I have made Station class as static and returning a list from create_messages().

//this program tells the user whether a station is a step free access station or not and how far is it from the platform

import java.util.Scanner; // imports the scanner function to input data from the user
import java.util.ArrayList;
import java.util.List;

class StationInformation {
    public static void main(String[] args) // main method where methods are sequenced
    {
        int numberOfStations = inputint("how many stations do you want to know about?");
        String station;

        for (int i = 1; i <= numberOfStations; i++) {
            station = inputstring("what station do you want to know about?");
            search_station(station);

        }

        System.exit(0);
    }

// A method to input integers
    public static int inputint(String message) {
        Scanner scanner = new Scanner(System.in);
        int answer;

        System.out.println(message);
        answer = Integer.parseInt(scanner.nextLine());

        return answer;
    } // END inputInt

    public static String inputstring(String message) {
        Scanner scanner = new Scanner(System.in);
        String answer;

        System.out.println(message);
        answer = scanner.nextLine();

        return answer;
    }

    public static String create_message(Station station) {
        String message;
        if (station.step_free_access == true) {
            message = (station.name + "does have step free access. " + "it is " + station.distance_from_platform
                    + "m away from the entrance");
        } else {
            message = (station.name + "does not have step free access. " + "it is " + station.distance_from_platform
                    + "m away from the entrance");
        }
        return message;
    }

    public static List<String> create_messages() {
        Station mile_end = new StationInformation.Station();
        Station oxford_circus = new Station();
        Station kings_cross = new Station();
        Station stepney_green = new Station();

        mile_end.distance_from_platform = 50;
        mile_end.name = "Mile End ";
        mile_end.step_free_access = false;
        String message1 = create_message(mile_end);

        oxford_circus.distance_from_platform = 200;
        oxford_circus.name = " Oxford Circus ";
        oxford_circus.step_free_access = true;
        String message2 = create_message(oxford_circus);

        kings_cross.distance_from_platform = 700;
        kings_cross.name = " kings cross ";
        kings_cross.step_free_access = true;
        String message3 = create_message(kings_cross);

        stepney_green.distance_from_platform = 300;
        stepney_green.name = " Stepney Green ";
        stepney_green.step_free_access = false;
        String message4 = create_message(stepney_green);

        List<String> list = new ArrayList<>();
        list.add(message1);
        list.add(message2);
        list.add(message3);
        list.add(message4);
        return list;
    }

    public static void search_station(String station) {

        List<String> list = create_messages();
        String mileEndMessage = list.get(0);
        String oxfordCircusMessage = list.get(1);
        String kingsCrossMessage = list.get(2);
        String stepneyGreenMessage = list.get(3);

        if (station.equals("Mile End")) {
            System.out.println(mileEndMessage);
        } else if (station.equals("kings cross")) {
            System.out.println(kingsCrossMessage);
        } else if (station.equals("oxford circus")) {
            System.out.println(oxfordCircusMessage);
        } else if (station.equals("stepney green")) {
            System.out.println(stepneyGreenMessage);
        } else {
            System.out.println(station + " is not a London underground station ");
        }

    }

    static class Station // a record to store information about stations
    {
        int distance_from_platform;
        String name;
        boolean step_free_access;
    }

}