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

    Default Input program dilemma

    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..


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

  2. #2
    CodesAway's Avatar
    CodesAway is offline Senior Member
    Join Date
    Sep 2009
    Location
    Texas
    Posts
    238
    Rep Power
    5

    Default

    My first hint would be to ask a question. Usually forum readers don't respond to "here's my code, what's wrong with it?" type of posts. If you don't take the time to state, clearly, what you need help with, what's wrong with your code, and what you are looking for, then readers will pass your post up for a better written one.

    Second hint, post compilable code. Your code refers to a "NewPerson" class and a "PersonTest" class, with no code for either of them. This would be fine, except the first Class might be a misspelling - you might have meant "Person" (the class the main method is in). The second class looks like either a unit test class, or a "tester" class, meant to test your Person class. So, having a constructor in that class is very confusing, granted the code does demonstrate the goal of the constructor.

    Third hint, variable names. First off, you use the "i" variable for both your int array (which you try to store a "NewPerson" in, for some reason) and the index value, so first off the code won't work. Secondly, "i" should not be used as an array name; instead, use a name that describes what the variable is - your code should be self-documenting in this respect. The variable "i" is commonly used as an index variable (when iterating an array, for example), so using it in another way is confusing to others reading your code.

    Third hint, variable names, continued. Instead of using "FName", "LName", and your other variable names, use "firstName", "lastName", etc. They make the code more readable and there is no question what the variable contains. Also, variable names in Java should start with a lower-case letter (e.g. firstName), with class names starting with an upper-case.
    Last edited by CodesAway; 12-02-2009 at 08:08 PM.
    CodesAway - codesaway.info
    writing tools that make writing code a little easier

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

    Default

    ok cheers for that i have more code from the class i had made earlier, it compiles fine.

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

  4. #4
    ocean is offline Senior Member
    Join Date
    Sep 2009
    Location
    Sweden/Borås
    Posts
    107
    Rep Power
    0

    Default

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

    wont work couse i=0. And dont understand you question. You want a loop that counts how many ppl has added information in the program?
    Last edited by ocean; 12-02-2009 at 08:21 PM.

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

    Default

    i know.. i just cant get my head around it at the moment..

  6. #6
    ocean is offline Senior Member
    Join Date
    Sep 2009
    Location
    Sweden/Borås
    Posts
    107
    Rep Power
    0

    Default

    Yea know how that is :)

    Start with your array name.change it to something like

    Java Code:
    int [] countPerson = new NewPerson[5];

    Becouse you have "i" at both places the last "i" is the one that counts. And that makes i.length = 0. and not 5 as you probly want.

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

    Default

    hey ocean many thanks for the tip. my loop is kind of workin lol basically my loop is asking the name five times instead of just askin for 5 forenames in once instance. could there be a way of inputting the 5 names,ages etc in one go when running my program?
    Java Code:
    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 to store in 5 People into my program
    		int []countPerson = new int [5];	
    		
    		
    		for (int count = 0; count <= countPerson.length ; count++)
    		{
    			
    		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));
    		}
    
    			
    	}
    }

  8. #8
    ocean is offline Senior Member
    Join Date
    Sep 2009
    Location
    Sweden/Borås
    Posts
    107
    Rep Power
    0

    Default

    Dont understand what you want. Can you explain little better please

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

    Default

    hey ocean no problems what i want my program do do is

    create a loop so that it asks the program to enter 5 fornames
    then surnames
    then ages
    then heights after that is completed to print out what was entered and number of persons entered were..

    hope you can help ocean =)

Similar Threads

  1. Input technique for unknown lines of input
    By ducreative in forum New To Java
    Replies: 16
    Last Post: 09-23-2009, 09:26 AM
  2. Replies: 4
    Last Post: 10-02-2008, 10:05 PM
  3. Dilemma netbeans or eclipse
    By Nick15 in forum NetBeans
    Replies: 8
    Last Post: 11-08-2007, 04:11 PM
  4. how to take input and verify input in Java programs
    By bilal_ali_java in forum Advanced Java
    Replies: 0
    Last Post: 07-21-2007, 08:46 AM
  5. Help with program input
    By Daniel in forum New To Java
    Replies: 1
    Last Post: 07-05-2007, 05:02 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
  •