-1

I have an assignment that uses arrays but I can not seem to get it to print the correct results. The assignment asks to create a class which has functions that allow the user to :

1- insert a name, age, and weight for an infinite number of people, until the name "FINISHED" has been entered.

2- It should be able to display all the peoples names, ages, and weights, sorted from lightest weight to the heaviest.

3- The user should also be able to display the age and weight of a person that they searched for (if exists).

Here's what I got so far:

 public static void main(String[] args) {
    Scanner kb = new Scanner(System.in);


    ArrayList<Persons> peopleAr = new ArrayList<Persons>();
    Persons ewPeople = new Persons();
    ewPeople.name = "Peter";
    ewPeople.age = 20;
    ewPeople.weight = 70.0;
    peopleAr.add(ewPeople);

    for (int i = 0; i < peopleAr.size()+5; i++) {

        System.out
                .println("Enter the number of the action you'd like to preform:");
        System.out.println("1. Add new profile.");
        System.out.println("2. View all prfiles (lightest to heaviest).");
        System.out.println("3. Search for a person.");
        System.out.println("4. Exit.");

        int key = kb.nextInt();

        switch (key) {
            case 1:
                System.out
                        .println("Enter the required information. Enter 'finished' in the name field to "
                                + "return to main menu. ");

                Persons newPeople = new Persons();

                newPeople.name = "abc";

                while (!newPeople.name.equals("finished")) {

                    System.out.println("Enter name: ");
                    newPeople.name = kb.next();

                    System.out.println("Enter age: ");
                    newPeople.age = kb.nextInt();

                    System.out.println("Enter weight: ");
                    newPeople.weight = kb.nextDouble();

                    peopleAr.add(newPeople);
                }
                break;

            case 2:

                for (int j = 0; j < peopleAr.size(); j++) {
                    System.out.println(peopleAr.get(i) + "  "
                            + peopleAr.get(i) + "   " + peopleAr.get(i + 2));
                }
                break;

            case 3:
                String nameTemp = "abc";
                while (!nameTemp.equalsIgnoreCase("done")) {
                    System.out
                            .println("Enter the name you'd like to search. Enter 'done' to return to main menu. ");
                    nameTemp = kb.next();
                    boolean check = peopleAr.contains(nameTemp);
                    if (check = true) {
                        int p = peopleAr.indexOf(nameTemp);

                        System.out.println(peopleAr.get(p) + "  "
                                + peopleAr.get(p + 1) + "   "
                                + peopleAr.get(p + 2));

                    } else {
                        System.out
                                .println("There is no match for your search.");

                    }
                }
                break;
            case 4:
                System.exit(0);
        }

    }
}

}

So, The first part works (except for little bugs). The second part does not return the values as a string. I've tried using the toString() method and converting the objects to a string with a loop but none of them work. I need some help with displaying the objects as strings instead of "lab04.Persons@14eac69". The third part also has some issues, but I believe they are related to the second issue since the compiler probably won't like comparing strings to objects.

I know there a lot of things that look like garbage in my code, but they are there to hold things together or otherwise I'd get errors. I wanna fix the main functions before picking the small bugs and cleaning up the code.

Mjalil93
  • 11
  • 1
  • 7
  • Do you have to use an array? – JamesB Jan 19 '15 at 00:15
  • *"Doesn't the array size have to be specified when declared?"* Yes and no. Yes, you must provide "a" size of an array, but there are ways you can grow arrays, either manually (you coding it) or via the available API – MadProgrammer Jan 19 '15 at 00:16
  • be more specify with which collections or structures you may use – roeygol Jan 19 '15 at 00:23
  • Okay, I see what you mean. @MadProgrammer And I am supposed to use an array because I haven't learned any data structures yet. We just spoke about link lists a little bit but for this assignment an array is preferred. – Mjalil93 Jan 19 '15 at 04:33

3 Answers3

0

You can use ArrayList to add new Person objects dynamically. ArrayList give you a bunch of useful options, for example:

  • add()
  • get()
  • set()
  • size()
  • contains()
  • ...

So for your assignment this is how you can achieve your goal:

  1. Ask the user for name, age and weight via Scanner or via GUI Interface. (Probably with Swing). An example for using the Scanner class can be found here: Scanner Example. Then you should wrap them in a Person data model class and add this object to your ArrayList.

  2. To sort the created ArrayList based on the person's weights you should implement a Comparator. An example can be found here: Comparator Example

  3. Last but not least ask the user for the searched Person via Scanner and use the contains method from your ArrayList to actually search for the object. You can always write your own hash() and equals() method to control how the contains() method will work. An example can be found here: Search ArrayList example

Community
  • 1
  • 1
dehlen
  • 7,141
  • 4
  • 38
  • 68
  • Thank you. This was very helpful. I don't know why I was thinking of using an array. An 'array list' would do the job much better. – Mjalil93 Jan 19 '15 at 04:37
-1

You can use a List object and the add() method to add more entities to that list. However, since you have to add multiple values you should make a List that contains other lists, or you could implement some other type of two-dimensional structure.

GeorgeG
  • 230
  • 2
  • 12
-1

First , define the class people

      public class People
      {
          public String name = "";
          public int  age ;
          public double weight;
      }

Second, define a List of class People

      List <People> peopleArray = new ArrayList < People> ();


     // create a new people
     People newPeople = new  People();
     newPeople.name  = "Peter";
     newPeople.age   = 20;
     newPeople.weight = 70.0;

     //  add it to array
     peopleArray.add(newPeople );
peterho
  • 64
  • 1
  • 3