# Distance

Printable View

• September 20th, 2013, 07:30 PM
einar123
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?

Code Java:

```  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....
• September 20th, 2013, 07:35 PM
Ubiquitous
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.
• September 20th, 2013, 07:43 PM
einar123
Re: Distance
Where? inside the loop? ...it seems to be a bigger problem... Now I've got an illegal start of expression
• September 20th, 2013, 07:51 PM
Norm
Re: Distance
Quote:

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.
• September 21st, 2013, 02:41 AM
einar123
Re: Distance
Now I got this:

Code Java:

```  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
• September 21st, 2013, 03:38 AM
GregBrannon
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.
• September 21st, 2013, 04:10 AM
einar123
Re: Distance
Now I'm trying to simplify this. I don't know how to call a method outside of main.

Code Java:

```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
• September 21st, 2013, 04:23 AM
GregBrannon
Re: Distance
Quote:

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] );
• September 21st, 2013, 04:47 AM
einar123
Re: Distance
Men.....hahahah you are killing me. I don't get it!
• September 21st, 2013, 04:59 AM
GregBrannon
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:
Code java:

```// 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```
• September 21st, 2013, 09:43 AM
einar123
Re: Distance
How do I work out that identifier problem and how should I use System.out.print differently?

Code Java:

```  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
• September 21st, 2013, 09:54 AM
Norm
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.
• September 21st, 2013, 09:57 AM
GregBrannon
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:
Code java:

```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?
• September 21st, 2013, 10:04 AM
einar123
Re: Distance
Ok. Hopefully this works.

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

Code java:

```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
• September 21st, 2013, 10:04 AM
GregBrannon
Re: Distance
Nope. You still can't easily see that the last statement is outside any method. Look at the way mine is formatted.
• September 21st, 2013, 10:59 AM
einar123
Re: Distance
I think I'll trash it and start over..
• September 21st, 2013, 11:16 AM
GregBrannon
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.
• September 21st, 2013, 02:42 PM
Ubiquitous
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
Code :

```// 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?
• September 21st, 2013, 03:59 PM
einar123
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.

Code java:

```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
• September 21st, 2013, 04:16 PM
GregBrannon
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:
Code java:

```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?
• September 21st, 2013, 04:44 PM
einar123
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.

Code java:

```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
• September 21st, 2013, 04:55 PM
GregBrannon
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!
• September 21st, 2013, 05:05 PM
einar123
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.