Hi all,
I am trying to write a simple program but I am having an error message that I am working on it for three days. The aim of program is to convert entered value depending on the clicked radio buttons. Please can someone help me about it ?
Thanks,

The error message is as below:

Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: empty String
        at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:994)
        at java.lang.Double.parseDouble(Double.java:510)
        at converter.ValuePanel.getLatEntered(ValuePanel.java:29)
        at converter.ConverterButtons.GetResult(ConverterButtons.java:46)
        at converter.ConvertGUI$CalcButtonListener.actionPerformed(ConvertGUI.java:47)
        at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
        at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
        at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
        at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
        at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
        at java.awt.Component.processMouseEvent(Component.java:6216)
        at javax.swing.JComponent.processMouseEvent(JComponent.java:3265)
        at java.awt.Component.processEvent(Component.java:5981)
        at java.awt.Container.processEvent(Container.java:2041)
        at java.awt.Component.dispatchEventImpl(Component.java:4583)
        at java.awt.Container.dispatchEventImpl(Container.java:2099)
        at java.awt.Component.dispatchEvent(Component.java:4413)
        at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4556)
        at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4220)
        at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4150)
        at java.awt.Container.dispatchEventImpl(Container.java:2085)
        at java.awt.Window.dispatchEventImpl(Window.java:2475)
        at java.awt.Component.dispatchEvent(Component.java:4413)
        at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
        at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
        at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
        at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
        at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)

Here is the code for Double.java:510:

package converter;
import javax.swing.*;
import java.awt.*;
public class ValuePanel extends JPanel {
    private JLabel valLabel;
    private JTextField valTextField;
    public ValuePanel()
    {
        setLayout(new GridLayout(1,2));
        valLabel = new JLabel("Enter Latitude");
        valTextField = new JTextField(10);        
        setBorder(BorderFactory.createTitledBorder("Latitude"));
        add(valLabel);
        add(valTextField);
    }

        public double getLatEntered()
        {
            //valTextField.setText("0");
            String input;
            double latitude;
            input = valTextField.getText();
            latitude = Double.parseDouble(input);
            return latitude;

        }
}

Recommended Answers

All 8 Replies

I assume that when you ran the code after you set the text with setText it ran just fine? A really quick way to debug this code would be to use a try catch block to find out what is going on when you parse your string.

public double getLatEntered()
{
	//valTextField.setText("0");
	String input;
	double latitude;
	input = valTextField.getText();
	try {
		latitude = Double.parseDouble(input);
	}
	catch ( Exception E ) {
		//find a place to out put the variable input
		//so you can see what the value is here
		latitude = 0.0;
	}
	
	return latitude;
	
}

I assume that when you ran the code after you set the text with setText it ran just fine? A really quick way to debug this code would be to use a try catch block to find out what is going on when you parse your string.

public double getLatEntered()
{
	//valTextField.setText("0");
	String input;
	double latitude;
	input = valTextField.getText();
	try {
		latitude = Double.parseDouble(input);
	}
	catch ( Exception E ) {
		//find a place to out put the variable input
		//so you can see what the value is here
		latitude = 0.0;
	}
	
	return latitude;
	
}

Hi nexocentric,

Thank you very much for your help. When I ran the program it always returns 0.0 I enter different values but it only returns 0.0 so do you have any idea about the solution?
Thank you,

Were you able to find out what's happening to the variable "input" by outputting it's value in the try catch block? Also if possible could you post the rest of your code so I can run it over here and check it really quick?

Were you able to find out what's happening to the variable "input" by outputting it's value in the try catch block? Also if possible could you post the rest of your code so I can run it over here and check it really quick?

Dear nexocentric,
Sorry for this but I am new to programming this is my first application and ı could not do it. I have four class files for this simple program;

1st :Name of the file is ValuePanel.java

package converter;

