Results 1 to 3 of 3
Thread: need some help with loops!
- 12-03-2009, 10:39 PM #1
Member
- Join Date
- Nov 2009
- Posts
- 75
- Rep Power
- 0
need some help with loops!
hey guys 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 =)
my program needs =
a loop so that it ask 5 times users name age etc etc
and then counts ther number of users entered..
i just cant get my head around it. i know i need a loop inside a loop but i cant figure out how i can do it. really would appreciate anyones help to get my program working i really would.
Java Code:/A Program to test my Person Class Program 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)); } } }Java Code://A Program to test my Person Class Program import java.util.*; public class PersonTest { // Tests the methods of the Person File public static void main(String [] args) { Scanner input = new Scanner(System.in); //Creates an Array that counts the number of Persons in the Program int [] countPerson = new int [5]; for (int i = 0; i < countPerson.length ; i++) { //System.out.print(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(); } //Person person1 = new Person(FName, LName, AAge, AHeight); //person1.format(); //System.out.println("The number of Persons entered are " + (i)); } }
- 12-03-2009, 10:54 PM #2
your person class doesn't need a main. I'm assuming it's just an object with FName, LName, Age, and Height as parameters.
I see you've commented out
//Person person1 = new Person(FName, LName, AAge, AHeight);
which is the proper direction to take this.
Get rid of the main method inside your Person class and instead put a constructor that takes those 4 parameters. Then place a getter method for each of those parameters that way you can retrieve the data from that specific object.Liberty has never come from the government.
Liberty has always come from the subjects of government.
The history of liberty is the history of resistance.
The history of liberty is a history of the limitation of governmental power, not the increase of it.
- 12-03-2009, 11:32 PM #3
Member
- Join Date
- Nov 2009
- Posts
- 75
- Rep Power
- 0
hey call me judas there was an error with my post then i posted the wrong code.. lol could you tell me if u think im close to finishing my program?
Java Code:public class PersonTest { // Constructs a Person Test Class with the default value. public PersonTest() { AHeight = 0; AAge = 0; Person = null; }//Constructs a Person with a given initial value public String Person(String NewPerson) { Person = NewPerson; } // Gets the First Name of the Person in the PersonTest public String getFName() { return Fname; } // Gets the Last Name of the Person in the PersonTest public String getLName() { return Sname; } // Gets the Height of the Person in the PersonTest public double getHeight() { return AHeight; } // Gets the Age of the Person in the PersonTest public double getAge () { return AAge; } private String Person; private String Fname; private String Sname; private double AAge; private double AHeight; }
Similar Threads
-
Help with loops!
By jrelvi23 in forum New To JavaReplies: 9Last Post: 09-04-2012, 01:36 PM -
when should we use loops
By shahemaan in forum New To JavaReplies: 1Last Post: 10-31-2009, 01:38 AM -
While Loops, need a bit of help.
By Keno777 in forum New To JavaReplies: 7Last Post: 10-30-2009, 08:24 PM -
how to use do while loops
By mikeitalydz in forum New To JavaReplies: 32Last Post: 09-26-2009, 08:30 PM -
Loops (while do etc)
By manupr in forum New To JavaReplies: 1Last Post: 01-15-2008, 03:59 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks