Reputation: 335
I have been studying Java since last week (I have basic knowledge in C#) and now I am practicing classes. I don't understand why I get this exception, I tried to change the code many times and nothing works. This is the code:
package javaapplication1;
import java.util.Scanner;
public class JavaApplication1 {
static Scanner sc = new Scanner(System.in);
public static void main(String[] args) {
Competitor[] competitors;
System.out.println("Enter number of competitors");
competitors = Create(sc.nextInt());
for(int i = 0;i<competitors.length - 1;i++)
for(int j=i+1;j<competitors.length;j++)
if(competitors[i].GetHeight()==competitors[j].GetHeight())
System.out.println(competitors[i]+" and "
+competitors[j] + "bounced to height"
+competitors[i].GetHeight());
}
static Competitor[] Create(int n) {
Competitor[] competitors = new Competitor[n];
for(Competitor c : competitors) {
c = new Competitor();
System.out.println("Enter name, id and bounce height");
c.SetName(sc.next());
c.SetId(sc.next());
c.SetHeight(sc.nextFloat());
}
return competitors;
}
}
class Competitor {
private String name;
private String id;
private float height;
public Competitor() {
}
public Competitor(String name, String id, float height) {
this.name = name;
this.id = id;
this.height = height;
}
public String GetName() {
return this.name;
}
public String GetId() {
return this.id;
}
public Float GetHeight() {
return this.height;
}
public void SetName(String name) {
this.name = name;
}
public void SetId(String id) {
this.id = id;
}
public void SetHeight(Float height) {
this.height = height;
}
}
I get the exception in this line: if(competitors[i].GetHeight()==competitors[j].GetHeight())
What is the problem here?
Thanks very much!
Upvotes: 1
Views: 118
Reputation: 1518
You're doing this
Competitor[] competitors = new Competitor[n]; for(Competitor c : competitors) { c = new Competitor(); System.out.println("Enter name, id and bounce height"); c.SetName(sc.next()); c.SetId(sc.next()); c.SetHeight(sc.nextFloat()); } return competitors;
But like that you just assign a new Competitor to "c". You should take off that c = new Competitor()
so it's like this
Competitor[] competitors = new Competitor[n]; for(Competitor c : competitors) { System.out.println("Enter name, id and bounce height"); c.SetName(sc.next()); c.SetId(sc.next()); c.SetHeight(sc.nextFloat()); } return competitors;
Your other problem with SetHeight()
was because SetHeight()
was picking a primitive value and putting it in a Class type Float (automatically thanks to auto-boxing). The class type internally store the float primitive in a field. When you used ==, it compared the two objects and determined that they didn't point to the same place, even though their fields pointed to a float primitive with the same value.
That's why when comparing non-primitive types we should always use .equals()
method instead of ==, unless you're trying to test it against null. .equals()
will make sure it doesn't compare the place where the variable is pointing but their internal fields.
Take a look at this answer https://stackoverflow.com/a/73021/2576857
I think it will help you to understand both problems you had.
Upvotes: 0
Reputation: 9601
This enhanced for loop:
for(Competitor c : competitors) {
c = new Competitor();
System.out.println("Enter name, id and bounce height");
c.SetName(sc.next());
c.SetId(sc.next());
c.SetHeight(sc.nextFloat());
}
is equivalent to:
for(int i = 0; i < competitors.length; i++) {
Competitor c = competitors[i];
c = new Competitor();
System.out.println("Enter name, id and bounce height");
c.SetName(sc.next());
c.SetId(sc.next());
c.SetHeight(sc.nextFloat());
}
So you modified c
, but you never modified competitors
Java have different conventions with C#. See here.
Methods should be verbs, in mixed case with the first letter lowercase, with the first letter of each internal word capitalized.
Upvotes: 0
Reputation: 3207
The Create method initializes the competitors values in the wrong way. Try this:
for(int i = 0;i<competitors.length;i++){
competitors[i] = new Competitor();
System.out.println("Enter name, id and bounce height");
competitors[i].SetName(sc.next());
competitors[i].SetId(sc.next());
competitors[i].SetHeight(sc.nextFloat());
}
One more suggestion. While in C# methods name starts with one upper case char, in Java they start in lower case.
Upvotes: 2