well basically i have this code working to display distance to nearest station and the station name but i want it to show what line the station nearest is located on i've tried to figure it out but just dont seem to be getting anywhere with it the code (including what I tried to do to get it to work) is below

any help would be much appreciated

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package calculatedistance;
import javax.swing.*;


/**
 *
 * @author Jade
 */
public class CalculateDistance {
    private static int i;
   

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
       
        double tempBearing;
        double tempLength;
        double distanceToClosest;
               
        // array of stations
        Station[]stations = new Station[16];
        
        //populate array list
        stations[0] = new Station("Trim Bridge", new Location(65.0,2600.0));
        stations[1] = new Station("Windmill Hill", new Location(65.0,4800.0));
        stations[2] = new Station("Pirac Cresent", new Location(82.0,3400.0));
        stations[3] = new Station("Easton", new Location(84.0,4800.0));
        stations[4] = new Station("Parkway", new Location(105.0,3400.0));
        stations[5] = new Station("Temple Fields", new Location(149.0,3000.0));
        stations[6] = new Station("St Judes Hill", new Location(344.0,1800.0));
        stations[7] = new Station("Clifton Street", new Location(287.0,4400.0));
        stations[8] = new Station("Tivoli", new Location(277.0,2200.0));
        stations[9] = new Station("Jacobs Well", new Location(251.0,1200.0));
        stations[10] = new Station("Central", new Location(240.0,2000.0));
        stations[11] = new Station("Newbridge", new Location(244.0,4400.0));
        stations[12] = new Station("Weston-On-Shore", new Location(235.0,5800.0));
        stations[13] = new Station("Shakespeare Court", new Location(183.0,2000.0));
        stations[14] = new Station("St Dennis", new Location(180.0,400.0));
        stations[15] = new Station("Maxbridge", new Location(180.0,800.0));
        
        //array of station lines
        Line[]lines = new Line[4];
        
        //populate array list
        lines[0] = new Line("Docks Line");
        lines[1]= new Line("Brunel Line");
        lines[2]= new Line("Gyratory Line");
        lines[3]= new Line("Interchange");
        
        //get current location
        String temp = JOptionPane.showInputDialog("Bearing to current location");
        tempBearing = Double.parseDouble(temp);
        
        temp= JOptionPane.showInputDialog("Distance from origin of current location");
        tempLength = Double.parseDouble(temp);
        
        Location current = new Location(tempBearing, tempLength);
        
        Station closest = stations[0];
        distanceToClosest = current.calcDistance(stations[0].location);
        
        
        
        for(int i=1; i <stations.length; i++){
            double tempDist = current.calcDistance(stations[i].location);
            
            if (tempDist<distanceToClosest){
                closest = stations[i];
                distanceToClosest=tempDist;
            }// if (tempDist<distanceToClosest)
        }//for loop
        
               
        if (closest.name= "Weston-On-Shore" or "Newbridge" or "Central" or "St Dennis"  or "Winmill Hill");
                Lines = "Docks Line";
                else if (closest.name = "Clifton Street" or "Tivoli" or"Easton");
                Lines = "Brunel Line";
                else if (closest.name = "Shakespeare Court" or "Temple Fields" or "Pirac Crescent" or "St Judes Hill")
                Lines = "Gyratory Line";
                
        
        

      
        JOptionPane.showMessageDialog(null, "the closest station is "+closest.name + "\nIt is " +distanceToClosest + " m away  on a bearing of");
       
    }
}
class Station{
    Location location;
    String name;
    
    public Station (String n, Location l){
        name = n;
        location = l;
    }
}//class station

class Location{
    double bearing;
    double length;
      
    public Location(){
        bearing = 0.0;
        length = 0.0;
    }
    public Location (double b, double l){
        bearing = b;
        length = l;
    }
    public double calcDistance(Location l){
        double angle = Math.abs(bearing - l.bearing); // calculate angle between bearings
        double temp = (length*length)+(l.length*l.length)-(2*length*l.length*Math.cos(Math.toRadians(angle)));
        return Math.pow(temp, 0.5);
    }
          
   
}//location

