0

Make a program that gets user input and stores it in arrays. You will store information about, at least 3, people. There will be three pieces of information you will need to store about each person: name, age and gender (age must be an integer). After the user inputs information about every person you will print all of the information like shown below

List of people

Melissa, 28, F
Adam, 11, M
Landon, 6, M
Sadie, 1, F

How do I order the people by their age when I have String and int at the same time? Here is my code:

public static void main(String[] args) {

    Scanner inputString = new Scanner(System.in);
    Scanner input = new Scanner(System.in);

    System.out.println("Enter your age:");
    int age1 = input.nextInt();
    System.out.println("Enter your name:");
    String name1 = inputString.nextLine();
    System.out.println("Enter your gender:");
    String gender1 = inputString.nextLine();

    System.out.println("Enter your age:");
    int age2 = input.nextInt();
    System.out.println("Enter your name:");
    String name2 = inputString.nextLine();
    System.out.println("Enter your gender:");
    String gender2 = inputString.nextLine();

    System.out.println("Enter your age:");
    int age3 = input.nextInt();
    System.out.println("Enter your name:");
    String name3 = inputString.nextLine();
    System.out.println("Enter your gender:");
    String gender3 = inputString.nextLine();

    int[] age = new int[3];
    age[0] = age1;
    age[1] = age2;
    age[2] = age3;

    String[] name = new String[3];
    name[0] = name1;
    name[1] = name2;
    name[2] = name3;

    String[] gender = new String[3];
    gender[0] = gender1;
    gender[1] = gender2;
    gender[2] = gender3;

    System.out.print("List of People");
    System.out.print("\n" + (age[0]) + ", " + (name[0]) + ", " + (gender[0]));
    System.out.print("\n" + (age[1]) +", " + (name[1]) +", "+ (gender[1]));
    System.out.print("\n" + (age[2]) + ", " + (name[2]) +" , "+ (gender[2]));
}
Mateusz Korwel
  • 1,118
  • 1
  • 8
  • 14
A.Thut
  • 1
  • 2
  • 2
    Possible duplicate of [How to sort multiple arrays in java](http://stackoverflow.com/questions/12164795/how-to-sort-multiple-arrays-in-java) – Andy Turner Dec 21 '15 at 15:27
  • 3
    you really need to learn how to use loops... and your question boils down to "how to sort an array in java", of which there's a few gazillion examples on the web... – Marc B Dec 21 '15 at 15:27
  • Possible duplicate of [Java - Sort one array based on values of another array?](http://stackoverflow.com/questions/28556129/java-sort-one-array-based-on-values-of-another-array) – Jordi Castilla Dec 21 '15 at 15:29
  • `nextLine()` after `nextInt()`.. Umm. Your code is gonna break.. – TheLostMind Dec 21 '15 at 15:30
  • After loops, learn about classes, eg `class Person {String name; int age; char gender;}` Then collections ... – Neil Masson Dec 21 '15 at 15:30

1 Answers1

1

If you want to learn Java, please try to first learn object oriented concepts.

In your specific case, you should be using a Person class, a single List<Person> and a Comparator of Person instances to sort your list, instead of trying to sort three different arrays. Have a look at the following example.

First, your runner class that contains the main() method:

package test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;
import test.Person.Gender;

public class Runner {

    public static void main(final String[] args) {
        Scanner input = new Scanner(System.in);
        Boolean createNewPerson = true;
        // a single list of person instances
        List<Person> people = new ArrayList<Person>();
        while (createNewPerson) {
            Person person = new Person();
            System.out.println("Enter age:");
            person.setAge(Integer.valueOf(input.nextLine()));
            System.out.println("Enter name:");
            person.setName(input.nextLine());
            System.out.println("Enter gender:");
            person.setGender(Gender.valueOf(input.nextLine().toUpperCase()));
            // add the person to the list
            people.add(person);
            System.out.println("Add another person ? (true/false)");
            createNewPerson = Boolean.valueOf(input.nextLine());
        }
        input.close();
        // here is the sorting trick
        Collections.sort(people, new AgeComparator());
        // print it out
        System.out.println(Arrays.toString(people.toArray()));
    }
}

Your Person class:

package test;

public class Person {
    private String name;
    private Gender gender;
    private Integer age;

    public Integer getAge() {
        return this.age;
    }

    public void setAge(final Integer age) {
        this.age = age;
    }

    public String getName() {
        return this.name;
    }

    public void setName(final String name) {
        this.name = name;
    }

    public Gender getGender() {
        return this.gender;
    }

    public void setGender(final Gender gender) {
        this.gender = gender;
    }

    @Override
    public String toString() {
        return this.name + " is a " + this.gender + " and is " + this.age + " year(s) old." + System.lineSeparator();
    }

    enum Gender {
        MALE,
        FEMALE;
    }

}

And your Comparator (here I wrote one that compares by age, but it is only an example):

package test;

import java.util.Comparator;

public class AgeComparator implements Comparator<Person> {

    @Override
    public int compare(final Person person1, final Person person2) {
        return person1.getAge().compareTo(person2.getAge());
    }

}

In other words, programming is not about writing lines, it's about conception, design, using concepts and only then writing lines of code.

Kraal
  • 2,779
  • 1
  • 19
  • 36