Java Newbie Printing out data help?

StuJ

New member
Joined
Nov 29, 2008
Messages
2
Reaction score
0
Points
1
the aim is to have 5 users to input data into their program and to output 5 people that have entered their details respectively. any hints would be great =)

import java.util.*;
public class Person
{
// Tests the methods of the Person File
public static void main(String [] args)
{
int [] i = new NewPerson[5];
Scanner input = new Scanner(System.in);

for (int i = 0; i <= i.length ; i++)
{

System.out.print("Please enter your Forename ");
String FName = input.next();

System.out.print("Please enter your Surname ");
String LName = input.next();

System.out.print("Please enter your Age ");
int AAge = input.nextInt();

System.out.print("Please enter your Height ");
int AHeight = input.nextInt();

PersonTest person1 = new PersonTest(FName, LName, AAge, AHeight);
person1.format();

System.out.println("The number of Persons entered are " + (i));
}


}
}
 
You have created a "Person" class.

So to have a new object of type "Person", have something like the following line.
new Person();
or
new Person(firstName, lastName, age, height);

The following lines that were in your code will produce errors. These will not compile.
new NewPerson[5];
new PersonTest(FName, LName, AAge, AHeight);

To pass the values to a "Person" constructor, you need to create a constructor that accepts the value. Look at my modified example.

There are several ways to print. I have created and used the printPerson() method to print a "Person".

Here is the code:
=============

import java.util.Scanner;

class Person {

String firstName;
String lastName;
int age;
int height;

// This is a constructor
public Person(){

}

// This is also a constructor
// To pass the values you will need like this one.
public Person(String myFirstName,String myLastName,int myAge,int myHeight){
this.firstName= myFirstName;
this.lastName= myLastName;
this.age= myAge;
this.height= myHeight;
}

public void printPerson(){
String temp=
"First Name : "+firstName+
"\nLast Name : "+lastName+
"\nAge : "+age+
"\nHeight : "+height;

System.out.println(temp);
}


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

// this is for having 5 person array
Person personArray[] = new Person[5];

// this for loop will get the data.

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

System.out.println("\nPlease enter data for Person "+(i+1)+"\n");

System.out.print("Please enter your Forename ");
String tempFirstName = input.nextLine();

System.out.print("Please enter your Surname ");
String tempLastName = input.nextLine();

System.out.print("Please enter your Age ");
int tempAge = Integer.parseInt(input.nextLine());

System.out.print("Please enter your Height ");
int tempHeight = Integer.parseInt(input.nextLine());

// here we create a new person object
// with the values we have got from the user.
personArray = new Person(
tempFirstName,
tempLastName,
tempAge,
tempHeight);

}

// this for loop is to print the data.

for(int i=0;i<5;i++){
System.out.println("\n Person "+(i+1)+"\n");
personArray.printPerson();
}
}
}
 
Back
Top