+ 1

Java Vector loop fails

Person indie = new Person(); Vector<Person> personList = new Vector<Person>(); //-Snip- while(nextPerson.equalsIgnoreCase("y")){ System.out.println("Name:"); indie.setName(scanner.next()); System.out.println("Gender (m/f):"); indie.setGender(scanner.next()); System.out.println("Age:"); indie.setAge(scanner.nextInt()); System.out.println("Weight:"); indie.setWeight(scanner.nextDouble()); System.out.println("Height:"); indie.setHeight(scanner.nextDouble()); personList.addElement(indie); System.out.println("Would you like to add another person?(y/n)"); nextPerson = scanner.next(); } for(int i = 0; i < personList.size();i++){ System.out.println("Name: " + personList.get(i).getName()); System.out.println("Gender: " + personList.get(i).getGender()); System.out.println("Age: " + personList.get(i).getAge()); System.out.println("Weight: " + personList.get(i).getWeight()); System.out.println("Height: " + personList.get(i).getHeight()); System.out.println("BMI: " + personList.get(i).calcBMI()); System.out.println("Assesment " + personList.get(i).assesBMI()); } This code only displays the last element of the vector for the times of its size and I don't know why it doesn't display the rest. It also doesnt work with the ForEach loop. None of the attributes that are set have any additional declaration besides "private". Could anybody help me out? Thanks!

20th Jun 2017, 6:36 PM
Anonymous
Anonymous - avatar
7 Antworten
+ 3
Move Person indie = new Person() inside your while loop at the top and it should work. Change vector to an arraylist.
20th Jun 2017, 6:40 PM
ChaoticDawg
ChaoticDawg - avatar
+ 2
This loop looks fine to me. If it's only displaying the last person there's likely a problem elsewhere in the code. Potential Problems: The last element must have became all of the other elements. Or, the variables 'height', 'width' etc.. are static.
20th Jun 2017, 5:53 PM
Rrestoring faith
Rrestoring faith - avatar
+ 2
It works now, I really thank you!
20th Jun 2017, 6:55 PM
Anonymous
Anonymous - avatar
+ 1
Why are you using a vector? If thread safety is not an issue you should be using an arraylist. Also without seeing the rest of the code you won't get an answer to your question, because the issue is most likely not in the for loop, but in the assignment of your objects to the vector or somewhere else before the loop.
20th Jun 2017, 6:01 PM
ChaoticDawg
ChaoticDawg - avatar
+ 1
No you're only adding 1 object multiple times to your vector and then just resetting its values. So when you get to the for loop all that is displayed is that 1 object.
20th Jun 2017, 6:38 PM
ChaoticDawg
ChaoticDawg - avatar
0
You're not creating a new object in your while loop. You're just setting variables to the one you created outside the loop (indie) over and over. So basically whatever the last values you set for that object are all that will be displayed.
20th Jun 2017, 6:33 PM
ChaoticDawg
ChaoticDawg - avatar
0
I crreated it outside of the loop, i thought it was irelevant to add it in this snippet, sorry if I confused you
20th Jun 2017, 6:35 PM
Anonymous
Anonymous - avatar