Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 23 of 23

Thread: Distance

  1. #1
    Member
    Join Date
    Aug 2013
    Location
    Reykjavik, Iceland
    Posts
    51
    Thanks
    37
    Thanked 0 Times in 0 Posts

    Default Distance

    Hi

    I'm trying to write a program that calculates the the length of two vectors of length N that are represented with one dimensional arrays and compute the Euclidean distance between them.

    Any hints?

     
    public class MissionImpossible{
     
     
     public static double main(String[] args)
     {
     int N = Integer.parseInt(args[0]);
     int[] a = new int[N];
     int[] b = new int[N];
     
     for (int i = 0; i < N;)
     {
     double Sum=0.0;
     
     Sum= Sum+Math.pow((a[i]-b[i]) ,2); 
     return Math.sqrt(Sum);
     }
    // return Math.sqrt(Sum);
    return N;
     
     
     }
    }


    --- Update ---

    I get the erorr Main method must return a value of type void in class Ed, please define the main method as public void main....


  2. #2
    Member
    Join Date
    Sep 2013
    Posts
    70
    Thanks
    1
    Thanked 13 Times in 13 Posts

    Default Re: Distance

    your main method cannot return a value. The compiler is telling you this clearly. If you must have this in a method create a method outside of main. Call it whatever you want and put the code in there. From the main method you call the method and it executes the code.

  3. The Following User Says Thank You to Ubiquitous For This Useful Post:

    einar123 (September 20th, 2013)

  4. #3
    Member
    Join Date
    Aug 2013
    Location
    Reykjavik, Iceland
    Posts
    51
    Thanks
    37
    Thanked 0 Times in 0 Posts

    Default Re: Distance

    Where? inside the loop? ...it seems to be a bigger problem... Now I've got an illegal start of expression

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Distance

    got an illegal start of expression
    Copy the full text of the error message and paste it here.

    --- Update ---

    Also post the new code that has the error.
    If you don't understand my answer, don't ignore it, ask a question.

  6. The Following User Says Thank You to Norm For This Useful Post:

    einar123 (September 21st, 2013)

  7. #5
    Member
    Join Date
    Aug 2013
    Location
    Reykjavik, Iceland
    Posts
    51
    Thanks
    37
    Thanked 0 Times in 0 Posts

    Default Re: Distance

    Now I got this:

     
    public class Ed{
     
     
     public static int main(String[] args)
     {
      int N = Integer.parseInt(args[0]);
      int[] a = new int[N];
      int[] b = new int[N];
     
      for (int i = 0; i < N;i++)
      {
       double Sum=0.0;
       Sum= Sum+Math.pow((a[i]-b[i]) ,2); 
       return (int) (Math.sqrt(Sum));}
     
          }
     
     public static double main1(String[] args){
     
     return (int) Math.sqrt(Sum);
     }
    }


    Error message:
    [line: 22]
    Error: cannot find symbol
    symbol: variable Sum
    location: class Ed

  8. #6
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Distance

    This program has other problems, likely more significant than the one related to the error you posted, but here's why you've gotten that error:

    Sum, which by Java naming convention should be called 'sum', is declared inside the 'for' loop so is invisible, or out of scope, when used in the method main1().

    You haven't correctly addressed Ubiquitous' original point. Now you have no static void main() method that can be used as the entry point to run this class.

  9. #7
    Member
    Join Date
    Aug 2013
    Location
    Reykjavik, Iceland
    Posts
    51
    Thanks
    37
    Thanked 0 Times in 0 Posts

    Default Re: Distance

    Now I'm trying to simplify this. I don't know how to call a method outside of main.

    public class Verkefni143
    {
    	public static void main(String[] args)
    	{
    	int N = Integer.parseInt(args[0]);
    	int[] a = new int[N];
    	int[] b = new int[N];
    	double elucidean_distance = 0.0;
    	for (int i = 0; i < N; i++)
    	{
    	distance = a[i] - b[i];
    	edean_distance = Math.sqrt(distance);
    	}
    	}
    	System.out println(edean_distance);
    }

    Error: line 15 <identifier> expected

  10. #8
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Distance

    I don't know how to call a method outside of main.
    Of course you do:

    int N = Integer.parseInt(args[0]);
    edean_distance = Math.sqrt(distance);

    are both calling methods outside main(). Those calls are to static methods in other classes, so calling a Verkefni143 method from your static main() is similar. If the method is call getDistance() with the signature

    public static double getDistance( double pointOne, double pointTwo )

    then the call from main() might be:

    double distance = getDistance( a[i], b[i] );

  11. The Following User Says Thank You to GregBrannon For This Useful Post:

    einar123 (September 21st, 2013)

  12. #9
    Member
    Join Date
    Aug 2013
    Location
    Reykjavik, Iceland
    Posts
    51
    Thanks
    37
    Thanked 0 Times in 0 Posts

    Default Re: Distance

    Men.....hahahah you are killing me. I don't get it!

  13. #10
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Distance

    I'm not sure what you're not getting (be specific), but here's a simple runnable example of what I described. You should be able to find similar examples in your text book, class notes, or online, but here's your very own:
    // a simple class to demonstrate calling another method from
    // the main() method and using/displaying the result
    public class TestClass2 
    {
        public static void main( String[] args )
        {
            // assigns the value returned by the method getDistance()
            // to the variable distance
            double distance = getDistance( 5, 25 );
     
            // displays the resulting distance
            System.out.println( "The distance is: " + distance );
     
        } // end method main()
     
        // a trivial method to return a value calculated from the
        // two parameters, point1 and point2
        public static double getDistance( double point1, double point2 )
        {
            return point2 - point1;
        }
     
    } // end class TestClass2

  14. The Following User Says Thank You to GregBrannon For This Useful Post:

    einar123 (September 21st, 2013)

  15. #11
    Member
    Join Date
    Aug 2013
    Location
    Reykjavik, Iceland
    Posts
    51
    Thanks
    37
    Thanked 0 Times in 0 Posts

    Default Re: Distance

    How do I work out that identifier problem and how should I use System.out.print differently?

     
    public class Ed{
     
     
     public static void main(String[] args)
     {
     int N = Integer.parseInt(args[0]);
     int[] a = new int[N];
     int[] b = new int[N];
     int[] c = new int[N];
     
     for (int i = 0; i < N;i++)
     {
    // double sum=0.0;
     
     int[] c = Math.pow((a[i]-b[i]),2); 
     
     //return Math.sqrt(sum);
     }
     
    //double distance=get(Object sum);
     
     }
     
    System.out.print(Math.sqrt(int [c]));
    }

    errors found:
    File: C:\Users\svana\introcs\Ed.java [line: 24]
    Error: <identifier> expected
    File: C:\Users\svana\introcs\Ed.java [line: 24]
    Error: <identifier> expected
    File: C:\Users\svana\introcs\Ed.java [line: 24]
    Error: ';' expected
    File: C:\Users\svana\introcs\Ed.java [line: 24]
    Error: illegal start of type
    File: C:\Users\svana\introcs\Ed.java [line: 24]
    Error: ';' expected

  16. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Distance

    Which line is line 24?

    --- Update ---

    One problem with the posted code is the poor formatting that makes it hard to see nested statements.

    With proper indentations it is easy to see what code is inside a particular pair of {}s.

    --- Update ---

    See post#10 for an idea on how to indent nested statements.
    If you don't understand my answer, don't ignore it, ask a question.

  17. The Following User Says Thank You to Norm For This Useful Post:

    einar123 (September 21st, 2013)

  18. #13
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Distance

    You have two local variables named 'c', (lousy name, by the way - give your variables better names, even if they are foreign to us).

    Either your code is poorly formatted to begin with, or it pasted/posted badly. I'll show you how it should be formatted below.

    Math.pow() returns a double, so you can't assign it to an int without casting.

    'c' is a reference to the array itself, but you want to assign values to individual elements of the array, like:

    c[i] = (int)Math.pow((a[i]-b[i]),2);

    And your print() statement exists outside the main() method. Reel it back in to before the main() method closing brace.

    Correctly formatted, but not all of the above problems have been fixed:
    public class Ed
    {
        public static void main(String[] args)
        {
            int N = Integer.parseInt(args[0]);
            int[] a = new int[N];
            int[] b = new int[N];
            int[] c = new int[N];
     
            for (int i = 0; i < N;i++)
            {
                // double sum=0.0;
     
                c[i] = (int)Math.pow((a[i]-b[i]),2); 
     
                //return Math.sqrt(sum);
            }
     
            //double distance=get(Object sum);
     
        } // end method main()
     
        System.out.print(Math.sqrt(int [c]));
    }

    Oh, and I just noticed that this in the print() method is just plain wrong:

    int [c]

    What are you trying to do there?

  19. The Following User Says Thank You to GregBrannon For This Useful Post:

    einar123 (September 21st, 2013)

  20. #14
    Member
    Join Date
    Aug 2013
    Location
    Reykjavik, Iceland
    Posts
    51
    Thanks
    37
    Thanked 0 Times in 0 Posts

    Default Re: Distance

    Ok. Hopefully this works.

    Line 24 is the System.out.print....last line.

    public class Ed{
     
     
     public static void main(String[] args)
     {
    	int N = Integer.parseInt(args[0]);
     	int[] a = new int[N];
     	int[] b = new int[N];
     	int[] c = new int[N];
     
     for (int i = 0; i < N;i++)
     {
     	int[] c = Math.pow((a[i]-b[i]),2); 
     }
     
     }
     	System.out.print(Math.sqrt(int [c]));
     }


    --- Update ---

    And the error message:
    5 errors found:
    File: C:\Users\svana\introcs\Ed.java [line: 17]
    Error: <identifier> expected
    File: C:\Users\svana\introcs\Ed.java [line: 17]
    Error: <identifier> expected
    File: C:\Users\svana\introcs\Ed.java [line: 17]
    Error: ';' expected
    File: C:\Users\svana\introcs\Ed.java [line: 17]
    Error: illegal start of type
    File: C:\Users\svana\introcs\Ed.java [line: 17]
    Error: ';' expected

  21. #15
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Distance

    Nope. You still can't easily see that the last statement is outside any method. Look at the way mine is formatted.

  22. The Following User Says Thank You to GregBrannon For This Useful Post:

    einar123 (September 21st, 2013)

  23. #16
    Member
    Join Date
    Aug 2013
    Location
    Reykjavik, Iceland
    Posts
    51
    Thanks
    37
    Thanked 0 Times in 0 Posts

    Default Re: Distance

    I think I'll trash it and start over..

  24. #17
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Distance

    What? All 18 lines?

    I wouldn't describe it as a huge loss, but there's much to be learned by fixing the mistakes you have in the 18 lines of code you've posted or the fewer mistakes that exist in the 24 lines that I posted. Perhaps you can write the next 18 or 24 lines without those errors. Perhaps not. It's your decision.

  25. The Following User Says Thank You to GregBrannon For This Useful Post:

    einar123 (September 21st, 2013)

  26. #18
    Member
    Join Date
    Sep 2013
    Posts
    70
    Thanks
    1
    Thanked 13 Times in 13 Posts

    Default Re: Distance

    einar123 are you taking a course on programming or are you learning on your own?

    The spacing that GregBrannon is talking about helps immensly in debugging your code. Naming convention and comments are also great maintainability specially in larger projects

    Example
    // No spacing no attention to anything
    public class NoFormating
    {
    public static void main(String[] args)
    {
    String a = "John";
    int b = 43;
    System.out.println("Hello " + a + ", that is an awesome name! I am also " + b + " years old.");
    }
    }
     
    // Spacing and naming conventions
    public class Formatting
    {
       public static void main(String[] args)
       {
          // Declare and initialize variables
          String name = "John"
          int age = 43;
     
          // Display a message containing values of variables
          System.out.println("Hello " + name + ", that is an awesome name! I am also " + age + " years old.");
       }// End main
    }

    As you can see it is a lot easier to follow the second one with spacing. The names of the variables allow a programmer to get an understanding of what the variable is suppose to do.

    In your code I see you using arrays is there a reason behind it?

  27. #19
    Member
    Join Date
    Aug 2013
    Location
    Reykjavik, Iceland
    Posts
    51
    Thanks
    37
    Thanked 0 Times in 0 Posts

    Default Re: Distance

    I'm taking a course. Apparantly my professor only wants to see that I can apply the formula. Which I have done. But I wonted the code to work.



    public class Euclediand{
     
     public static void main(String[] args)
     {
    	int N = args.length/2;//Vektor given by the user/2
    	Integer.parseInt(args[0]);
    	//Declaration of arrays
    	double[] vektora = new double[N];
    	double[] vektorb = new double[N];
     
     for (int i = 0; i < N;i++)
    	{
    		vektora[i] = Double.parseDouble(args[i]);//creation
    		vektorb[i] = Double.parseDouble(args[N+i]);//this is to add one to the original user input
    	{
     
      double distance = 0.0;//Initializing
     //Now I try to calculate the distance between points
     for (int i = 0; i < N;i++)
    	{
    		distance =((vektora[i]-vektorb[i])*(vektora[i]-vektorb[i]))*2;
    	}
     double edistance= Math.sqrt(distance);
     }
      System.out.print(distance);
     }
     }
     }

    Errors

    File: C:\Users\svana\introcs\Ed.java [line: 19]
    Error: variable i is already defined in method main(java.lang.String[])
    File: C:\Users\svana\introcs\Ed.java [line: 25]
    Error: cannot find symbol
    symbol: variable distance

  28. #20
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Distance

    These are simple fixes, so don't get discouraged.

    For the first, "variable i is already defined," there can only be one variable with the same name in the same "scope." You have nested for loops, both using the loop control variable, 'i'. Change the name of the loop control variable in the second loop to another, like 'j'. As you make that change, you may have to update the distance equation at line 21. Some of those indices may be 'i', some may be 'j'.

    For the second, "cannot find symbol variable distance," the variable distance is declared as a local variable inside extra braces inside the first for loop. Get rid of the extra braces. so that the result looks like:
    for (int i = 0; i < N;i++)
    {
        vektora[i] = Double.parseDouble(args[i]);//creation
        vektorb[i] = Double.parseDouble(args[N+i]);//this is to add one to the original user input
     
        double distance = 0.0;//Initializing
        //Now I try to calculate the distance between points
        for (int j = 0; j < N;j++)
        {
            distance =((vektora[i]-vektorb[j])*(vektora[i]-vektorb[j]))*2;
        }
        double edistance= Math.sqrt(distance);
     
        System.out.print(distance);
    }
    I notice that edistance is computed but never used. What's that variable for? Is that what is supposed to be printed out? If so, fix that.

    Then I suspect you'll have some runtime errors, because I don't know what the user is supposed to enter when running the program. What arguments are supposed to be provided by the user on the command line when running the program?

  29. The Following User Says Thank You to GregBrannon For This Useful Post:

    einar123 (September 21st, 2013)

  30. #21
    Member
    Join Date
    Aug 2013
    Location
    Reykjavik, Iceland
    Posts
    51
    Thanks
    37
    Thanked 0 Times in 0 Posts

    Default Re: Distance

    Only one error left that has been with me all the time.
    N is just some number or numbers, proferable N>0.
    I have to sqare the distance and print it out.
    GregBrannon are you online like 24h?) Thanks for the effort.


    public class Euclediand{
     
     public static void main(String[] args)
     {
    	int N = args.length/2;//Vektor given by the user/2
    	Integer.parseInt(args[0]);
    	//Declaration of arrays
    	double[] vektora = new double[N];
    	double[] vektorb = new double[N];
     
     for (int i = 0; i < N;i++)
    	{
    		vektora[i] = Double.parseDouble(args[i]);//creation
    		vektorb[i] = Double.parseDouble(args[N+i]);//this is to add one to the original user input
    	{
     
      double distance = 0.0;//Initializing
     //Now I try to calculate the distance between points
     for (int j = 0; j < N;j++)
    	{
    		distance =((vektora[i]-vektorb[j])*(vektora[i]-vektorb[j]))*2;
    	}
    	double edistance= Math.sqrt(distance);
     }
    	System.out.print(edistance);
     }
     }
     }

    Error:
    Cannot find symbol
    System.out.print(edistance)

    --- Update ---

    Solved

    Thanks

  31. #22
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Distance

    You're done? Congrats. Hope I helped, glad if I did.

    Online 24 hrs? No. And if I'm ever getting in the way, just PM me or post for all to see that I should butt out. (That's an idiom for "back off," "go away," "take a break," etc.)

    Be well, and keep coding!

  32. #23
    Member
    Join Date
    Aug 2013
    Location
    Reykjavik, Iceland
    Posts
    51
    Thanks
    37
    Thanked 0 Times in 0 Posts

    Default Re: Distance

    Yes it's solved. It's supposed to be like this

    Your a big help. Thanks. Now it's on to the next.... After a good nights sleep.

Similar Threads

  1. Distance traveled problem
    By Steph2752 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: June 25th, 2013, 10:43 AM
  2. Calculating Fuel After Distance
    By floorplay in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 23rd, 2013, 11:12 PM
  3. Distance function help
    By abf617 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: August 2nd, 2012, 06:32 PM
  4. Getting Distance between two addresses
    By Shockwave786 in forum Java Theory & Questions
    Replies: 3
    Last Post: July 23rd, 2012, 10:19 AM
  5. Distance between 2 points
    By captain in forum Java Theory & Questions
    Replies: 3
    Last Post: February 22nd, 2012, 12:53 AM