well basically i have this code working to display distance to nearest station and the station name but i want it to show what line the station nearest is located on i've tried to figure it out but just dont seem to be getting anywhere with it the code (including what I tried to do to get it to work) is below

any help would be much appreciated

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package calculatedistance;
import javax.swing.*;


/**
 *
 * @author Jade
 */
public class CalculateDistance {
    private static int i;
   

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
       
        double tempBearing;
        double tempLength;
        double distanceToClosest;
               
        // array of stations
        Station[]stations = new Station[16];
        
        //populate array list
        stations[0] = new Station("Trim Bridge", new Location(65.0,2600.0));
        stations[1] = new Station("Windmill Hill", new Location(65.0,4800.0));
        stations[2] = new Station("Pirac Cresent", new Location(82.0,3400.0));
        stations[3] = new Station("Easton", new Location(84.0,4800.0));
        stations[4] = new Station("Parkway", new Location(105.0,3400.0));
        stations[5] = new Station("Temple Fields", new Location(149.0,3000.0));
        stations[6] = new Station("St Judes Hill", new Location(344.0,1800.0));
        stations[7] = new Station("Clifton Street", new Location(287.0,4400.0));
        stations[8] = new Station("Tivoli", new Location(277.0,2200.0));
        stations[9] = new Station("Jacobs Well", new Location(251.0,1200.0));
        stations[10] = new Station("Central", new Location(240.0,2000.0));
        stations[11] = new Station("Newbridge", new Location(244.0,4400.0));
        stations[12] = new Station("Weston-On-Shore", new Location(235.0,5800.0));
        stations[13] = new Station("Shakespeare Court", new Location(183.0,2000.0));
        stations[14] = new Station("St Dennis", new Location(180.0,400.0));
        stations[15] = new Station("Maxbridge", new Location(180.0,800.0));
        
        //array of station lines
        Line[]lines = new Line[4];
        
        //populate array list
        lines[0] = new Line("Docks Line");
        lines[1]= new Line("Brunel Line");
        lines[2]= new Line("Gyratory Line");
        lines[3]= new Line("Interchange");
        
        //get current location
        String temp = JOptionPane.showInputDialog("Bearing to current location");
        tempBearing = Double.parseDouble(temp);
        
        temp= JOptionPane.showInputDialog("Distance from origin of current location");
        tempLength = Double.parseDouble(temp);
        
        Location current = new Location(tempBearing, tempLength);
        
        Station closest = stations[0];
        distanceToClosest = current.calcDistance(stations[0].location);
        
        
        
        for(int i=1; i <stations.length; i++){
            double tempDist = current.calcDistance(stations[i].location);
            
            if (tempDist<distanceToClosest){
                closest = stations[i];
                distanceToClosest=tempDist;
            }// if (tempDist<distanceToClosest)
        }//for loop
        
               
        if (closest.name= "Weston-On-Shore" or "Newbridge" or "Central" or "St Dennis"  or "Winmill Hill");
                Lines = "Docks Line";
                else if (closest.name = "Clifton Street" or "Tivoli" or"Easton");
                Lines = "Brunel Line";
                else if (closest.name = "Shakespeare Court" or "Temple Fields" or "Pirac Crescent" or "St Judes Hill")
                Lines = "Gyratory Line";
                
        
        

      
        JOptionPane.showMessageDialog(null, "the closest station is "+closest.name + "\nIt is " +distanceToClosest + " m away  on a bearing of");
       
    }
}
class Station{
    Location location;
    String name;
    
    public Station (String n, Location l){
        name = n;
        location = l;
    }
}//class station

class Location{
    double bearing;
    double length;
      
