Results 1 to 5 of 5
Like Tree1Likes
  • 1 Post By pbrockway2

Thread: Problem Changing an Object's Instance Variable

  1. #1
    stdq is offline Member
    Join Date
    Jul 2012
    Posts
    3
    Rep Power
    0

    Default Problem Changing an Object's Instance Variable

    Hello, everyone! I created a class called Person, with one attribute called nameOfPerson; I created another class, called Test, to test the class Person: it creates an object from class Person, initializing its field through its constructor. The object is created sucessfully. Then, class Test asks the user to change the name by pressing 1 - however, if 1 is entered, the setNameOfPerson method from class Person is called correctly, but the program doesn't wait for the user to enter the new name, and the name is then print as a character space. The classes are:

    Java Code:
    /** Class Person. */
    
    public class Person
    {
        /** Instance variable: */
        private String nameOfPerson;
    
        public Person(String name)
        {
            nameOfPerson = name;
        }
        
        public String getNameOfPerson()
        {
            return nameOfPerson;
        }
        
        public void setNameOfPerson(String newName)
        {
            nameOfPerson = newName;
        }
    }
    Java Code:
    /** Class Test. */
    
    import java.util.Scanner;
    
    public class Test
    {
        public static void main(String[] args)
        {
            /** Creates an object from class Person: */
            Person person1 = new Person("Robert Johnson");
            
            /** Will be used to receive the option chosen by the user: */
            int option;
            
            /** Scanner object: */
            Scanner dataEntry = new Scanner(System.in);
            
            /** Will be used to change Person's name: */
            String newName;
            
            /** Prints the initial name of the person: */
            System.out.printf("The name of the person is %s.", person1.getNameOfPerson());
            System.out.println();
            
            /** Asks the user to change the person's name or not: */
            System.out.print("Please enter 1 to change the person's name: ");
            option = dataEntry.nextInt();
            
            if ( option == 1 )
            {
                /** Receives a new name from the keyboard: */
                System.out.print("Please enter the person's new name: ");
                newName = dataEntry.nextLine();
                
                /** Updates the name of the person: */
                person1.setNameOfPerson(newName);
            }
            
            /** Prints the current name of the person: */
            System.out.printf("The name of the person ");
            System.out.printf("is %s.", person1.getNameOfPerson());
            System.out.println();
        }
    }
    The output is:

    The name of the person is Robert Johnson.
    Please enter 1 to change the person's name: 1
    Please enter the person's new name: The new name of the person is .


    What is wrong with the program? Thanks in advance!

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Problem Changing an Object's Instance Variable

    the name is then print as a character space
    From the look of the output you posted it would be more accurate to say that the name is printed as an empty string. (The space that appears in the output doesn't come from the name)

    the program doesn't wait for the user to enter the new name
    This is the heart of the problem. dataEntry is an instance of Scanner and scanners sometimes wait and sometimes do not. They will only wait if they have to.

    If you call nextLine() and System.in does not contain a new line then the scanner will wait until you type one. Then it returns everything up to, but not including, the new line and clears the new line out of the System.in stream.

    On the other hand if System.in does already contain a new line the scanner will not wait: it will, as before, return everything up to, but not including, the new line and clear the new line out of the System.in stream.

    (Details of all this are in the Scanner API docs)

    So the behaviour of the scanner depends on what is already in the System.in stream and to see why your program is behaving as it is you need to go back a step to examine what the scanner has already done.

    Java Code:
    option = dataEntry.nextInt();
    This line reads a token from System.in (removing it from the stream) and returns the corresponding integer value. The important thing to realise is that following the token it read is a new line character. And the scanner leaves this character in the stream.

    So when you subsequently call nextLine() the scanner returns an empty string (ie everything up to the new line character in the stream) and removes the new line from the stream. Importantly it does not have to wait, and will not wait, before doing this.

    The solution is to remove that new line character from the stream before you ever call nextLine() to get the data. For instance you can can call nextLine() straight after calling nextInt() (and ignore the empty string it returns). This will "clear out" the new line character from the stream and subsequent calls of nextLine() will behave as you expect: waiting and returning a line of data.
    Norm likes this.

  3. #3
    stdq is offline Member
    Join Date
    Jul 2012
    Posts
    3
    Rep Power
    0

    Default Re: Problem Changing an Object's Instance Variable

    Thank you very much, pbrockway2! I did what you said and now the program waits for me to enter the new name. Regards!

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Problem Changing an Object's Instance Variable

    You're welcome.

    I hope the rather rambling explanation made sense because you have to be alert to the fact that other methods besides nextInt() can leave a new line sitting in the stream to cause unexpected behaviour next time nextLine() is called.

  5. #5
    stdq is offline Member
    Join Date
    Jul 2012
    Posts
    3
    Rep Power
    0

    Default Re: Problem Changing an Object's Instance Variable

    I see. That'll help me in future programs. Thanks again!

Similar Threads

  1. Replies: 1
    Last Post: 07-07-2012, 10:12 PM
  2. Replies: 1
    Last Post: 04-21-2012, 06:03 PM
  3. Replies: 0
    Last Post: 08-14-2010, 10:09 AM
  4. Instance Variable In Servlet
    By javarishi in forum Java Servlet
    Replies: 3
    Last Post: 06-14-2008, 08:28 AM
  5. Instance variable
    By Jack in forum New To Java
    Replies: 2
    Last Post: 07-04-2007, 04:00 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
  •