-3

I am having a hard time finding the source of the problem in my currency converter.

import javax.swing.*;

import java.awt.*;
import java.awt.event.*;

public class Adam_Markros_Valutaomvandlare extends JFrame implements ActionListener {
    JPanel left, middle, right;
    JFrame ram;
    JButton buttonOK;
    JTextField fieldfrom, fieldto, extrafield;
    JLabel labelfrom, labelto, labeldollar, labeleuro, labelpund, labelkrona, extralabel, nothing;
    JRadioButton buttonDollar, buttonEuro, buttonPund, buttonKrona;
    JRadioButton buttonDollar2, buttonEuro2, buttonPund2, buttonKrona2;
    ButtonGroup GroupFrom = new ButtonGroup();
    ButtonGroup GroupTo = new ButtonGroup();

    int currencyFrom;
    int currencyTo;
    int taljare = 0;
    int USD     = 6;
    int Pund = 10;
    int Euro = 9;
    int other;
    int namnare = 0;

    public Adam_Markros_Valutaomvandlare(){
        left = new JPanel();
        middle = new JPanel();
        right = new JPanel();

        setSize(600,250);
        setTitle("Adam Markros - Valutaomvandlare");
        setLayout(new GridLayout(1,3));
        left.setLayout(new GridLayout(6,1));
        middle.setLayout(new GridLayout(6,1));
        right.setLayout(new GridLayout(6,1));

        this.add(left);
        this.add(middle);
        this.add(right);

        buttonOK = new JButton("OK");
        fieldfrom = new JTextField();
        fieldto = new JTextField();
        extrafield = new JTextField();
        labelfrom = new JLabel("Från:");
        labelto = new JLabel("Till:");
        extralabel = new JLabel("Annan valuta:");
        nothing = new JLabel("");

        buttonDollar = new JRadioButton("USD");
        buttonEuro = new JRadioButton("Euro");
        buttonPund = new JRadioButton("Pund");
        buttonKrona = new JRadioButton("SEK");

        buttonDollar2 = new JRadioButton("USD");
        buttonEuro2 = new JRadioButton("Euro");
        buttonPund2 = new JRadioButton("Pund");
        buttonKrona2 = new JRadioButton("SEK");

        GroupFrom.add(buttonDollar);
        GroupFrom.add(buttonEuro);
        GroupFrom.add(buttonPund);
        GroupFrom.add(buttonKrona);

        GroupTo.add(buttonDollar2);
        GroupTo.add(buttonEuro2);
        GroupTo.add(buttonPund2);
        GroupTo.add(buttonKrona2);

        left.add(labelfrom);
        left.add(buttonDollar);
        left.add(buttonEuro);
        left.add(buttonPund);
        left.add(buttonKrona);
        left.add(fieldfrom);

        middle.add(extralabel);
        middle.add(extrafield);
        middle.add(buttonOK);

        right.add(labelto);
        right.add(buttonDollar2);
        right.add(buttonEuro2);
        right.add(buttonPund2);
        right.add(buttonKrona2);
        right.add(fieldto);

        buttonOK.addActionListener(this);
        fieldfrom.addActionListener(this);
        fieldto.addActionListener(this);
        extrafield.addActionListener(this);

        buttonDollar.addActionListener(this);
        buttonDollar2.addActionListener(this);

        buttonEuro.addActionListener(this);
        buttonEuro2.addActionListener(this);

        buttonPund.addActionListener(this);
        buttonPund2.addActionListener(this);

        buttonKrona.addActionListener(this);
        buttonKrona2.addActionListener(this);

        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        this.setVisible(true);
    }

    public static void main (String[] args){
        new Adam_Markros_Valutaomvandlare();
    }

