-2

Almost done with this program, but when it's sorted out the weight is being arranged to from least to greatest, which is correct. The only problem is that the name and age associated with the weight are not sorted with the weight.

for example

mike 25 180          
jen  36 105      
sam  22 120

I should get

jen  36 105     
sam  22 120     
mike 25 180

but I get

mike 25 105    
jen  36 120   
sam  22 180
import java.util.*;
import java.util.ArrayList;

import java.util.List;

public class Lab0 {


public static void main(String[] args) {

  //Scanner and variables  
  Scanner keyboard = new Scanner (System.in);
  String name = "";
  int age;
  double weight;
  int number;

  //Creates list for name, age, and weight
  ArrayList<String> Name = new ArrayList<String>();
  ArrayList<Integer> Age = new ArrayList<Integer>();
  ArrayList<Double> Weight = new ArrayList<Double>();

    System.out.println("Enter the information needed.");

    //Ask user to enter information needed
    while (true){
        System.out.print("Enter last name: ");
        name = keyboard.nextLine();

        if(name.equalsIgnoreCase("FINISHED")){
            break;
        }
        else {
            Name.add(name);
            System.out.print("Enter age: "); 
            age = keyboard.nextInt();
            Age.add(age);
            System.out.print("Enter weight: ");
            weight = keyboard.nextDouble();
            Weight.add(weight);
            keyboard.nextLine();
            System.out.println("==========================================\n");

        }
    }

    //new list to sort weight and age and name
    ArrayList<String> NameList = Name;
    ArrayList<Integer> AgeList = Age;
    ArrayList<Double> WeightList = Weight;

    Collections.sort(Weight);
    for(int i=0; i<Weight.size(); i++){
        for (int j=0; j<Weight.size(); j++){
            if(WeightList.get(j) == Weight.get(i)){
                Name.set(j, NameList.get(i));
                Age.set(j, AgeList.get(i));
            }

            else;
        }
    }



    //prints out information entered
     for (int k=0; k<Weight.size(); k++){

         System.out.println("Name: " + Name.get(k) + " Age: " + Age.get(k)
         + " weight: " + Weight.get(k));
     }


     while (true){
         System.out.println("=============================================");
         System.out.print("Enter a last name that you listed: ");
         String Search = keyboard.next();

         int index = Name.indexOf(Search);
         if (index >=0){
             System.out.println("Age: " + Age.get(index));
             System.out.println("Weight: " + Weight.get(index));
         }
         else if(Search.equalsIgnoreCase ("DONE")){
             System.exit(0);
         }
         else{
             System.out.println("NOT FOUND");
         }
     }
}

}
RealSkeptic
  • 33,993
  • 7
  • 53
  • 79
Loraine
  • 5
  • 5
  • 5
    Why not make a Person object and make a Comparable method? – Chris Forrence Jan 30 '15 at 18:43
  • 1
    You should probably state what the assignment was, exactly. Personally, I would create a single list of "Person" objects and sort it, rather than use three separate lists, but it may not be what your assignment is about, so please let us know. – RealSkeptic Jan 30 '15 at 18:44
  • We have to use an array for the assignment to list the information provided. – Loraine Jan 30 '15 at 18:46
  • You may want to talk to your professor and ask some clarifying questions. A reasonable solution would be to create a class that encapsulates the Person (as suggested by @ChrisForrence and @RealSkeptic). The solution would still use an array, there'd just be a single array rather than three of them. – Morgen Jan 30 '15 at 18:49
  • You may want to visit [this](http://www.mkyong.com/java/java-object-sorting-example-comparable-and-comparator/) resource; it does a good job of explaining sorting objects. – Chris Forrence Jan 30 '15 at 18:58
  • I did say "exactly", meaning that you should add the description of the assignment - **in full** - to your question. You are not actually using an array here, so the way you described it is probably incorrect. I can tell you what's wrong with your program, but I can't give you a recommendation to improve it without knowing what was required. – RealSkeptic Jan 30 '15 at 19:02

1 Answers1

0

The problem is, that the relation is loosing between the single-informations. A person is a tripel like (name, age, weight).

You have three lists and after sorting the list with weights, you're want to concatenate the single-informations.

That is the problem-section of your code:

ArrayList<String> NameList = Name;
ArrayList<Integer> AgeList = Age;
ArrayList<Double> WeightList = Weight;

Collections.sort(Weight);
for(int i=0; i<Weight.size(); i++){
    for (int j=0; j<Weight.size(); j++){
        if(WeightList.get(j) == Weight.get(i)){
            Name.set(j, NameList.get(i));
            Age.set(j, AgeList.get(i));
        }

        else;
    }
}

Your first problem is: If two or more persons having the same weight, you're not find a clearly relation. (Not the topic of your question, but a problem.)

Your secound problem is: (If all weights are different.)

Collections.sort(Weight);

This method sorts the list 'Weight'. But it is the same list with the name 'WeightList '. You're copy the reference.

You shold clone the list first, bevore you're use sort.

And you need to change i and j in 'Name.set(...)' and 'Age.set(...)'.

This should help:

ArrayList<String> NameList = (ArrayList<String>) Name.clone();
ArrayList<Integer> AgeList = (ArrayList<Integer>) Age.clone();
ArrayList<Double> WeightList = (ArrayList<Double>) Weight.clone();

Collections.sort(Weight);
for (int i = 0; i < Weight.size(); i++) {
    for (int j = 0; j < Weight.size(); j++) {
        if (WeightList.get(j) == Weight.get(i)) {
            Name.set(i, NameList.get(j));
            Age.set(i, AgeList.get(j));
        }

        else
            ;
    }
}

I think that's the answer for your problem.

In addition:

You should think about 'ChrisForrence' comment of your qurestion. It is better using a class for person which implements the interface 'Comparable'.

You should use comparators in my opinion. That is the general method for getting flexibility.

Uwe Schmidt
  • 81
  • 1
  • 7