import javax.swing.*;
import java.awt.*;
public class ValuePanel extends JPanel {
    private JLabel valLabel;
    private JTextField valTextField;
    public ValuePanel()
    {
        setLayout(new GridLayout(1,2));
        valLabel = new JLabel("Enter Latitude");
        valTextField = new JTextField(10);        
        setBorder(BorderFactory.createTitledBorder("Latitude"));
        add(valLabel);
        add(valTextField);
    }

        public double getLatEntered()
        {
            //valTextField.setText("0");
            String input;
            double latitude;
            input = valTextField.getText();
            try{
                latitude = Double.parseDouble(input);
            }
            catch (Exception E){
                latitude = 0.0;
            }

            return latitude;

        }
}

2nd : Name of the file is :ConverterButtons.java

package converter;

import javax.swing.*;
import java.awt.*;

public class ConverterButtons extends JPanel {
    private ValuePanel velugir;
    private JRadioButton fromGeodetic;
    private JRadioButton fromGeodesic;
    private JRadioButton fromReduced;
    private JRadioButton toGeodetic;
    private JRadioButton toGeodesic;
    private JRadioButton toReduced;
    private ButtonGroup bgFrom;
    private ButtonGroup bgTo;

    public ConverterButtons(){
        setLayout(new GridLayout(3,2));
        fromGeodetic = new JRadioButton("Geodetic",true);
        fromGeodesic = new JRadioButton("Geodesic");
        fromReduced = new JRadioButton("Reduced");
        toGeodetic = new JRadioButton("Geodetic");
        toGeodesic = new JRadioButton("Geodesic",true);
        toReduced = new JRadioButton("Reduced");
        bgFrom = new ButtonGroup();
        bgFrom.add(fromGeodetic);
        bgFrom.add(fromGeodesic);
        bgFrom.add(fromReduced);
        bgTo = new ButtonGroup();
        bgTo.add(toGeodetic);
        bgTo.add(toGeodesic);
        bgTo.add(toReduced);
        setBorder(BorderFactory.createTitledBorder("Convert From - To"));
        add(fromGeodetic);
        add(toGeodetic);
        add(fromGeodesic);
        add(toGeodesic);
        add(fromReduced);      
        add(toReduced);
    }
    public double GetResult(){
        velugir = new ValuePanel();
        double result;
        //double girilen;
        //girilen = velugir.getLatEntered();
        //double radians = Math.toRadians(girilen);
        //double a = 5.0;
        //double b = 3.0;
        double denesene = velugir.getLatEntered();

        /*if (fromGeodetic.isSelected() && toGeodesic.isSelected())
            result = Math.atan((b*b*Math.tan(radians))/(a*a));
        else if (fromGeodetic.isSelected() && toReduced.isSelected())
            result = Math.atan((b*Math.tan(radians))/a);
        else if(fromGeodesic.isSelected() && toGeodetic.isSelected())
            result = Math.atan((a*a*Math.tan(radians))/(b*b));
        else if (fromGeodesic.isSelected() && toReduced.isSelected())
            result = Math.atan((a*Math.tan(radians))/b);
        else if (fromReduced.isSelected() && toGeodesic.isSelected())
            result = Math.atan((b*Math.tan(radians))/a);
        else if (fromReduced.isSelected() && toGeodetic.isSelected())
            result = Math.atan((a*Math.tan(radians))/b);
        else
            result = a*b;*/
        result = denesene;
        return result ;
    }
}

3rd: Name of the file is :ConvertGUI.java

package converter;
import javax.swing.*;
import javax.swing.JOptionPane;
import java.awt.*;
import java.awt.event.*;
//import java.text.DecimalFormat;

public class ConvertGUI extends JFrame {
    private ValuePanel valuegirSon;
    private ConverterButtons radiosSon;
    private JPanel buttonPanel;
    private JButton calcButton;
    private JButton exitButton;

    public ConvertGUI()
    {
        setTitle("Latitude converter");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(new BorderLayout());
        valuegirSon = new ValuePanel();
        radiosSon = new ConverterButtons();
        buildButtonPanel();
        add(valuegirSon, BorderLayout.NORTH);
        add(radiosSon, BorderLayout.CENTER);
        add(buttonPanel, BorderLayout.SOUTH);
        pack();
        setVisible(true);
    }

