简体   繁体   中英

Custom Object Arrays and For Loops

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);
    }
}

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
60120 I

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM