简体   繁体   中英

SetText method on JTextField from return value of a method works on one method but not on another identical method

I think the title pretty much can speak for itself.

In my ActionPerformed method, on the two first statements of the conditionals:

        if(!checkIfNokTextFieldIsEmpty() && checkIfOtherTextFieldIsEmpty()) {
            otherTextField.setText(calculateFromNok(txt, "GBP"));
            otherLabel.setText("GBP");
        }

        else if(checkIfNokTextFieldIsEmpty() && !checkIfOtherTextFieldIsEmpty()) {
            nokTextField.setText(calculateToNok(txt, "GBP"));
        }

... only the top one (if) works eventhough i have set calculateFromNok and calculateToNok to be exactly identical. The top one sets the return value of the calculateFromNok method as setText in the otherTextField, and i want to do the same but opposite when only otherTextField is set. But as i said, eventhough both methods are identical only the setText of otherTextField works. I have set them to be identical to check if it works then, and just to have said it the currency mathemathics i will deal with later.

Here is my entire code:

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

public class ValutaKalkulator implements ActionListener
{
private JFrame frame;
private JOptionPane optionPane;
private JButton buttonRemoveNok;
private JButton removeOther;
private JButton removeBoth;
private JButton exitButton;
private JButton usdButton;
private JButton sekButton;
private JButton gbpButton;
private JButton eurButton;
private JLabel nokLabel;
private JLabel otherLabel;
private JTextField otherTextField;
private JTextField nokTextField;

public ValutaKalkulator() 
{
    frame = new JFrame();
    frame.setResizable(false);
    optionPane = new JOptionPane();
    frame.setTitle("VALUTAKALKULATOR");
    buttonRemoveNok = new JButton("Fjern NOK");
    removeOther = new JButton("Fjern annen valuta");
    removeBoth = new JButton("Fjern begge");
    exitButton = new JButton("Avslutt");
    usdButton = new JButton("USD");
    sekButton = new JButton("SEK");
    gbpButton = new JButton("GBP");
    eurButton = new JButton("EUR");
    nokLabel = new JLabel("NOK");
    otherLabel = new JLabel("Annen valuta");
    otherTextField = new JTextField();
    nokTextField = new JTextField();

    ImageIcon img = new ImageIcon("dlr.png");
    frame.setIconImage(img.getImage());

    buttonRemoveNok.addActionListener(this);
    removeOther.addActionListener(this);
    removeBoth.addActionListener(this);
    exitButton.addActionListener(this);
    usdButton.addActionListener(this);
    sekButton.addActionListener(this);
    gbpButton.addActionListener(this);
    eurButton.addActionListener(this);

    JPanel pnlSouth = new JPanel(new GridLayout(1, 4));
    JPanel pnlCenter = new JPanel(new GridLayout(2, 2));
    JPanel pnlNorth = new JPanel(new GridLayout(1, 4));

    pnlNorth.add(nokLabel);
    pnlNorth.add(nokTextField);
    pnlNorth.add(otherLabel);
    pnlNorth.add(otherTextField);

    pnlCenter.add(gbpButton);
    pnlCenter.add(eurButton);
    pnlCenter.add(usdButton);
    pnlCenter.add(sekButton);

    pnlSouth.add(buttonRemoveNok);
    pnlSouth.add(removeOther);
    pnlSouth.add(removeBoth);
    pnlSouth.add(exitButton);

    frame.add(pnlNorth, BorderLayout.NORTH);
    frame.add(pnlSouth, BorderLayout.SOUTH);
    frame.add(pnlCenter, BorderLayout.CENTER);

    frame.setVisible(true);
    frame.pack();

}

public String calculateFromNok(String nokValueString, String toValue)
{
    double result = 0;
    double nokValue = Double.valueOf(nokValueString);

    switch(toValue)
    {
        case "GBP":
        result = nokValue * 0.109690;
        break;

        case "EUR":
        result = nokValue * 0.093158;
        break;

        case "USD":
        result = nokValue * 0.085537;
        break;

        case "SEK":
        result = nokValue * 97.03 / 100;
        break;
    }

    String resultString = Double.toString(result);
    return resultString;
}


public String calculateToNok(String nokValueString, String toValue)
{
    double result = 0;
    double nokValue = Double.valueOf(nokValueString);

    switch(toValue)
    {
        case "GBP":
        result = nokValue * 0.109690;
        break;

        case "EUR":
        result = nokValue * 0.093158;
        break;

        case "USD":
        result = nokValue * 0.085537;
        break;

        case "SEK":
        result = nokValue * 97.03 / 100;
        break;
    }

    String resultString = Double.toString(result);
    return resultString;
}

public void actionPerformed(ActionEvent event)
{
    String text = event.getActionCommand();
    String txt = nokTextField.getText();

    switch(text)
    {
        case "Fjern NOK":
        nokTextField.setText("");
        break;

        case "Fjern annen valuta":
        otherTextField.setText("");
        otherLabel.setText("Annen valuta");
        break;

        case "Fjern begge":
        nokTextField.setText("");
        otherTextField.setText("");
        otherLabel.setText("Annen valuta");
        break;

        case "Avslutt":
        System.exit(0);
        break;

        case "GBP":
        if(!checkIfNokTextFieldIsEmpty() && checkIfOtherTextFieldIsEmpty()) {
            otherTextField.setText(calculateFromNok(txt, "GBP"));
            otherLabel.setText("GBP");
        }

        else if(checkIfNokTextFieldIsEmpty() && !checkIfOtherTextFieldIsEmpty()) {
            nokTextField.setText(calculateToNok(txt, "GBP"));
        }

        else if(!checkIfNokTextFieldIsEmpty() && !checkIfOtherTextFieldIsEmpty()) {
            optionPane.showMessageDialog(frame, "Du må skrive bare ett beløp!");
        }

        else if(checkIfNokTextFieldIsEmpty() && checkIfOtherTextFieldIsEmpty()) {
            optionPane.showMessageDialog(frame, "Du må skrive ett beløp!");
        }
        break;

        case "EUR":
        if(!checkIfNokTextFieldIsEmpty() && checkIfOtherTextFieldIsEmpty()) {
            otherTextField.setText(calculateFromNok(txt, "EUR"));
            otherLabel.setText("EUR");
        }

        else if(checkIfNokTextFieldIsEmpty() && !checkIfOtherTextFieldIsEmpty()) {
            nokTextField.setText(calculateToNok(txt, "EUR"));

        }

        else if(!checkIfNokTextFieldIsEmpty() && !checkIfOtherTextFieldIsEmpty()) {
            optionPane.showMessageDialog(frame, "Du må skrive bare ett beløp!");
        }

        else if(checkIfNokTextFieldIsEmpty() && checkIfOtherTextFieldIsEmpty()) {
            optionPane.showMessageDialog(frame, "Du må skrive ett beløp!");
        }
        break;

        case "USD":
        if(!checkIfNokTextFieldIsEmpty() && checkIfOtherTextFieldIsEmpty()) {
            otherTextField.setText(calculateFromNok(txt, "USD"));
            otherLabel.setText("USD");
        }

        else if(checkIfNokTextFieldIsEmpty() && !checkIfOtherTextFieldIsEmpty()) {
            nokTextField.setText(calculateToNok(txt, "USD"));
        }

        else if(!checkIfNokTextFieldIsEmpty() && !checkIfOtherTextFieldIsEmpty()) {
            optionPane.showMessageDialog(frame, "Du må skrive bare ett beløp!");
        }

        else if(checkIfNokTextFieldIsEmpty() && checkIfOtherTextFieldIsEmpty()) {
            optionPane.showMessageDialog(frame, "Du må skrive ett beløp!");
        }
        break;

        case "SEK":
        if(!checkIfNokTextFieldIsEmpty() && checkIfOtherTextFieldIsEmpty()) {
            otherTextField.setText(calculateFromNok(txt, "SEK"));
            otherLabel.setText("SEK");
        }

        else if(checkIfNokTextFieldIsEmpty() && !checkIfOtherTextFieldIsEmpty()) {
            nokTextField.setText(calculateToNok(txt, "SEK"));
        }

        else if(!checkIfNokTextFieldIsEmpty() && !checkIfOtherTextFieldIsEmpty()) {
            optionPane.showMessageDialog(frame, "Du må skrive bare ett beløp!");
        }

        else if(checkIfNokTextFieldIsEmpty() && checkIfOtherTextFieldIsEmpty()) {
            optionPane.showMessageDialog(frame, "Du må skrive ett beløp!");
        }
        break;
    }

}

public boolean checkIfNokTextFieldIsEmpty()
{
    if(nokTextField.getText().isEmpty())
    {
        return true;
    }
    return false;
}

public boolean checkIfOtherTextFieldIsEmpty()
{
    if(otherTextField.getText().isEmpty()) {
        return true;
    }
    return false;
}

}