    public Location(){
        bearing = 0.0;
        length = 0.0;
    }
    public Location (double b, double l){
        bearing = b;
        length = l;
    }
    public double calcDistance(Location l){
        double angle = Math.abs(bearing - l.bearing); // calculate angle between bearings
        double temp = (length*length)+(l.length*l.length)-(2*length*l.length*Math.cos(Math.toRadians(angle)));
        return Math.pow(temp, 0.5);
    }
          
   
}//location

I dont know if this is the only error but look here

if (closest.name= "Weston-On-Shore" or "Newbridge" or "Central" or "St Dennis"  or "Winmill Hill");

an if statement should not have a ';' at the end here is the structure of an if statement:

if (1 == 2) {
            //do something
        } else if (1 == 2) {
            //do something
        } else {
            //do something
        }

also '=' is for assignment and '==' Compares references, not values, for a string we use equals() method in java as it Compares values for equality. check this for more info:http://www.leepoint.net/notes-java/data/expressions/22compareobjects.html

to check the equality of a String you should use

if (closest.equals("Weston-On-Shore")) {}

also there is no 'or' statement in java use the '||' as an or operator i.e

if (closest.equals( "Weston-On-Shore" )||closest.equals("Newbridge")) {}

and as for this 'closest.name' why are you using that there is no such variable as that and there can never be...

use this intsead:

closest

as thats what you assign the stations name too as seen here

closest = stations[i];

also there is no 'or' statement in java use the '||' as an or operator i.e

if (closest.name= "Weston-On-Shore" ||closest.name.equals("Newbridge" or "Central")) {}

you made an obvious error here... "Newbridge" or "Central" :icon_wink:

you made an obvious error here... "Newbridge" or "Central" :icon_wink:

Ooops sorry i edited it, thats what happens when you just copy and paste OP's code and have no syntax highlighting lol :). my mistake

I dont know if this is the only error but look here

if (closest.name= "Weston-On-Shore" or "Newbridge" or "Central" or "St Dennis"  or "Winmill Hill");

an if statement should not have a ';' at the end here is the structure of an if statement:

if (1 == 2) {
            //do something
        } else if (1 == 2) {
            //do something
        } else {
            //do something
        }

also '=' is for assignment and '==' Compares references, not values, for a string we use equals() method in java as it Compares values for equality. check this for more info:http://www.leepoint.net/notes-java/data/expressions/22compareobjects.html

to check the equality of a String you should use

if (closest.equals("Weston-On-Shore")) {}

also there is no 'or' statement in java use the '||' as an or operator i.e

if (closest.equals( "Weston-On-Shore" )||closest.equals("Newbridge")) {}

and as for this 'closest.name' why are you using that there is no such variable as that and there can never be...

use this intsead:

closest

as thats what you assign the stations name too as seen here

closest = stations[i];

adding to my original help for you, look over here:

//array of station lines
        Line[] lines = new Line[4];
        
        //populate array list
        lines[0] = new Line("Docks Line");

uhm Line is a sound sample, which i doubt whether you are trying to create that maybe you should use a string array ? because its a string you are assigning like this:

//array of station lines
       String[] lines = new String[4];
        
        //populate array list
        lines[0] = "Docks Line";

i would also suggest making your string array global and changing the case of your

Lines = "Brunel Line";

because you declared it as lines and not Lines!

ok so i kind of understand what everyones saying and have made some changes but i still cant quite understand what im doing heres the code with bits altered

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package calculatedistance;
import javax.swing.*;


/**
 *
 * @author Jade
 */
public class CalculateDistance {
    private static int i;
   

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
       
        double tempBearing;
        double tempLength;
        double distanceToClosest;
               
        // array of stations
        Station[]stations = new Station[16];
        
