2

My program only accepts dots when accepting decimals; I'd like it to also accept commas.

I don't want to replace the dot with a comma; instead, I want to make it accept both dots and commas. I need to apply it to the String "amount" which is converted to a double afterwards! How can I do that?

    public static void main(String[] args){
    
        double euro, usd, gpb, dkk, done;
        
        Scanner input = new Scanner(System.in);
    
        String temp = JOptionPane.showInputDialog("Convert from " +"USD, GBP, DKK or EURO?");
        
        String tempp = JOptionPane.showInputDialog("To " +"USD, GBP, DKK or EURO?");
    
        Map<String, Double> lookUpMap = new HashMap<String, Double>(){{
            put("EURO", new Double(7.46));
            put("USD", new Double(5.56));
            put("GBP", new Double(8.84));
            put("DKK", new Double (1.0));
            }};
        
        
        String amount = JOptionPane.showInputDialog("amount of " +(temp));
        double amountt = Double.parseDouble(amount);
        done = (lookUpMap.get(temp.toUpperCase()) / lookUpMap.get(tempp.toUpperCase())) * amountt;
        
        JOptionPane.showMessageDialog(null, "It is " +done + " " +(temp),"Final amount of " + (temp), JOptionPane.PLAIN_MESSAGE);
        
        String exit = JOptionPane.showInputDialog("Do u want to rerun program? YES or NO");
        if(exit.equalsIgnoreCase("YES")){
            RerunGUI.main(args);
        }else{
            System.out.println("");
        }

    }

}

Jeff Schaller
  • 2,352
  • 5
  • 23
  • 38
Oliver Bak
  • 151
  • 12
  • 1
    What's the difference between a dot and a decimal? Please explain more clearly what you need to accomplish – Paul Samsotha Dec 02 '13 at 17:03
  • @DoubleDouble I actually understood it to mean the way that Ijgw (or is it ljgw?) understood it, where commas are the same as the decimal dot. – Justin Dec 02 '13 at 17:11
  • @Quincunx yes, that makes more sense. Removed comment because it is unhelpful now. – DoubleDouble Dec 02 '13 at 17:12
  • 1
    As an aside, you should never use floats or doubles to represent money. http://stackoverflow.com/questions/3730019/why-not-use-double-or-float-to-represent-currency – dnault Dec 02 '13 at 21:21

3 Answers3

4

If you want to parse a dollar value that has commas and decimals you can use NumberFormat And use a locale that uses that number format.

     String value1 = "1,222.34";
     String value2 = "2,334.45";

     double d1 = 0;
     double d2 = 0;
     try {
        NumberFormat usFormat = NumberFormat.getNumberInstance(Locale.US);
        d1 = usFormat.parse(value1).doubleValue();
        d2 = usFormat.parse(value2).doubleValue();
     } catch(ParseException ex) {
         ex.printStackTrace();
     }

     System.out.println(d1 + d2);  // Output 3556.79

See Here for list of different locales.

Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
1

I think that you might want to try using String.replaceAll() command on value that you receive, then cast the value into double.

double amountt = Double.parseDouble(amount.replaceAll(",","."));
Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
Dimitri
  • 453
  • 3
  • 11
0

This is a bit ugly, but it seems to be what you want:

double amountt = Double.parseDouble(amount.replaceAll(",",".");

this way, everything that is entered (be-it a decimal with a comma or with a point) is parsed.

ljgw
  • 2,751
  • 1
  • 20
  • 39