    private void buildButtonPanel()
    {
        buttonPanel = new JPanel();
        calcButton = new JButton("Calculate");
        exitButton = new JButton("exit");
        calcButton.addActionListener(new CalcButtonListener());
        exitButton.addActionListener(new ExitButtonListener());
        buttonPanel.add(calcButton);
        buttonPanel.add(exitButton);
    }

    private class CalcButtonListener implements ActionListener
    {
        public void actionPerformed(ActionEvent e){
            //JOptionPane.showMessageDialog(null, "snuc = " + valuegirSon.getLatEntered());
            double sonAci;
            //sonAci = Math.toDegrees(radiosSon.GetResult());
            sonAci = radiosSon.GetResult();
            JOptionPane.showMessageDialog(null, "soonuc = " + sonAci);

        }
    }
    private class ExitButtonListener implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
            System.exit(0);
        }
    }


}

4th: Name of the file is : Converter.java

package converter;

/**
 *
 * @author esesili
 */
public class Converter {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        new ConvertGUI();
    }

}

Thanks a lot,

You managed to make the try catch block. Now all you have to do is System.out.println(variable nexocentric told you to output). Debugging is a very important part of programming, so you need to learn to do it. Debugging tools are very helpful, but I personally prefer print statements. If you put them in appropriate places, such as when you read a value into a variable, after a calculation is done on a variable, etc, then you will find your problem. Basically find the key points, logically determine what the value of the variables should be at those points, and use the print statements to see if they actually are.

commented: Yeah, debugging is pretty important. Nice catch on the listener problem. +1

You managed to make the try catch block. Now all you have to do is System.out.println(variable nexocentric told you to output). Debugging is a very important part of programming, so you need to learn to do it. Debugging tools are very helpful, but I personally prefer print statements. If you put them in appropriate places, such as when you read a value into a variable, after a calculation is done on a variable, etc, then you will find your problem. Basically find the key points, logically determine what the value of the variables should be at those points, and use the print statements to see if they actually are.

Hi BestJewSinceJC,
I put three System.out.println to see what is gong on. The result is

input = 
latitude = 0.0

It does not matter what I enter for textfield I alwys get same result. My code is below. Do you have any idea ?
thanks for helps,

package converter;

import javax.swing.*;
import java.awt.*;
public class ValuePanel extends JPanel {
    private JLabel valLabel;
    private JTextField valTextField;
    public ValuePanel()
    {
        setLayout(new GridLayout(1,2));
        valLabel = new JLabel("Enter Latitude");
        valTextField = new JTextField(10);
        //valTextField.setText("0");
        setBorder(BorderFactory.createTitledBorder("Latitude"));
        add(valLabel);
        add(valTextField);
    }

        public double getLatEntered()
        {
            //valTextField.setText("0");
            String input;
            double latitude;
            input = valTextField.getText();
            System.out.println("input = " + input);
            try{
                latitude = Double.parseDouble(input);
                System.out.println("latitude = " + latitude);
            }
            catch (Exception E){
                latitude = 0.0;
            }
            System.out.println("latitude = " + latitude);
            return latitude;

        }
}

http://java.sun.com/docs/books/tutorial/uiswing/components/textfield.html

^ Read that. You have no mechanism to let the user enter a value into the text field, then to get it. Your getLatEntered method looks like it gets what is in the text field properly, but you have no way to wait for the user to enter text in it first. You can't do the second step without the first -- you need to implement the ActionListener class, and add an action listener to your text field. The examples you will find in the link I gave you show you how to do both of these things and more.

The code for the button and its actionlistener are in the earlier post that shows all the code. I assume the later listing is just a subset.
I must admit, I've spent quite a few minutes on this and can't see the fault...
[edit]
but a few minutes later...
in getResult you have velugir = new ValuePanel(); and you then access the text field in this new, undisplayed, value panel. Shouldn't this access the one you created and displayed in createGUI?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.