        //populate array list
        stations[0] = new Station("Trim Bridge", new Location(65.0,2600.0));
        stations[1] = new Station("Windmill Hill", new Location(65.0,4800.0));
        stations[2] = new Station("Pirac Cresent", new Location(82.0,3400.0));
        stations[3] = new Station("Easton", new Location(84.0,4800.0));
        stations[4] = new Station("Parkway", new Location(105.0,3400.0));
        stations[5] = new Station("Temple Fields", new Location(149.0,3000.0));
        stations[6] = new Station("St Judes Hill", new Location(344.0,1800.0));
        stations[7] = new Station("Clifton Street", new Location(287.0,4400.0));
        stations[8] = new Station("Tivoli", new Location(277.0,2200.0));
        stations[9] = new Station("Jacobs Well", new Location(251.0,1200.0));
        stations[10] = new Station("Central", new Location(240.0,2000.0));
        stations[11] = new Station("Newbridge", new Location(244.0,4400.0));
        stations[12] = new Station("Weston-On-Shore", new Location(235.0,5800.0));
        stations[13] = new Station("Shakespeare Court", new Location(183.0,2000.0));
        stations[14] = new Station("St Dennis", new Location(180.0,400.0));
        stations[15] = new Station("Maxbridge", new Location(180.0,800.0));
        
        //array of station lines
        String[] lines = new String[4];
        
        //populate array list
        lines[0] = "Docks Line";
        lines[1]= "Brunel Line";
        lines[2]= "Gyratory Line";
        lines[3]= "Interchange";
        
        //get current location
        String temp = JOptionPane.showInputDialog("Bearing to current location");
        tempBearing = Double.parseDouble(temp);
        
        temp= JOptionPane.showInputDialog("Distance from origin of current location");
        tempLength = Double.parseDouble(temp);
        
        Location current = new Location(tempBearing, tempLength);
        
        Station closest = stations[0];
        distanceToClosest = current.calcDistance(stations[0].location);
        
        
        
