Results 1 to 3 of 3
  1. #1
    Chewart is offline Member
    Join Date
    Nov 2009
    Posts
    75
    Rep Power
    0

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

  2. #2
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    7

    Default

    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.

  3. #3
    Chewart is offline Member
    Join Date
    Nov 2009
    Posts
    75
    Rep Power
    0

    Default

    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

  1. Help with loops!
    By jrelvi23 in forum New To Java
    Replies: 9
    Last Post: 09-04-2012, 02:36 PM
  2. when should we use loops
    By shahemaan in forum New To Java
    Replies: 1
    Last Post: 10-31-2009, 02:38 AM
  3. While Loops, need a bit of help.
    By Keno777 in forum New To Java
    Replies: 7
    Last Post: 10-30-2009, 09:24 PM
  4. how to use do while loops
    By mikeitalydz in forum New To Java
    Replies: 32
    Last Post: 09-26-2009, 09:30 PM
  5. Loops (while do etc)
    By manupr in forum New To Java
    Replies: 1
    Last Post: 01-15-2008, 04:59 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •