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");
    }

}





RamsTheDonAsked:
Who is Participating?
 
basicinstinctCommented:
there is so much wrong with your code...

You do need to get rid of those extra brackets, like StillUnaware says, but that is the least of the problems.

But the MAIN problem is that you never set the values from the text file.  Here's a simplified pseudocode of what you do:

1. Establish all of the variables to default values
2. Perform a calculation using variables (which are all at default value, ie they are all zero - your calculation is pointless - and at this point you don't know what towns the user is going to enter, so it is doubly pointless)
3. Obtain input from user and store in 'town'
4. Obtain more input from user and store in 'town' (overwriting the value you obtained before)
5. Display the results of the calculation.

-You never read the values from the text file, even if you did not have the code present to use the values from the text file.  

The code you have here is:
1. Totally wrong and confused
2. Massively incomplete

To make your code work would require a 90% rewrite.
I suggest you sit down and think about it some more.

Sorry to be the bearer of bad news.
0
 
Harisha M GCommented:
Hi, you need to find the square root of the sum of squares. So,

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


---
Harish
0
 
StillUnAwareCommented:
You should remove extra brackets. Insted of this peace of code:

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

leave only the two meaningfull lines:

    double Distance;
    Distance= (Math.pow((gridEast1-gridEast2),2) + Math.pow((gridNorth1-gridNorth2),2));
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
RamsTheDonAuthor Commented:
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.
0
 
RamsTheDonAuthor Commented:
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.

0
 
basicinstinctCommented:
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
0
 
RamsTheDonAuthor Commented:
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");
 }

 }

 
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.