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

Thread: Error in printing method as always it prints the last value

  1. #1
    Junior Member
    Join Date
    Sep 2009
    Posts
    11
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Error in printing method as always it prints the last value

    I'm having another problem with a homework assignment. We have to have 2 classes, one has the main program and the other has all the methods for the data received in the first class. Here is what I have. Everything works except the printInfo method always prints the data entered last, e.g. person 3

    The first code is my main class, the second is my methods class
    import java.util.*;
    public class FindYoungest {
    public static void main(String[] args){
    	Scanner stdin = new Scanner(System.in);
    	PersonInfo person1 = new PersonInfo();
    	PersonInfo person2 = new PersonInfo();
    	PersonInfo person3 = new PersonInfo();
     
     
    	//Get all info for person 1
    	System.out.println("Enter the name of the 1st person: ");
    	person1.setName(stdin.next());
    	System.out.println("Enter his/her RID: ");
    	person1.setRID(stdin.next());
    	System.out.println("Enter his/her age: ");
    	person1.setAge(stdin.nextInt());
    	System.out.println("Enter his/her favorite car: ");
    	person1.setCar(stdin.next());
     
    	//Get all info for person 2
    	System.out.println("Enter the name of the 2nd person: ");
    	person2.setName(stdin.next());
    	System.out.println("Enter his/her RID: ");
    	person2.setRID(stdin.next());
    	System.out.println("Enter his/her age: ");
    	person2.setAge(stdin.nextInt());
    	System.out.println("Enter his/her favorite car: ");
    	person2.setCar(stdin.next());
     
    	//Get all info for person 3
    	System.out.println("Enter the name of the 3rd person: ");
    	person3.setName(stdin.next());
    	System.out.println("Enter his/her RID: ");
    	person3.setRID(stdin.next());
    	System.out.println("Enter his/her age: ");
    	person3.setAge(stdin.nextInt());
    	System.out.println("Enter his/her favorite car: ");
    	person3.setCar(stdin.next());
     
    	//Determine who is youngest
    	if(person1.getAge() < person2.getAge() && person1.getAge() < person3.getAge()){
    		person1.printInfo();
    	}else if(person2.getAge() < person1.getAge() && person2.getAge() < person3.getAge()){
    		person2.printInfo();
    	}else
    		person3.printInfo();
    }
    }

    public class PersonInfo {
    	private static String name, RID, car;
    	private static int age;
     
    	public String getName(){
    		return name;
    	}	
    	public void setName(String getName) {
    		name = getName;
    	}
    	public String getRID(){
    		return RID;
    	}
    	public void setRID(String getRID) {
    		RID = getRID;
    	}
    	public int getAge(){
    		return age;
    	}
    	public void setAge(int getAge) {
    		age = getAge;
    	}
    	public String getCar(){
    		return car;
    	}
    	public void setCar(String getCar) {
    		car = getCar;
    	}
    	public void printInfo() {
    		System.out.println("The details for the youngest are:" +
    				"\nName: " + getName() + "\nRID: " + getRID() + "\nAge: " +
    				getAge() + "\nFavorite Car: " + getCar());
    	}
    }


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Homework help... beginning classes

    All you're code looks good... but here's the logic error that may or may not pop up:

    You're finding the youngest person by comparing to the other2 ages, which is fine and everything, but what happens when a person is the same age?

    This is an inherently undefined logical path, so you must decide what happens when two ages that are the same are entered. As of right now, if any of the ages are the same, it's printing out the 3rd person's info (hopefully you can see why, if not please say so and we'll tell you).

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

    TommyFiz (October 12th, 2009)

  4. #3
    Junior Member
    Join Date
    Sep 2009
    Posts
    11
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Homework help... beginning classes

    Thanks for that! No, I do see why, but that isn't the problem in this case. Well, it may be ONE problem, but not THE problem. When I run the program I make sure to enter different ages, and it's still printing the very last persons info. Any input on that? Thanks

  5. #4
    Little Star
    Join Date
    May 2009
    Posts
    30
    Thanks
    0
    Thanked 9 Times in 7 Posts

    Default Re: Homework help... beginning classes

    Its because in PersonInfo you setting all variables as static.
    Static means that no matter how many different person objects you will create, they all will have same value.
    Since you set person3 information at the end, it overrides everything that was there before, if you will do a test, and try to print out all the names, you will see that both person1 and person2 will have the same name as person3.

    Here is example of how it works
    Person1.setAge(10) //now all person objects have age set to 10. Person2.getAge() will return 10 too
    Person2.setAge(20) //now all person objects have age 20. Person
    Person3.setAge(100) //and now once again you change age for all the persons to 100
    //same with their name RID and other static variables.

    So don't use static variables here!
    ----

    And the other problem - what if you have two persons with same age? Example:
    Person1 is 10 years old.
    Person2 is 10 years old.
    Person3 is 20 years old.

    Your program will print out that person3 is the youngest:
    if Person1.age < Person2.age && Person2.age < Person3.age ----> 10 < 10 && 10 < 20 -----> False && True -----> False (this if will fail)
    else if Person2.age < Person1.age && Person2.age < Person3.age -----> 10 < 10 && 10 < 20 -----> False && True -----> False (else if will fail too)
    At the end else will print out third person information.

    Hope this helps,
    Dal
    Last edited by Dalisra; September 29th, 2009 at 05:37 PM.

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

    TommyFiz (October 12th, 2009)

  7. #5
    Junior Member
    Join Date
    Sep 2009
    Posts
    11
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Homework help... beginning classes

    Dalisra, thank you! Removing static from my variables worked perfectly.

    I didn't quite understand your explanation of fixing the formula at the end, but the assignment states "assuming all different ages." SO i'm assuming we're not that far into the learning process.

    Thanks again both of you for the help

  8. #6
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: Homework help... beginning classes

    Sounds like you need to read up on "What's the difference between a class and an object?"

    // Json

  9. #7
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: Homework help... beginning classes

    ahh it looks like you have encountered same problem as i had before ..

    no matter how many time you instantiate an object for your class it only prints the LAST ONE....

    I agree with Json.. and i woud like to specify it.. i think you should study a liitle bit about STATIC declaration..
    coz it will HELP YOU A LOT....

  10. The Following User Says Thank You to chronoz13 For This Useful Post:

    TommyFiz (October 12th, 2009)

Similar Threads

  1. need help with homework!
    By programmer12345 in forum Java Theory & Questions
    Replies: 2
    Last Post: September 27th, 2009, 05:34 AM
  2. Homework - using 'IF' for 'For Loops'
    By Enzo in forum Loops & Control Statements
    Replies: 5
    Last Post: September 16th, 2009, 10:52 AM
  3. Beginning java programmer!!!!!!!!!!!!!!!!!! need help
    By raidcomputer in forum Java Theory & Questions
    Replies: 3
    Last Post: September 15th, 2009, 08:52 PM
  4. [SOLVED] What is cast operator and how to use it?
    By napenthia in forum Java Theory & Questions
    Replies: 11
    Last Post: April 27th, 2009, 06:11 AM
  5. [SOLVED] Java program to prompt and display the user input number as odd or even
    By napenthia in forum Java Theory & Questions
    Replies: 11
    Last Post: April 22nd, 2009, 01:19 AM