Link to home
Start Free TrialLog in
Avatar of RamsTheDon
RamsTheDon

asked on

Distance formula

Hey guys!

I've been working really hard lately, and my programme is almost complete 'm a little bit stuck. My programme allows users to enter town names which are all in a text file but should also then compile and the distance.This is where i'm having problems , can someone please advise on the code that i'm missing.
The programme is as below.

Thank you!

import java.io.FileReader;
import java.io.BufferedReader;
import java.lang.*;
import java.io.*;


public class TownsInfo extends Distances
{

    public static void main(String[] args){

      String town = "";
      String Origin;
      String Destination;
      float gridEast1 = 0;
      float gridEast2 = 0;
      float gridNorth1 = 0;
      float gridNorth2 = 0;
            InputStreamReader stdin =
                new InputStreamReader(System.in);
        BufferedReader  console =
        new BufferedReader(stdin);

    {
   
         
    double Distance;
    Distance= (Math.pow((gridEast1-gridEast2),2) + Math.pow((gridNorth1-gridNorth2),2));
    }

        try
        {
          System.out.print("Enter Origin Town (IN CAPS):");
          town = console.readLine();
          System.out.print("Enter Destination Town (IN CAPS):");
          town = console.readLine();
          System.out.println("Distance in KM is" + Distance);




        }
            catch (IOException ioex)
        {
            System.out.println("IO error");
            System.exit(1);

            }


    }

}

class Distances
{
      public static void gettowninfo(String s)
      {

                  boolean      gotit = false;
      //System.out.println("Looking for " + s);
          try
        {
                FileReader file = new FileReader("towns.txt");
            BufferedReader datafile =
                  new BufferedReader(file);
            String town = s;
            try
            {


                  String line;
                  while(( line = datafile.readLine())!=null)
                  {

                        String f[] = line.split(":");
                        if(f[0].equals(town))
                        {
                              //System.out.println("Town Found");
                              //System.out.println(line);
                              gotit = true;
                        }


                  }
            }
            catch(IOException ioex)
            {
                  System.out.println("IO error");
                  System.exit(1);
            }
      }
      catch(FileNotFoundException fnex)
      {
            System.out.println("Couldn't find file");

      }
      if(!gotit)      System.out.println("not found");
    }

}





SOLUTION
Avatar of Harisha M G
Harisha M G
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of RamsTheDon
RamsTheDon

ASKER

Thanks guys , but what i'm really craving for is a piece of code to instruct  the programme to work out the distance based on the txt file contents. Refer to the copied and pasted dos output. The distance is comimg out as 0.0.
The format for each  town in the txt file is as COLTON:SD:190020:-6900:405:320 190020 latitude, 6900 longitude, 405 km grid east coord, 320 km grid north coord.

(current output)

Enter Origin town (IN CAPS):
PARIS
Enter Destination Town (IN CAPS):
COLOGNE
Distance in KM is:0.0


Thanks.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I accept your criticisms basicinstinct , i admit i'm no java expert i'm still learning. I think your comments have been a little too hard and not encouraging enough.
Contrary to what you said this programme is working and my only problem at the moment is that it's not calculating the distance between cities, which is all i needed assistance on.

Thank you.

dude - sorry if i seemed harsh, i was only trying to be honest

i disagree that the program is working.  to prove it, try removing the file 'towns.txt' from your disk (or just rename it) - you will see that it makes no difference to your program (because it never reads the file) - you will not get any errors

or here's another way to test it, try typing in the names of cities that aren't listed in towns.txt - again, there won't be any problem because the program never checks the file

keep going, you've made a good start
hey basic
Well i've realised that i erronously attached a wrong programme. You are right ,the one i attached does not read the file. Thanks for spotting the error.
Here's the right one.

Thanks

 p/s it's still incomplete but working , The only trouble is calculating the distance and time.



import java.io.FileReader;
 import java.io.BufferedReader;
 import java.lang.*;
 import java.io.*;



 public class TownsInfo extends Distances
 {



 public static void main(String[] args){



 String town = "";
 String From_town;
 String To_town;
 float gridEast1 = 0;
 float gridEast2 = 0;
 float gridNorth1 = 0;
 float gridNorth2 = 0;
 float time = 0;
 double Distance;
 double speed = 0;
 
 

 InputStreamReader stdin =
 new InputStreamReader(System.in);
 BufferedReader console =
 new BufferedReader(stdin);


 
 //Methods
 Distance= Math.sqrt((Math.pow((gridEast1-gridEast2),2) + Math.pow((gridNorth1-gridNorth2),2)));
 speed = Distance/time;
 time = Distance/speed;


            


 try
 {
 System.out.println("Enter From town (IN CAPS):");
 town = console.readLine();


 Distances.gettowninfo(town);
 System.out.println("Enter To Town (IN CAPS):");
 town = console.readLine();


 Distances.gettowninfo(town);
 System.out.println("Distance in KM is:" + Distance);
 //Distance = Double.parseDouble(console.readLine());
 System.out.println("Enter speed In KM/H:");
 speed = Double.parseDouble(console.readLine());
 System.out.println("Approximate time of travel is:" + time );


 if(speed < 30)
 System.out.println("too slow");
 //if(speed = < 120)
 //System.out.println("Speed within range");      
 if (speed>120)
 System.out.println("Danger (!) Exceeds national speed limit");

 


 }
 catch (IOException ioex)
 {
 System.out.println("IO error");
 System.exit(1);

 }
 Distances.gettowninfo(town);

 }

 }

 class Distances
 {



   public static void gettowninfo(String s)
   {



       boolean  gotit = false;
   //System.out.println("Looking for " + s);
 try
    {
   FileReader file = new FileReader("towns.txt");
     BufferedReader datafile =
       new BufferedReader(file);
     String town = s;
     double latitude;
     double longitude;
     int gridEast;
     int gridNorth;
     try
     {

      
       String line;
       while(( line = datafile.readLine())!=null)
       {

         String f[] = line.split(":");
         if(f[0].equals(town))
         {

       //System.out.println(f[0]);  
        // System.out.println(f[1]);  
              latitude = Double.parseDouble(f[2]);
       longitude = Double.parseDouble(f[3]);
       gridEast = Integer.parseInt(f[4]);
         gridNorth = Integer.parseInt(f[5]);
      
      
   
      
      
      

           //System.out.println("Town Found");
           //System.out.println(line);
           gotit = true;
         }


       }
     }
     catch(IOException ioex)
     {
       System.out.println("IO error");
       System.exit(1);
     }
   }
   catch(FileNotFoundException fnex)
   {
     System.out.println("Couldn't find file");

   }
   if(!gotit)  System.out.println("Town not found //please enter correct spelling and in CAPS");
 }

 }