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

Thread: How to get constructed

  1. #1
    Junior Member
    Join Date
    Mar 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default How to get constructed

    I am currently working on an assignment where we have to make a class that encapsulates a person’s name in such a way that a program will run off it. Now, if it was just 2 classes I could get it to work fine, but the main program creates multiple objects, and these new objects are supposed to refer to one another for information. And this I am not sure how to do.

    For example, the first part of the program creates the object newName, and then has one enter information defining the variables FirstName, MiddleName, and LastName.
    Then it creates the object myName. At this point it is supposed to access the previous class, obtain the variable values from there, store them in the new class, and print them out. And, like I said, it's accessing the other class that I'm not sure how to do.

    The Line in question:
    Name myName = new Name(newName.getFirstName(), newName.getMiddleName(), newName.getLastName());

    Edit: and now I realize I screwed up the title. Probably means I should get to sleep sooner rather than later.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: How to get constructed

    What problems are you having with that line of code? Does it compile?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Mar 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to get constructed

    Quote Originally Posted by Norm View Post
    What problems are you having with that line of code? Does it compile?
    Ah, sorry. The program compiles, and even runs. The issue is that it doesn't return the right values.

    Here's more of the complete code.

    Main Code(can not alter this, and there is more, but my issue isn't with those areas directly)
    import java.util.Scanner;
     
    public class Project1NameClass {
     
     
    	public static void main(String[] args)
    	{
    		Name newName = new Name();
    		System.out.println("Please enter a complete name in the following format. \nFirstname Middlename Lastname");
    		getName(newName);
    		Name myName = new Name(newName.getFirstName(), newName.getMiddleName(), newName.getLastName());
    		System.out.println("You entered: ");
    		printNameOut(myName);}
     
    static void getName(Name newName)
    	{
    		Scanner keyboard = new Scanner(System.in);
    		newName.setFirstName(keyboard.next());
    		newName.setMiddleName(keyboard.next());
    		newName.setLastName(keyboard.next());
    		keyboard.nextLine();
    	}
    static void printNameOut(Name printName)
    {
    	System.out.println(printName.getFirstName() + " " +
    			printName.getMiddleName() + " " +
    			printName.getLastName());
    }
    }

    Class Code(can alter this)
    import java.util.Scanner;
    public class Name {
    	Scanner keyboard = new Scanner(System.in);
    	/**
    	 * @param args
    	 */
     
    		private String FirstName;
    		private String MiddleName;
    		private String LastName;
     
    	public Name ()
    		{
     
    		}	
    	public Name ( String FirstName, String MiddleName, String LastName)
    	{
     
     
    	}
    	public String getFirstName ()
    	{
    		return FirstName;
     
    	}
    	public String getMiddleName ()
    	{
    		return MiddleName;
     
    	}
    	public String getLastName ()
    	{
     
    		return LastName;
    	}
    	public void setFirstName (String newName)
    	{
    		FirstName = newName;
    	}
    	public void setMiddleName (String newName)
    	{
    		MiddleName = newName;
    	}
    	public void setLastName (String newName)
    	{
    		LastName = newName;
    		}}

    The output of printNameOut(myName) should be the values input for newName. Instead I'm getting null, null, null. Though if I put the method printNameOut(newName), I get the right values.

  4. #4
    Junior Member
    Join Date
    Mar 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to get constructed

    Welp, after more looking over my books, I feel pretty stupid, but I think I've solved the immediate problem.

Similar Threads