I have trouble when parsing with class NumberFormat, my data:
Cocacola 10 50
Tea 10 50
Water 10 50
Milk 10 50
Soda 10 50
I checked readLine() and it read correctly, but when I parse to double or integer value and print, it was wrong, this is stdout:
Cocacola 1.0 5
Tea 1.0 5
Water 1.0 5
Milk 2.0 5
Soda 1.0 5
And code:
String tmp[] = line.split("\\s+");
String name = tmp[0];
double cost=0;
int number=0;
NumberFormat nf = NumberFormat.getInstance();
try {
cost = nf.parse(tmp[1].trim()).doubleValue();
number=nf.parse(tmp[2].trim()).intValue();
} catch (ParseException e) {
e.printStackTrace();
}
System.out.println(name+" "+cost+" "+number+" ");
I cant use normal parse (Double.parse(), Integer.parse(), etc) because them make NumberFormatException error.
System.out.println(Locale.getDefault());