FileDocCategorySizeDatePackage
GetNumber.javaAPI DocExample1440Sat Nov 25 12:56:02 GMT 2000None

GetNumber

public class GetNumber extends Frame
GetNumber - program to determine if a number is float or int.
author
Ian Darwin, ian@darwinsys.com
version
$Id: GetNumber.java,v 1.4 2000/11/25 17:56:02 ian Exp $

Fields Summary
protected TextField
textField
The textfield to enter
protected double
dvalue
The value, if a double
protected int
ivalue
The 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 voidmain(java.lang.String[] ap)

		new GetNumber().setVisible(true);