2 Replies - 204 Views - Last Post: 10 October 2012 - 12:06 AM Rate Topic: -----

#1 holidaze  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 27-September 12

Overwrite variable data using toString (Involves JPane and objects)

Posted 09 October 2012 - 10:47 PM

Ive been working on this code for the past couple days and have run into one final problem and can't seem to figure out what the issue is. The panes are based off a command-line argument. A example of what the panes should display is:

Argument entered: Jessica 22
Pane 1
Jessica is 22 years old.
Pane 2
Dr. Jessica is 23 years old.

For some reason after I recall the toString method, the output is still not changed. Am i calling the phd/birthday method wrong or is the phd/birthday methods coding incorrectly?

Thanks,

   import javax.swing.JOptionPane;

   public class PersonCall {
      public static void main(String[ ] args) {
      	
      	//Parse Integers entered on the command-line
         int age1 = Integer.parseInt(args[1]);
      	
       	//creates two persons objects
         Person person1 = new Person(args[0], age1);
      	
      	//Prints the first JPane with relative data fields
         String display1 = person1.toString();
         JOptionPane.showMessageDialog(null, display1);
      	
      	//Calls phd and birthday method
         person1.birthday();//Calls birthday method
         person1.phd();//Calls phd method
	 display1 = person1.toString();//Calls toString to refresh new data.
         JOptionPane.showMessageDialog(null, display1);
      
      }//end of class
   }
	
/*Stores the name and age of the person*/
   class Person{
	
    //data fields that store each object's data
      private String name;
      private int age;
   	
   /** Constructor - Used To Create Each Object & Initialize Data Fields.
   * @param n1 is the Persons name 
   * @param a1 is the Persons age*/  
      public Person(String n1, int a1){
         name = n1;
         age = a1; 
      }
   	
   //Used to Display The Data Stored In Each Object's Data Field.
      public String toString(){
         String personString = name + " is " + age + " years old.";
         return personString;
      }
		
   //Method used to display age 	
      public void phd(){
         String n1 = "Dr. " + name;
      }
		
   //Method used to display age 	
      public void birthday(){
         Integer a1 = age + 1;
      }
   }



Is This A Good Question/Topic? 0
  • +

Replies To: Overwrite variable data using toString (Involves JPane and objects)

#2 CasiOo  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 1398
  • View blog
  • Posts: 3,097
  • Joined: 05-April 11

Re: Overwrite variable data using toString (Involves JPane and objects)

Posted 10 October 2012 - 12:01 AM

You are having the same problem in both of your methods

Instead of assigning then new value to your instance variable, you assign it to a local variable

	
public void phd(){
	//n1 is local
	String n1 = "Dr. " + name;
	
	//'name' is our instance variable
	//Notice how we do not write the type again
	name = "Dr. " + name;
	
	//Here we write the type, this means it is a local variable to the method
	String name;
}



Was This Post Helpful? 1
  • +
  • -

#3 holidaze  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 27-September 12

Re: Overwrite variable data using toString (Involves JPane and objects)

Posted 10 October 2012 - 12:06 AM

View PostCasiOo, on 10 October 2012 - 12:01 AM, said:

You are having the same problem in both of your methods

Instead of assigning then new value to your instance variable, you assign it to a local variable

	
public void phd(){
	//n1 is local
	String n1 = "Dr. " + name;
	
	//'name' is our instance variable
	//Notice how we do not write the type again
	name = "Dr. " + name;
	
	//Here we write the type, this means it is a local variable to the method
	String name;
}




I understand now. Thank you for your help.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1