0

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.

6
  • I hope it be 10.0 or 50 as file data Commented Nov 25, 2016 at 15:09
  • Have you tried to watch in debugger (or printing) values which you get after split? Commented Nov 25, 2016 at 15:19
  • I have printed before and after splited, values were corrected. It just wrong after parse to number. Commented Nov 25, 2016 at 15:28
  • Please, execute the following line and tell me the output so I try to set your same locale: System.out.println(Locale.getDefault()); Commented Nov 25, 2016 at 15:49
  • Output is "en_US" Commented Nov 25, 2016 at 15:51

3 Answers 3

1

What happens if you try with this file in input?

Cocacola 10 50
GreenTea 10 50
Water 10 50
MilkTea 10 50
Soda 10 50

May be it is only the blank space (and split) that generate the errror, i.e.: "Green Tea", "Milk Tea",... The split function in this line "Green Tea 10 50" generate tmp["Green","Tea","10","50"] and in this line of code:

cost = nf.parse(tmp[1].trim()).doubleValue();

You are trying to parse to number the String "Tea".

Sign up to request clarification or add additional context in comments.

3 Comments

It have same as my file, after parase 10 -> 1.0 50 ->5 Correcty if print as String value.
I know that it is a trivial question but, are you sure the program is reading the file you think it reads?
Sorry, I got copy wrong file. Sdtout posted was of this file Cocacola 10 50 Tea 15 50 Water 10 50 Milk 20 50 Soda 15 50
1

Your problem is related to the fact that you could have spaces in the middle of your name so using line.split("\\s+") cannot work as you could get an array of String with a length greater than 3 while your code expects a length of exactly 3.

You should use a regular expression that will define the expected format of your line, something like this:

// Meaning a sequence of any characters followed by a space
// then a double followed by a space and finally an integer 
Pattern pattern = Pattern.compile("^(.*) (\\d+(?:\\.\\d+)?) (\\d+)$");
Matcher matcher = pattern.matcher(line);
if (matcher.find()) {
    String name = matcher.group(1);
    double cost = Double.valueOf(matcher.group(2));
    int number = Integer.valueOf(matcher.group(3));
    System.out.printf("%s %f %d%n", name, cost, number);
} else {
    throw new IllegalStateException(
        String.format("line '%s' doesn't have the expected format", line)
    );
}

Comments

0

I guess you should use 2 different formats, one for the price another for the quantity. You could you try this:

NumberFormat nf = NumberFormat.getNumberInstance(Locale.ENGLISH);
try {
    cost = nf.parse(tmp[1].trim())).doubleValue();
    number = nf.parse(tmp[2].trim()).intValue();
} catch (ParseException e) {
    e.printStackTrace();
}

If you try exactly this code:

NumberFormat numberFormat = NumberFormat.getNumberInstance(Locale.ENGLISH);
double d = numberFormat.parse("10").doubleValue();
int q = numberFormat.parse("50").intValue();
System.out.println("Cost: " + d);
System.out.println("Quantity: " + q);

The output would be:

10.0 50

Is not your expected code?

2 Comments

I have excuted but output still wrong after parsed.
@ĐàmĐìnhĐinh check your input values, because the example i did was with hardcoded values and worked, so probably some of your input value is wrong

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.