GetNumberpublic class GetNumber extends Frame GetNumber - program to determine if a number is float or int. |
Fields Summary |
---|
protected TextField | textFieldThe textfield to enter | protected double | dvalueThe value, if a double | protected int | ivalueThe value, if int |
Constructors Summary |
---|
public GetNumber()
setLayout(new FlowLayout());
add(new Label("Number:"));
add(textField = new TextField(10));
textField.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ev) {
String s = textField.getText();
//+
System.out.println("Input is " + s);
if (s.indexOf('.") >0 ||
s.indexOf('d") >0 || s.indexOf('e") >0)
try {
dvalue = Double.parseDouble(s);
System.out.println("It's a double: " + dvalue);
return;
} catch (NumberFormatException e) {
System.out.println("Invalid a double: " + s);
return;
}
else // did not contain . or d or e, so try as int.
try {
ivalue = Integer.parseInt(s);
System.out.println("It's an int: " + ivalue);
return;
} catch (NumberFormatException e2) {
System.out.println("Not a number:" + s);
}
}
//-
});
pack();
|
Methods Summary |
---|
public static void | main(java.lang.String[] ap)
new GetNumber().setVisible(true);
|
|