The error message i get is:

Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: 
empty String

And that should mean that its trying to format an empty string into a Double i guess? But still, the formatting from double to string works on the other method without errors...

Maybe that was abit poorly explained, but i hope you understand what i mean. Thanks in advance!

You have made a small mistake in this part:

case "GBP":
        if(!checkIfNokTextFieldIsEmpty() && checkIfOtherTextFieldIsEmpty()) {
            otherTextField.setText(calculateFromNok(txt, "GBP"));
            otherLabel.setText("GBP");
        }

        else if(checkIfNokTextFieldIsEmpty() && !checkIfOtherTextFieldIsEmpty()) {
            nokTextField.setText(calculateToNok(txt, "GBP"));
        }

Here in the else if condition Suppose if nokTextField is empty then in String txt = nokTextField.getText(); , here txt stores an empty String. So in execution of the else if condition First Function checkIfNokTextFieldIsEmpty() will return true and consider that another function returns true as well. So this condition gets satisfied and then calculateToNok(txt, "GBP") function will be called.

Now in that function you have a statement double nokValue = Double.valueOf(nokValueString); which will try to convert that empty string to double and causing the Exception .

So I think you have made in that condition of else if part. Condition should be :

else if(!checkIfNokTextFieldIsEmpty() && !checkIfOtherTextFieldIsEmpty())

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM