-1

I have an array of Strings where one index specifically the 7th one, needs to be a double number so I could use a conditional on it.

while (s.hasNextLine()) {
                    String customer = s.nextLine();
                    String[] fields = customer.split("\\|");

double score = Double.parseDouble(fields[7]);
}

Also gave this a try, but no luck. Still the same error.

while (s.hasNextLine()) {
                String customer = s.nextLine();
                String[] fields = customer.split("\\|");
                if (Double.parseDouble(fields[7]) > 5.00) {

The fields[7] contains all the numbers in my input file but how do I make them double values in order to use a conditional on them in an if statement. All the other indexes must stay at strings however.

Doing this causes this error:

Exception in thread "main" java.lang.NumberFormatException: For input string: "$280.80"
    at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:2043)
    at sun.misc.FloatingDecimal.parseDouble(FloatingDecimal.java:110)
    at java.lang.Double.parseDouble(Double.java:538)
    at CollectionLetter.main(CollectionLetter.java:13)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)

SOLUTION: THE ANSWER WAS THAT I HAD CURRENCY SYMBOLS AND I WAS TRYING TO PARSE IT STILL, I SIMPLY USED:

            if (Double.parseDouble(fields[7].replace("$","")) > 5.00) {

THANK YOU EVERYONE!

Lubeular
  • 25
  • 1
  • 1
  • 4

2 Answers2

0

If I am understanding your question correctly, you must be trying to check for a double value from a string.

In this case, Double.parseDouble is needed.

if(Double.parseDouble(fields[7]) ... )
Trivaxy
  • 41
  • 1
  • 5
0

Please handle parsing currencies correctly. Although your string is "$123456.78", in other parts of the world "₹3,00,00,000" or "R$ 399,00" are valid amounts.

Use NumberFormat to avoid things blowing up when your friend in Japan runs your code.

If you don't think all that is really needed, here is Boris' shortcut: https://stackoverflow.com/a/23991368/3430807

Community
  • 1
  • 1
Andreas
  • 4,937
  • 2
  • 25
  • 35
  • Yeah I am truly a fool for not thinking about the currency marks were the reasoning behind the error in parsing it as a double. You are answer is more correct than the only other one, so I give it to you. – Lubeular Mar 27 '17 at 18:41