Results 1 to 5 of 5
- 07-30-2012, 01:15 AM #1
Member
- Join Date
- Jul 2012
- Posts
- 3
- Rep Power
- 0
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; } }The output is: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 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!
- 07-30-2012, 01:41 AM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,546
- Rep Power
- 11
Re: Problem Changing an Object's Instance Variable
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 name is then print as a character space
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.the program doesn't wait for the user to enter the new name
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.
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.Java Code:option = dataEntry.nextInt();
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.
- 07-30-2012, 02:08 AM #3
Member
- Join Date
- Jul 2012
- Posts
- 3
- Rep Power
- 0
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!
- 07-30-2012, 02:17 AM #4
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,546
- Rep Power
- 11
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.
- 07-30-2012, 02:20 AM #5
Member
- Join Date
- Jul 2012
- Posts
- 3
- Rep Power
- 0
Similar Threads
-
how to change an instance variable of an object from multiple arraylist references?
By smande3 in forum New To JavaReplies: 1Last Post: 07-07-2012, 10:12 PM -
Creating an instance variable in one class that connects to another instance variable
By SpicyElectricity in forum New To JavaReplies: 1Last Post: 04-21-2012, 06:03 PM -
Changing binding when changing the underlying model object
By ChrisNY in forum NetBeansReplies: 0Last Post: 08-14-2010, 10:09 AM -
Instance Variable In Servlet
By javarishi in forum Java ServletReplies: 3Last Post: 06-14-2008, 08:28 AM -
Instance variable
By Jack in forum New To JavaReplies: 2Last Post: 07-04-2007, 04:00 AM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks