FileDocCategorySizeDatePackage
GetNumber.javaAPI DocExample1685Sat Mar 06 15:59:34 GMT 2004None

GetNumber

public class GetNumber extends Frame
GetNumber - program to determine if a number is float or int.
author
Ian Darwin, http://www.darwinsys.com/
version
$Id: GetNumber.java,v 1.6 2004/03/06 21:59:33 ian Exp $

Fields Summary
private TextField
textField
The input textField
private TextField
statusLabel
The results area
private static Number
NAN
Constructors Summary
public GetNumber()
Constructor: set up the GUI

		Panel p = new Panel();
		p.add(new Label("Number:"));
		p.add(textField = new TextField(10));
		add(BorderLayout.NORTH, p);
		textField.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent ev) {
				String s = textField.getText();
				statusLabel.setText(process(s).toString());
			}
		});
		add(BorderLayout.SOUTH, statusLabel = new TextField(10));
		pack();
	
Methods Summary
public static voidmain(java.lang.String[] ap)

		new GetNumber().setVisible(true);
	
public java.lang.Numberprocess(java.lang.String s)


	/* Process one String, returning it as a Number subclass
	 * Does not require the GUI.
	 */
	    
		if (s.matches(".*[.dDeEfF]")) {
			try {
				double dValue = Double.parseDouble(s);
				System.out.println("It's a double: " + dValue);
				return new Double(dValue);
			} catch (NumberFormatException e) {
				System.out.println("Invalid a double: " + s);
				return NAN;
			}
		} else // did not contain . d e or f, so try as int.
			try {
				int iValue = Integer.parseInt(s);
				System.out.println("It's an int: " + iValue);
				return new Integer(iValue);
			} catch (NumberFormatException e2) {
				System.out.println("Not a number:" + s);
				return NAN;
			}