    public void actionPerformed(ActionEvent e) {
        if((fieldfrom.getText().toString()) != ""){
            currencyFrom = Integer.parseInt(fieldfrom.getText());
        }
        if((fieldto.getText().toString()) != ""){
            currencyTo = Integer.parseInt(fieldto.getText());
        }

        if((extrafield.getText().toString()) != ""){
            taljare = Integer.parseInt(extrafield.getText());
        }

        /*
        if(e.getSource() == buttonDollar){
        currencyFrom = USD;
        }
        if(e.getSource() == buttonDollar2){
        currencyTo = USD;
        }
        if(e.getSource() == buttonPund){
        currencyFrom = Pund;
        }
        if(e.getSource() == buttonPund2){
        currencyTo = Pund;
        }
        if(e.getSource() == buttonEuro){
        currencyFrom = Euro;
        }
        if(e.getSource() == buttonEuro2){
        currencyTo = Euro;
        }
        if(e.getSource() == buttonKrona){
        currencyFrom = 1;
        }
        if(e.getSource() == buttonKrona2){
        currencyTo = 1;
        }
        else{
        taljare = other;
        }
        */

        if(e.getSource() == buttonOK){

            if(buttonPund.isSelected()){
                taljare = 10;
            }
            if(buttonDollar.isSelected()){
                taljare = 6;
            }
            if(buttonEuro.isSelected()){
                taljare = 9;
            }
            if(buttonKrona.isSelected()){
                taljare = 1;
            }

            if(buttonPund2.isSelected()){
                namnare = 10;
            }
            if(buttonDollar2.isSelected()){
                namnare = 6;
            }
            if(buttonEuro2.isSelected()){
                namnare = 9;
            }
            if(buttonKrona2.isSelected()){
                namnare = 1;
            }

            currencyTo = (currencyFrom * (taljare / namnare));

            fieldto.setText(Integer.toString(currencyTo));          
        }
    }
}

