Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

empty string error 1

Status
Not open for further replies.

vonehle

Programmer
May 16, 2006
35
0
0
US
I have marked exactly where my error is occurring. I'm trying to check the contents of a textbox to see if the user entered a number. When I run the program, and click the "Calculate" button, I should get a JOptionPane error box. Instead, I get a long error in the command prompt starting with a NumberFormatException: empty string. Could someone please tell me why this is not being caught by strSquare.length?

Code:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.math.*;

public class SquareRoots extends JFrame
{
	public static void main(String[] args)
	{
		new SquareRoots();
	}

	private JPanel upper, middle, lower;
	private JTextField entry, output;
	private JButton calculate, close;
	double dblSquare, result;
	private String strSquare;
	private int intSquare;

	public SquareRoots()
	{
		super("Square Root Calculator");
		ButtonListener b1 = new ButtonListener();
		ButtonListener b2 = new ButtonListener();
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		upper = new JPanel();
		upper.setBorder(BorderFactory.createTitledBorder("Enter a number"));
		upper.setLayout(new BorderLayout());
		entry = new JTextField();
		upper.add(entry, BorderLayout.CENTER);

		middle = new JPanel();
		middle.setBorder(BorderFactory.createTitledBorder("The Square Root is "));
		//middle.setLayout(new FlowLayout(FlowLayout.CENTER));
		middle.setLayout(new BorderLayout());
		output = new JTextField();
		middle.add(output, BorderLayout.CENTER);

		lower = new JPanel();
		lower.setLayout(new FlowLayout(FlowLayout.RIGHT));
		calculate = new JButton("Calculate");
		calculate.addActionListener(b1);
		lower.add(calculate);
		close = new JButton("Exit");
		close.addActionListener(b2);
		lower.add(close);

		Container cp = getContentPane();
		cp.add(upper, BorderLayout.NORTH);
		cp.add(middle, BorderLayout.CENTER);
		cp.add(lower, BorderLayout.SOUTH);
		pack();
		setVisible(true);
	}

		class ButtonListener implements ActionListener
		{
			public void actionPerformed(ActionEvent e) 
			{
				if (e.getSource() == close)
				{
					System.exit(0);
				}


				if (e.getSource() == calculate)
				{
					String strSquare = entry.getText();
					dblSquare = Double.valueOf(strSquare.trim()).doubleValue();

					if (strSquare.length() == 0)  //THIS IS NOT WORKING CORRECTLY
					{

						JOptionPane.showMessageDialog(
						   SquareRoots.this, "You didn't enter a number.",
						   "OOPS!", JOptionPane.INFORMATION_MESSAGE);

					}

					else
					{
						result = Math.sqrt(dblSquare);
						output.setText(" " + result);
					}


				}
			}
		}

	}
 
If you look at the JavaDocs for the Double.valueOf method you will see that it throws :-
NumberFormatException - if the string does not contain a parsable number

So you need a try/catch around your Double.valueOf method call catching a NumberFormatException, and the catch part could be used to trigger your message box.

Note that the compiler does not complain about the lack of such a try/catch since a NumberFormatException is derived from a RuntimeException, a type of exception which doesn't have to be declared as thrown. These exceptions will just 'bubble' back up the call stack until you eventually do catch them, or they pop out of the top and kill the program.

Tim
 
Thank you very much. I tried a try/catch earlier and it didn't work. Not sure what I did different this time, but it worked.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top