Jump to content

Java Swing Problem

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
2 replies to this topic

#1
Funk

Funk

    Newbie

  • Members
  • Pip
  • 2 posts
Hi,

I'm pretty new on programming overall, especially Java Swing, but i want to make a basic currency converter that calculates how many dollars, euros and pounds you get for an ammount of Swedish crones.


import javax.swing.*;

public class Currency{

public static void main (String[] arg) {


int v, dollar, euro, pound;

String indata;


indata = JOptionPane.showInputDialog(null, 

"How many Swedish crones would you like to exchange: ", "Currency-converter",JOptionPane.QUESTION_MESSAGE);


v = Double.parseDouble(indata);



dollar = (v * 7,1);

euro = (v * 10,2);

pound = (v * 11,3);



JOptionPane.showMessageDialog(null, v + " Your ammount is equal to:" + "/nDollar: " + dollar + "/nEuro: " + euro + "/nPound: " + pound, 

"Currency-converter", JOptionPane.PLAIN_MESSAGE );




System.exit(0);

	}

}


Whenever i try to compile the program it says "; missing" on the dollar/euro/punds = (v * XX) lines", and i fail to see where the problem is. I might be way off with the entire code, but any help appriciated!

Thanks.

#2
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
Your biggest problem is:
int v, dollar, euro, pound;
which needs to be double and not int.

Another little thing is that you used /n for newline instead of \n :)

If the user enters nothing, or clicks cancel, or closes the optionpane. Your program will crash.
Using
 if(indata!=null && indata.length()>0){
around the whole part after the JOptionpane (not System.exit(0)) will solve that.

If the user inputs text, thus no double it crashes aswell as it will try to parse it to a double.
This is solved using a try-catch.

Somewhat unhandy is that the parse method does not work with real commas. You must use a point to specify a decimal.
This is fixed using String.replace(',' , '.').

Finally the last thing that doesn't look nice is, when you enter 15.2 for example the result for dollar shows: 107.91999999999999
To only show 2 decimals:
DecimalFormat dfm = new DecimalFormat(",##");
dollar = Double.valueOf(dfm.format(dollar));

Total code:
import javax.swing.*;
import java.text.DecimalFormat;

public class Currency {
    public static void main(String[] arg) {

        double v, dollar, euro, pound;
        String indata;

        indata = JOptionPane.showInputDialog(null, "How many Swedish crones would you like to exchange: ", "Currency-converter", JOptionPane.QUESTION_MESSAGE);

        if (indata != null && indata.length() > 0) {
            try {
                indata = indata.replace(',', '.');
                v = Double.parseDouble(indata);

                dollar = (v * 7.1);
                euro = (v * 10.2);
                pound = (v * 11.3);

                DecimalFormat dfm  = new DecimalFormat(",##");
                dollar = Double.valueOf(dfm.format(dollar));
                euro = Double.valueOf(dfm.format(euro));
                pound = Double.valueOf(dfm.format(pound));

                JOptionPane.showMessageDialog(null, v + " Your ammount is equal to:" + "\nDollar: " + dollar + "\nEuro: " + euro + "\nPound: " + pound,
                        "Currency-converter", JOptionPane.PLAIN_MESSAGE);
            } catch (NumberFormatException e) {
                JOptionPane.showMessageDialog(null, "Please enter a valid number next time", "Fail", JOptionPane.ERROR_MESSAGE);
            }

        }
        System.exit(0);
    }
}


#3
Funk

Funk

    Newbie

  • Members
  • Pip
  • 2 posts
Thanks!