0

I'm trying to make a program that has an array of Person objects called "personArray" that is populated by a for loop that reads user input for three separate variables and then turns those variables into a Person object and adds it to the array.

Here is my code for Person.java:

public class Person
{
    private String firstName, lastName;
    private int zip;

    public Person(String fName, String lName, int perZip)
    {
        firstName = fName;
        lastName = lName;
        zip = perZip;
    }
}

And here is my code for PersonDriver.java:

import java.util.Scanner;

public class PersonDriver
{
    public static void main(String[] args)
    {
        Scanner scan = new Scanner(System.in);
        String firstName, lastName;
        int zipCode;

        Person[] personArray = new Person[25];

        for (int i = 0; i < 25; i++)
        {   
            System.out.println("Enter first name: ");
            firstName = scan.nextLine();

            System.out.println("Enter last name: ");
            lastName = scan.nextLine();

            System.out.println("Enter zip: ");
            zipCode = scan.nextInt();

            personArray[i] = new Person(firstName, lastName, zipCode);
        }

        scan.close();

        System.out.println(personArray);
    }
}
  • 1
    Not sure what your asking us to do.. The question is unclear – UnknownOctopus Jul 05 '15 at 17:36
  • You've done already what you've asked for – Madhan Jul 05 '15 at 17:38
  • Welcome to Stack Overflow. Please take a look at [How do I ask a good question](https://stackoverflow.com/help/how-to-ask). In its current state, your post does not contain enough information for us to help you. – Jeffrey Jul 05 '15 at 17:38
  • Sorry, the code doesn't work correctly. When I run it it seems to run fine the first time around but then it prints out "First Name:" "Last Name:" at the same time. –  Jul 05 '15 at 17:38
  • 1
    That's because you're trying to print the array directly which contains Person objects. You should iterate through the array while picking out the attributes you want to print by some getters in your Person class. – tankucukoglu Jul 05 '15 at 17:41
  • possible duplicate of [Getting User input with Scanner](http://stackoverflow.com/questions/12999899/getting-user-input-with-scanner) – Madhan Jul 05 '15 at 17:48

1 Answers1

2

Funny story: this is a very frustrating and common error I came across when I was a Java newbie.

Put an extra scan.nextLine() after zipCode. Do not save the value. Just call the method.

The reasoning behind this is when you call nextInt(), the scanner's cursor moves to the position after the integer, but not to the next line if a newline follows the integer. When you call nextLine(), it grabs all characters between the cursor and the next newline character it comes across, which in this case is a blank String. it For example, if you have input such as:

Alec
Parsons
60120

The first iteration will work just fine, but the cursor will stay on the zip code's line, like so:

Alec
Parsons
60120I

At the next call to nextLine() for firstName, you will instead receive a blank String. Eventually, the program will call nextInt() on a String value, which will throw an Exception.

It's not very intuitive, but that's how Scanner works.

Edit: Also, as pointed out in the comments, every Java object has a String representation, so putting an array inside println() is syntactically correct. However, the String representation is not always readable. What you want to do is to loop through each Person and print his/her information. One awesome thing about Java is that you can define how your class is represented as a String by overriding the toString() method. Just put this method in your Person class:

public String toString()
{
    // You can define how Person is represented in String form here
    return "Name: " + firstName + " " + lastName + "\n" + "Zip: " + zip;
}

This way, you can use Person as an argument to println(), and Java will automatically print out the information in your desired format.

for(Person p: personArray)
    System.out.println(p);

Name: Alec Parsons
Zip: 60120

Aaron Ong
  • 181
  • 9