?
Solved

Distance formula

Posted on 2006-05-03
7
Medium Priority
?
1,148 Views
Last Modified: 2012-05-05
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");
    }

}





0
Comment
Question by:RamsTheDon
7 Comments
 
LVL 37

Assisted Solution

by:Harisha M G
Harisha M G earned 100 total points
ID: 16599613
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
 
LVL 14

Assisted Solution

by:StillUnAware
StillUnAware earned 100 total points
ID: 16599628
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
 

Author Comment

by:RamsTheDon
ID: 16600450
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 23

Accepted Solution

by:
basicinstinct earned 300 total points
ID: 16602027
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
 

Author Comment

by:RamsTheDon
ID: 16621212
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
 
LVL 23

Expert Comment

by:basicinstinct
ID: 16621490
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
 

Author Comment

by:RamsTheDon
ID: 16621812
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

Featured Post

[Webinar] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
The SignAloud Glove is capable of translating American Sign Language signs into text and audio.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
Starting up a Project

569 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question