It looks like you're new here. If you want to get involved, click one of these buttons!
Issue:
There is an error with the system.out.println - says it does not work with integers. I cant find information about this. Any ideas?
Error:
myObject.setAge(JOptionPane.showInputDialog("Please enter age"));
System.out.println(myObject.getAge());
If you need an idea of what this is, have a look below, maybe it will help. At the very bottom you'll notice the clip above is bold to highlight where in the code my issue is.
package JavaProject1;
import javax.swing.JOptionPane;
/**
* javadoc comment
* this is my first attempt at a Java class.
* @author mgoodard
* all code has to go inside methods.
* Java compiles into bite-codes.
*/
public class MyClass {//all class names begin with upper-case letters.
// all variable names begin with a lower-case letter.
private String name; //an instance variable
//name is in the instance domain
private String color;
private int age; //int = integer
/**
* A getter for color.
* @return the color of this object(a string)
*/
public String getColor(){
return color;
}
/*
* A getter for age
* @return the age of the object (a string)
*/
public int getAge() {
return age;
}
/*
* A getter for name
* @return the name of user (a string)
*/
public String getName() {
return name;
}
/**
* A setter for color
* @param color The color to set this object's color to.
*/
public void setColor(String color){
this.color = color;
}
/*
* A setter for age
* @parm age The age to set the objects integer to.
*/
public void setAge(int age) {
this.age = age;
}
public void setName(String name) {
this.name = name;
}
// all constant names are all upper-case (public,static, classes)
public static final double TAX_RATE = 0.10;
//static = class domain
/**
* main method
* The starting point for all Java applications.
* This is required for all Java Applications
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
// all method names(e.g. main) begin with a lower-case letter.
MyClass myObject = new MyClass(); //an object instance of my class.
MyClass myObject2 = new MyClass(); //another object instance.
myObject.setColor("RED");
myObject2.setColor("BLUE");
String color1 = JOptionPane.showInputDialog("Please enter the color of the "
+ "first object");
String color2 = JOptionPane.showInputDialog("Please enter the color of the "
+ "second object");
// Processing
myObject.setColor(color1);
myObject2.setColor(color2);
// Output
JOptionPane.showMessageDialog(null,
"The color of myObject is " +myObject.getColor());
JOptionPane.showMessageDialog(null,
"The color of myObject is " +myObject2.getColor());
myObject.setName(JOptionPane.showInputDialog("Please enter name"));
System.out.println(myObject.getName());
myObject.setAge(JOptionPane.showInputDialog("Please enter age"));
System.out.println(myObject.getAge());
}
private void setintage(int age) {
throw new UnsupportedOperationException("Not yet implemented");
}
}//end of the class
KillerTwinkie - That one guy who used to mod mmorpg.com's forums.
Comments
Java uses static typing, does it not? You can't use an integer for a function that requires a string.
myObject.setAge(JOptionPane.showInputDialog("Please enter age"));
System.out.println(myObject.getAge().toString());
That should work, but I can't guarantee anything.
On the net, no one knows that I'm a dog.
System.out.println works with all primitives and Strings because it is an overloaded function.
The problem in his code above is...
myObject.setAge(JOptionPane.showInputDialog("Please enter age"));
setAge function accepts integers as parameters as defined here..
public void setAge(int age)
...and the method showInputDialog in the JOption pane returns a String, so the arguments don't match.
You ned to parse the integer. Do this:
myObject.setAge(Integer.parseInt(JOptionPane.showInputDialog("Please enter age")));