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.

object

public class CircleObj {

	public void Constructor(){
	
		System.out.print("I work");

		
	}

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

main program

import java.util.Scanner;
public class UseCircles {

	public static void main(String[] args) {

		Scanner input = new Scanner( System.in );
		
		CircleObj myCircleObj = new CircleObj();
		
		System.out.println ("Please enter value for X:" );
		String theX = input.nextLine();
		myCircleObj.setVarX( theX );
		
		myCircleObj.displayMessage();
	}

}

this is my output:

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

_____________________

how come that 9 wont show up??

Recommended Answers

All 4 Replies

Looks like you have the variables set up the wrong way around

#
public void setVarX( String X ){
#
X = VarX;}

Try it this way

#
public void setVarX( String X ){
#
 VarX = X ;}

hmm, i changed them around and i am still getting the same output

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

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

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.

Try:

System.out.printf("The Value for X is: " + VarX);

// OR

System.out.println("The Value for X is: " + VarX);

I am not familiar with printing using: printf. I know that it is used to format the output, but I have never used it before. You might want to try the API:

printf

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.