Results 1 to 11 of 11
  1. #1
    shroomiin is offline Member
    Join Date
    Sep 2009
    Posts
    31
    Rep Power
    0

    Default Simple question about objects.

    Hello all, i was just introduced to objects and am trying to construct a program using get/set. i just want to set the user read variable and read it back. I have it almost figured out but for some reason it will not display anything for the value. heres what i have.

    Java Code:
    public class CircleObj {
    
     CircleObj(int varX){
    		
    	}
    
    
    	private String VarX;
    	
    	public void setVarX( String X ){
    		VarX = X;}
    		
    	public String getVarX(){
    		return VarX;}
    			
    	public void displayMessage(){
    	
    		System.out.printf("The Value for X is: ", VarX);
    		
    	}	
    }

    and

    Java Code:
    import java.util.Scanner;
    public class UseCircles {
    
    	public static void main(String[] args) {
    
    		Scanner input = new Scanner( System.in );
    		
    		CircleObj myCircleObj = new CircleObj(0);
    		
    		System.out.println ("Please enter value for X:" );
    		String VarX = input.nextLine();
    		myCircleObj.setVarX( VarX );
    		
    		myCircleObj.displayMessage();
    	}
    
    }
    hmmm.


    this is my output:

    Please enter value for X:
    9
    The Value for X is:

    _____________________

    how come that 9 wont show up??
    Last edited by shroomiin; 10-10-2009 at 01:33 AM.

  2. #2
    shroomiin is offline Member
    Join Date
    Sep 2009
    Posts
    31
    Rep Power
    0

    Default

    nobody knows?

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

    Default

    Quote Originally Posted by shroomiin View Post
    nobody knows?
    Or maybe they had other things to do for the last 45 minutes? Unforgiveable given your question, but there you go.

    A couple of things:

    First the output you posted does not include the line "I work" and that strikes me as a big problem, although you didn't mention it.

    Java Code:
    public void Constructor(){
        System.out.print("I work");
    }

    I assume that's supposed to be a constructor for the CircleObj class. Compare it with the constructors in your notes/textbook/whatever. Do constructors have a return type ("void")? What should the name of the constructor ("Constructor") really be if it supposed to be a constructor for the CircleObj class?

    Secondly,

    Java Code:
    public void setVarX( String X ){
        X = VarX;
    }

    That line assigns the value of VarX to X which is not quite what you mean. Read (or think about) assignments right-to-left:

    to this <--- this gets assigned

  4. #4
    shroomiin is offline Member
    Join Date
    Sep 2009
    Posts
    31
    Rep Power
    0

    Default

    well i appologize im just stressed out with all of this. i just started learning about objects haha. but thanks ill see what i can do

    but yes i realized i had to change that, i have this now

    Java Code:
    public void setVarX( String X ){
    		VarX = X;}
    and also the "i work" was my mistake. i used that to test if the object worked correctly at first.
    Last edited by shroomiin; 10-10-2009 at 12:55 AM.

  5. #5
    shroomiin is offline Member
    Join Date
    Sep 2009
    Posts
    31
    Rep Power
    0

    Default

    well, this is what i have now. modified a lot but still wont display the text.

    Java Code:
    public class CircleObj {
    
     CircleObj(int varX){
    		
    	}
    
    
    	private String VarX;
    	
    	public void setVarX( String X ){
    		VarX = X;}
    		
    	public String getVarX(){
    		return VarX;}
    			
    	public void displayMessage(){
    	
    		System.out.printf("The Value for X is: ", VarX);
    		
    	}	
    }

    and

    Java Code:
    import java.util.Scanner;
    public class UseCircles {
    
    	public static void main(String[] args) {
    
    		Scanner input = new Scanner( System.in );
    		
    		CircleObj myCircleObj = new CircleObj(0);
    		
    		System.out.println ("Please enter value for X:" );
    		String VarX = input.nextLine();
    		myCircleObj.setVarX( VarX );
    		
    		myCircleObj.displayMessage();
    	}
    
    }
    hmmm.

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

    Default

    Java Code:
    System.out.printf("The Value for X is: ", VarX);

    You forgot something in the "The Value is..." string! Where is the bit that tells it to actually print VarX?

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

    Default

    And I assume you really did mean printf() and not println(). Both are OK, but you get the VarX to actually print in slightly different ways.

  8. #8
    shroomiin is offline Member
    Join Date
    Sep 2009
    Posts
    31
    Rep Power
    0

    Default

    changed the line to:

    Java Code:
    		System.out.printf("The Value for X is: %s\n", VarX);
    and it seems to be working. is that what you meant?

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

    Default

    Yes, exactly.

  10. #10
    shroomiin is offline Member
    Join Date
    Sep 2009
    Posts
    31
    Rep Power
    0

    Default

    thanks for the help!

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

Similar Threads

  1. Replies: 0
    Last Post: 03-10-2009, 09:55 AM
  2. Simple Question
    By barusk in forum Networking
    Replies: 13
    Last Post: 03-04-2009, 07:33 PM
  3. Replies: 5
    Last Post: 02-25-2009, 07:14 PM
  4. Creating objects question
    By sergm in forum New To Java
    Replies: 2
    Last Post: 12-27-2007, 04:10 PM
  5. Probably a really simple question...
    By ibanez270dx in forum New To Java
    Replies: 0
    Last Post: 11-16-2007, 01:27 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
  •