Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Hey Guys new to Java.. Got most of program done require a little assistance plz

  1. #1
    Junior Member
    Join Date
    Dec 2009
    Posts
    22
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Hey Guys new to Java.. Got most of program done require a little assistance plz

    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.

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



    //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));
     
     
     
    	}
    }
    Last edited by helloworld922; December 3rd, 2009 at 09:31 PM.


  2. #2
    Junior Member
    Join Date
    Dec 2009
    Posts
    8
    Thanks
    1
    Thanked 5 Times in 5 Posts

    Default Re: Hey Guys new to Java.. Got most of program done require a little assistance plz

    hi there,The code will be showed below :

    import java.util.*;
     
    class Person{
     
    private String Forename;
    private String Surname;
    private int Age;
    private float Height;
    private int personID;
    Person(String Forename,String Surname,int Age,float Height,int personID){
     
    this.Forename=Forename;
    this.Surname=Surname;
    this.Age=Age;
    this.Height=Height;
    this.personID=personID;
    }
    public void Display(){
    System.out.println("person"+personID+"info:"+Forename+Surname+","+Age+","+Heigth+",");
    }
    }
    public class PersonTest{
    private static String Forename;
    private static String Surname;
    private static int Age;
    private static int Height;
    private static int personID;
    public static void main(String args[]){
    Person []persons=new Person[5];
    for(int i=1;i<=5;i++){
    Scanner scanner=new Scanner(System.in);
    System.out.println("OutPut Forename:");
    Forename=scanner.nextLine();
    System.out.println("Output Surname:");
    Surname=scanner.nextLine();
    System.out.println("Output Age:");
    Age=scanner.nextInt();
    System.out.println("Output Height:");
    Height=scanner.nextInt();
    personID=i;
    persons[i-1]=new Person(Forename,Surname,Age,Height,personID);
    }
    for(int i=0;i<5;i++){
    persons[i].Display();
    }
    }
    }

  3. The Following User Says Thank You to lewvan00 For This Useful Post:

    JavaNoob82 (December 8th, 2009)

  4. #3
    Junior Member
    Join Date
    Dec 2009
    Posts
    22
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Hey Guys new to Java.. Got most of program done require a little assistance plz

    many thanks for your reply lewvan00. i see with your code that you kindly wrote to help me out, i dont see any access or mutator methods used. is there a way of accessing my person test by calling a persons age with getage? or someones details that way? i have ammended my program could you take a look to see if i can use the get methods please? much appreciated

    import java.util.*;
    public class Person
    {
    		private String Forename;
    		private String Surname;
    		private int Age;
    		private int Height;
    		private int personID;
     
    	// Constructs a Person Test Class with the default value.
    	Person(String Forename,String Surname,int Age,int Height,int personID)
    	{
    		this.Forename=Forename;
    		this.Surname=Surname;
    		this.Age=Age;
    		this.Height=Height;
    		this.personID=personID;
    	}
     
    		// Gets the First Name of the Person in the PersonTest
    		public String getFName()
    	{
    		return Forename;
    	}
     
    			// Gets the Last Name of the Person in the PersonTest
    		public String getLName()
    	{
    		return Surname;
    	}
     
    		// Gets the Height of the Person in the PersonTest
    		public double getHeight()
    	{
    		return Height;
    	}
     
    		// Gets the Age of the Person in the PersonTest
    		public int getAge ()
    	{
    		return Age;
    	}
     
    		public void Format()
    	{
    		System.out.println("person"+personID+"\tinfo:"+"\t" +Forename+ " \t"+Surname+"\t"+Age+"\t"+Height);
    	}
     
     
    }

    import java.util.*;
    public class PersonTest
    {
     
    	private static String Forename;
    	private static String Surname;
    	private static int Age;
    	private static int Height;
    	private static int personID;
     
    	// Tests the methods of the Person File
    	public static void main(String [] args)
    	{
     
    		//Creates an Array that counts the number of Persons in the Program
    		Person [] persons = new Person [5];	
     
     
    		for(int i=1;i<=5;i++)
    		{
    			Scanner scanner=new Scanner(System.in);
     
    			System.out.println("Enter a Forename:");
    			Forename=scanner.nextLine();
     
    			System.out.println("Enter a Surname:");
    			Surname=scanner.nextLine();
     
    			System.out.println("Enter a Age:");
    			Age=scanner.nextInt();
     
    			System.out.println("Enter Height:");
    			Height=scanner.nextInt();
     
    			personID=i;
    			persons[i-1]=new Person(Forename,Surname,Age,Height,personID);
    		}
     
    			for(int i=0;i<5;i++)
    			{
    				persons[i].Format();
    			}	
     
     
     
    	}
    }

  5. #4
    Junior Member
    Join Date
    Dec 2009
    Posts
    8
    Thanks
    1
    Thanked 5 Times in 5 Posts

    Default Re: Hey Guys new to Java.. Got most of program done require a little assistance plz

    Actually,you can write a get and set method for each attribute of Person.
    Like:
    public String getForename()
    {
    return this.Forename;
    }
    public void setForename(String Forename){
    this.Forename=Forename;
    }
    then,you can access or /and mutator whichever property of Person you want in the client code.

  6. The Following User Says Thank You to lewvan00 For This Useful Post:

    JavaNoob82 (December 8th, 2009)

  7. #5
    Junior Member
    Join Date
    Dec 2009
    Posts
    22
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Hey Guys new to Java.. Got most of program done require a little assistance plz

    cheers dude i have ammeded my program with your guidence thank you.. i need to set a counter also so that the person test prints out the number of person entered onto the screen so something like. persons enter = 5

    public class Person
    {
    		private String Forename;
    		private String Surname;
    		private int Age;
    		private int Height;
    		private int personID;
     
    	// Constructs a Person Test Class with the default value.
    	Person(String Forename,String Surname,int Age,int Height,int personID)
    	{
    		this.Forename=Forename;
    		this.Surname=Surname;
    		this.Age=Age;
    		this.Height=Height;
    		this.personID=personID;
    	}
    		// Gets the First Name of the Person in the PersonTest
    		public String getForename()
    	{
    		return this.Forename;
    	}
    		public void setForename(String Forename)
    	{
    		this.Forename=Forename;
    	}
     
    			// Gets the Last Name of the Person in the PersonTest
    		public String getSurname()
    	{
    		return this.Surname;
    	}
    		public void setSurname(String Surname)
    	{
    		this.Surname=Surname;
    	}	
    		// Gets the Height of the Person in the PersonTest
    		public double getHeight()
    	{
    		return this.Height;
    	}
    		public void setHeight()
    	{
    		this.Height=Height;
    	}	
    		// Gets the Age of the Person in the PersonTest
    		public int getAge ()
    	{
    		return this.Age;
    	}
    		public void setAge()
    	{
    		this.Age=Age;
    	}	
    		public void format()
    	{
    		System.out.println("person"+personID+"\tinfo:"+"\t" +Forename+ " \t"+Surname+"\t"+Age+"\t"+Height);
    	}		
    }

  8. #6
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Posts
    834
    My Mood
    Cynical
    Thanks
    7
    Thanked 105 Times in 90 Posts

    Default Re: Hey Guys new to Java.. Got most of program done require a little assistance plz

    Hi JavaNoob, wonder if steve minds you using this site

    Who are you at Bangor University?

    Edit: Btw, JavaPF, Json is you were wondering this is the work I get set lol! This guy/girl is from my university
    Last edited by Freaky Chris; December 13th, 2009 at 07:20 PM.

Similar Threads

  1. need help guys
    By Imeri0n in forum Java Theory & Questions
    Replies: 9
    Last Post: December 3rd, 2009, 09:39 AM
  2. FileReader need assistance
    By tazjaime in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: November 8th, 2009, 01:12 AM
  3. Convert Java Program to Java ME code
    By rinchan11 in forum Java ME (Mobile Edition)
    Replies: 1
    Last Post: October 5th, 2009, 10:18 PM
  4. Java Program Help
    By javakid93 in forum Java Theory & Questions
    Replies: 6
    Last Post: July 27th, 2009, 11:03 AM
  5. [SOLVED] Some important questions about java programming
    By chronoz13 in forum Java Theory & Questions
    Replies: 12
    Last Post: April 16th, 2009, 02:55 PM