Here are the errors:

Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: "" at java.lang.NumberFormatException.forInputString(Unknown Source) at java.lang.Integer.parseInt(Unknown Source) at java.lang.Integer.parseInt(Unknown Source) at Adam_Markros_Valutaomvandlare.actionPerformed(Adam_Markros_Valutaomvandlare.java:116) at javax.swing.AbstractButton.fireActionPerformed(Unknown Source) at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source) at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source) at javax.swing.JToggleButton$ToggleButtonModel.setPressed(Unknown Source) at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source) at java.awt.Component.processMouseEvent(Unknown Source) at javax.swing.JComponent.processMouseEvent(Unknown Source) at java.awt.Component.processEvent(Unknown Source) at java.awt.Container.processEvent(Unknown Source) at java.awt.Component.dispatchEventImpl(Unknown Source) at java.awt.Container.dispatchEventImpl(Unknown Source) at java.awt.Component.dispatchEvent(Unknown Source) at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source) at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source) at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source) at java.awt.Container.dispatchEventImpl(Unknown Source) at java.awt.Window.dispatchEventImpl(Unknown Source) at java.awt.Component.dispatchEvent(Unknown Source) at java.awt.EventQueue.dispatchEventImpl(Unknown Source) at java.awt.EventQueue.access$000(Unknown Source) at java.awt.EventQueue$1.run(Unknown Source) at java.awt.EventQueue$1.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source) at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source) at java.awt.EventQueue$2.run(Unknown Source) at java.awt.EventQueue$2.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source) at java.awt.EventQueue.dispatchEvent(Unknown Source) at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.run(Unknown Source) Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: "" at java.lang.NumberFormatException.forInputString(Unknown Source) at java.lang.Integer.parseInt(Unknown Source) at java.lang.Integer.parseInt(Unknown Source) at Adam_Markros_Valutaomvandlare.actionPerformed(Adam_Markros_Valutaomvandlare.java:119) at javax.swing.AbstractButton.fireActionPerformed(Unknown Source) at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source) at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source) at javax.swing.JToggleButton$ToggleButtonModel.setPressed(Unknown Source) at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source) at java.awt.Component.processMouseEvent(Unknown Source) at javax.swing.JComponent.processMouseEvent(Unknown Source) at java.awt.Component.processEvent(Unknown Source) at java.awt.Container.processEvent(Unknown Source) at java.awt.Component.dispatchEventImpl(Unknown Source) at java.awt.Container.dispatchEventImpl(Unknown Source) at java.awt.Component.dispatchEvent(Unknown Source) at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source) at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source) at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source) at java.awt.Container.dispatchEventImpl(Unknown Source) at java.awt.Window.dispatchEventImpl(Unknown Source) at java.awt.Component.dispatchEvent(Unknown Source) at java.awt.EventQueue.dispatchEventImpl(Unknown Source) at java.awt.EventQueue.access$000(Unknown Source) at java.awt.EventQueue$1.run(Unknown Source) at java.awt.EventQueue$1.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source) at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source) at java.awt.EventQueue$2.run(Unknown Source) at java.awt.EventQueue$2.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source) at java.awt.EventQueue.dispatchEvent(Unknown Source) at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.run(Unknown Source) Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: "" at java.lang.NumberFormatException.forInputString(Unknown Source) at java.lang.Integer.parseInt(Unknown Source) at java.lang.Integer.parseInt(Unknown Source) at Adam_Markros_Valutaomvandlare.actionPerformed(Adam_Markros_Valutaomvandlare.java:119) at javax.swing.AbstractButton.fireActionPerformed(Unknown Source) at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source) at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source) at javax.swing.DefaultButtonModel.setPressed(Unknown Source) at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source) at java.awt.Component.processMouseEvent(Unknown Source) at javax.swing.JComponent.processMouseEvent(Unknown Source) at java.awt.Component.processEvent(Unknown Source) at java.awt.Container.processEvent(Unknown Source) at java.awt.Component.dispatchEventImpl(Unknown Source) at java.awt.Container.dispatchEventImpl(Unknown Source) at java.awt.Component.dispatchEvent(Unknown Source) at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source) at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source) at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source) at java.awt.Container.dispatchEventImpl(Unknown Source) at java.awt.Window.dispatchEventImpl(Unknown Source) at java.awt.Component.dispatchEvent(Unknown Source) at java.awt.EventQueue.dispatchEventImpl(Unknown Source) at java.awt.EventQueue.access$000(Unknown Source) at java.awt.EventQueue$1.run(Unknown Source) at java.awt.EventQueue$1.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source) at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source) at java.awt.EventQueue$2.run(Unknown Source) at java.awt.EventQueue$2.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source) at java.awt.EventQueue.dispatchEvent(Unknown Source) at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.run(Unknown Source)

  • 3
    1. What is the problem? 2. Please remove all unnecessary code. – Keppil May 26 '13 at 13:14
  • It doesn't show any errors on the left side, but when I start the program and press a radio button, a red text appears that says: – user2422314 May 26 '13 at 13:17
  • Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: "" at java.lang.NumberFormatException.forInputString(Unknown Source) at java.lang.Integer.parseInt(Unknown Source) at java.lang.Integer.parseInt(Unknown Source) – user2422314 May 26 '13 at 13:18
  • Please post your error messages and all important details of your problem as an edit to your question. The information is too important to be buried in a comment. – Hovercraft Full Of Eels May 26 '13 at 13:19
  • Learn what the error messages said, they are very informative and you can always find a description/solution of your problem by Googling it. Otherwise, you will ask a lot of questions here... – Alexis C. May 26 '13 at 13:20
  • What values are in `fieldfrom`, `fieldto` and `extrafield` when the action listener is called. You are getting an error parsing a string into an int so what's in those fields will probably be revealing... – cmbaxter May 26 '13 at 13:21
  • fieldfrom = the amount you choose. fieldto = the result. extrafield = an extra currency. – user2422314 May 26 '13 at 13:24
  • What I meant was what values were in the text boxes. Were they empty? Did they contain a value that was not a number? Check Hovercraft Full of Eels answer as he is most likely right in that your check to see if the boxes is empty is not right. – cmbaxter May 26 '13 at 13:25
  • they were empty. The errors appear when I press radio buttons – user2422314 May 26 '13 at 13:31

1 Answers1

2

Don't use == or != for comparing Strings since these check if one object reference is the same as another, something you're not interested in.

Use equals(...) Or in your case isEmpty() since these check to see if a String's contents are the same as another or if a String is empty.

if (!myTextField.getText().isEmpty()) {

}
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373