For the program I have the user entering their current distance and bearing from specified point with the programme then returning the nearest station and bearing/distance to this station.

However I also want to include what line the station is situated so I know the code will be something along then lines of

if (closest.name.equal("Wesston-On-Shore"));
return ("Docks Line")

not sure if this is correct plus how you 'name' it to recall in the display for the user any help would be appreciated

Dani AI

Generated

A clean solution is to model stations as objects that carry name, coordinates and the line(s) they serve, have the geometric code return the Station object, and let the UI print the station name and its lines. That follows ’s suggestion to separate calculation and display and avoids long if/return chains. Also heed ’s syntax notes (no stray semicolons after if, use String.equals(...) correctly). later confirmed a different approach fixed the problem.

Example structure (conceptual):

class Station {
  private final String name;
  private final double lat, lon;
  private final List<String> lines;

  public Station(String name, double lat, double lon, List<String> lines) {
    this.name = name;
    this.lat = lat;
    this.lon = lon;
    this.lines = new ArrayList<>(lines);
  }

  public String getName() { return name; }
  public List<String> getLines() { return Collections.unmodifiableList(lines); }

  public double distanceTo(double lat2, double lon2) {
    // use haversine or appropriate distance formula
    return haversine(lat, lon, lat2, lon2);
  }
}
static Station findClosest(double lat, double lon, List<Station> stations) {
  Station best = null;
  double bestDist = Double.POSITIVE_INFINITY;
  for (Station s : stations) {
    double d = s.distanceTo(lat, lon);
    if (d < bestDist) { bestDist = d; best = s; }
  }
  return best;
}

// usage:
Station nearest = findClosest(userLat, userLon, stations);
if (nearest != null) {
  System.out.println(nearest.getName() + " - lines: " + nearest.getLines());
}

Tips: trim and normalize user input before comparing (use equalsIgnoreCase if appropriate), guard against null, store multiple lines as a List<String> or an EnumSet if the set is fixed, and consider a spatial index (quadtree/KD-tree) if the station list is large. See the Java class/how-to docs for patterns and String.equals() behavior: Java OO tutorial String.equals() docs.

Recommended Answers

All 6 Replies

You could create a station class that stored the line (or lines) which it services. Depending on how your lines are managed you could either store its name in a string or a reference to the appropriate line class instance. Your calculation function could then determine which station is closest and return that string or reference.

A good rule of thumb is to separate your calculation and display code. This makes it much easier to write different GUIs and command line tools for your code if the need ever arises. It would probably work best to store a string for each station (either in an array or in a class, depending how complex your project is) and just print that string when you've determined the appropriate station.

if (closest.name.equal("Wesston-On-Shore"));
return ("Docks Line")

not sure if this is correct plus how you 'name' it to recall in the display for the user any help would be appreciated

well.. what is your method supposed to return, if anything?
a few mistakes in the above lines anyway:
don't put a ';' after your if statement itself:
there are 2 ways for an if statement to 'complete' it's conditional code:
1. the if statement is followed by brackets ({})
the conditional code runs untill the closing bracket of that block
2. if you don't use brackets, or you place your ';' on the wrong place: the conditional part of your if-structure ends as soon as the first ';' is found.
the return statement above will automatically execute, whether the if statement returns true or not.

also, if it's a String object you're returning, you could as easily code:
return "Docks line";
(and here you should place an ';' :) )

ok so I need to firstly

create a lines class, then create the if statement?

I think I understand what to do but am not 100% and yes it is a string i'm returning, well trying to, if anyone could give an example of the code I need so I can see it for myself it would be much appreciated

I would second the opinion from stultuske.

if anyone could give an example of the code I need so I can see it for myself it would be much appreciated

we're not really here to provide you with code, we're just helping you find and fix your bugs.

and I must say, it would be a lot easier to do so, if you explained exactly what this 'line' is that you're trying to return (what does it have to do, what does it represent ...) and the complete method you are working in, not just bits and pieces that leave information missing.

I've managed to solve my problem now I approached it differently and managed to create a solution to my problem thanks for the help guys

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.