Hi, how can I check whether a number that is transformed by a wrapper class is negative or not in Java?
I used the following code to check whether a input number is negative or not.
But it's not working.
Can anyone please help me?
import javax.swing.JFrame;
import javax.swing.JTextField;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
private JTextField theLength; //a type of JTextField
private JLabel theLabel //a type of JLabel
public static double stringToDouble(String stringObject)
{
return Double.parseDouble(stringObject.trim());
}
public void actionPerformed(ActionEvent e)
{
if ((stringToDouble(theLength.getText())) < 0)
{
theLabel.setText("the number is negative.");
}
}
Re: Hi, how can I check whether a number that is transformed by a wrapper class is negative or not in Java?
Please edit your post and wrap your code with
[code=java]
<YOUR CODE HERE>
[/code]
to get highlighting and preserve formatting.
What happens when the code is executed? Add some println statements to print out the values of the variables that are used and given values. For example: what String is passed to the stringToDouble() method? What value does the stringToDouble() method return?
Re: Hi, how can I check whether a number that is transformed by a wrapper class is negative or not in Java?