        for(int i=1; i <stations.length; i++){
            double tempDist = current.calcDistance(stations[i].location);
            
            if (tempDist<distanceToClosest){
                closest = stations[i];
                distanceToClosest=tempDist;
            }// if (tempDist<distanceToClosest)
        }//for loop
        
               
        if closest.name.equals ("Weston-On-Shore") ||closest.name.equals("Newbridge") ||closest.name.equals("Central")||closest.name.equals("St Dennis")||closest.name.equals("Winmill Hill"){
                Lines = "Docks Line";
                else if (closest.name.equals("Clifton Street")||closest.name.equals("Tivoli")||closest.name.equals("Easton"){
                Lines = "Brunel Line";
                else if (closest.name.equals("Shakespeare Court")||closest.name.equals("Temple Fields")||closest.name.equals ("Pirac Crescent") ||closest.name.equals ("St Judes Hill"){
                Lines = "Gyratory Line";
                
               
        

      
        JOptionPane.showMessageDialog(null, "the closest station is "+closest.name + "\nIt is " +distanceToClosest + " m away  on a bearing of");
       
    }
}
class Station{
    Location location;
    String name;
    
    public Station (String n, Location l){
        name = n;
        location = l;
    }
}//class station

class Location{
    double bearing;
    double length;
      
    public Location(){
        bearing = 0.0;
        length = 0.0;
    }
    public Location (double b, double l){
        bearing = b;
        length = l;
    }
    public double calcDistance(Location l){
        double angle = Math.abs(bearing - l.bearing); // calculate angle between bearings
        double temp = (length*length)+(l.length*l.length)-(2*length*l.length*Math.cos(Math.toRadians(angle)));
        return Math.pow(temp, 0.5);
    }
          
   
}//location

ok so i kind of understand what everyones saying and have made some changes but i still cant quite understand what im doing heres the code with bits altered

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package calculatedistance;
import javax.swing.*;


/**
 *
 * @author Jade
 */
public class CalculateDistance {
    private static int i;
   

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
       
        double tempBearing;
        double tempLength;
        double distanceToClosest;
               
        // array of stations
        Station[]stations = new Station[16];
        
        //populate array list
        stations[0] = new Station("Trim Bridge", new Location(65.0,2600.0));
        stations[1] = new Station("Windmill Hill", new Location(65.0,4800.0));
        stations[2] = new Station("Pirac Cresent", new Location(82.0,3400.0));
        stations[3] = new Station("Easton", new Location(84.0,4800.0));
        stations[4] = new Station("Parkway", new Location(105.0,3400.0));
        stations[5] = new Station("Temple Fields", new Location(149.0,3000.0));
        stations[6] = new Station("St Judes Hill", new Location(344.0,1800.0));
        stations[7] = new Station("Clifton Street", new Location(287.0,4400.0));
        stations[8] = new Station("Tivoli", new Location(277.0,2200.0));
        stations[9] = new Station("Jacobs Well", new Location(251.0,1200.0));
        stations[10] = new Station("Central", new Location(240.0,2000.0));
        stations[11] = new Station("Newbridge", new Location(244.0,4400.0));
        stations[12] = new Station("Weston-On-Shore", new Location(235.0,5800.0));
        stations[13] = new Station("Shakespeare Court", new Location(183.0,2000.0));
        stations[14] = new Station("St Dennis", new Location(180.0,400.0));
        stations[15] = new Station("Maxbridge", new Location(180.0,800.0));
        
        //array of station lines
        String[] lines = new String[4];
        
        //populate array list
        lines[0] = "Docks Line";
        lines[1]= "Brunel Line";
        lines[2]= "Gyratory Line";
        lines[3]= "Interchange";
        
        //get current location
        String temp = JOptionPane.showInputDialog("Bearing to current location");
        tempBearing = Double.parseDouble(temp);
        
        temp= JOptionPane.showInputDialog("Distance from origin of current location");
        tempLength = Double.parseDouble(temp);
        
        Location current = new Location(tempBearing, tempLength);
        
        Station closest = stations[0];
        distanceToClosest = current.calcDistance(stations[0].location);
        
        
        
        for(int i=1; i <stations.length; i++){
            double tempDist = current.calcDistance(stations[i].location);
            
            if (tempDist<distanceToClosest){
                closest = stations[i];
                distanceToClosest=tempDist;
            }// if (tempDist<distanceToClosest)
        }//for loop
        
               
        if closest.name.equals ("Weston-On-Shore") ||closest.name.equals("Newbridge") ||closest.name.equals("Central")||closest.name.equals("St Dennis")||closest.name.equals("Winmill Hill"){
                Lines = "Docks Line";
                else if (closest.name.equals("Clifton Street")||closest.name.equals("Tivoli")||closest.name.equals("Easton"){
                Lines = "Brunel Line";
                else if (closest.name.equals("Shakespeare Court")||closest.name.equals("Temple Fields")||closest.name.equals ("Pirac Crescent") ||closest.name.equals ("St Judes Hill"){
                Lines = "Gyratory Line";
                
               
        

      
        JOptionPane.showMessageDialog(null, "the closest station is "+closest.name + "\nIt is " +distanceToClosest + " m away  on a bearing of");
       
    }
}
class Station{
    Location location;
    String name;
    
    public Station (String n, Location l){
        name = n;
        location = l;
    }
}//class station

class Location{
    double bearing;
    double length;
      
    public Location(){
        bearing = 0.0;
        length = 0.0;
    }
    public Location (double b, double l){
        bearing = b;
        length = l;
    }
    public double calcDistance(Location l){
        double angle = Math.abs(bearing - l.bearing); // calculate angle between bearings
        double temp = (length*length)+(l.length*l.length)-(2*length*l.length*Math.cos(Math.toRadians(angle)));
        return Math.pow(temp, 0.5);
    }
          
   
}//location

why are you still using 'closest.name' i told you use 'closest' as thats what you assigned it to! also you have now completely messed up your brackets in your if statements.

edit here i will only help you with this one thing here is your fixed if statement and how it should look:

if (closest.equals ("Weston-On-Shore") ||closest.equals("Newbridge") ||closest.equals("Central")||closest.equals("St Dennis")||closest.equals("Winmill Hill")){
                lines = "Docks Line";
        } else if (//other names) {
        } else {//it didnt match any of the above so it must be ....
        }
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.