0

I'm making a program that reads from a file 3 numbers, doubles or integers, per line except the first one. The first line is the the number of lines after. The 3 numbers in each line are variables a, b, and c in the quadratic formula. Then the program is supposed to return the difference between the + and - part of the formula. Here is an example of what the file may contain.

3
1 2 4
1.5 3.12 4.31
0.09 5 2

My problem is the when using scanner to turn the tokens in the file into doubles so that they can be inputted to the quadratic formula, I am getting the following error:

Exception in thread "main" java.lang.NumberFormatException: For input string: "­1"
    at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1250)
    at java.lang.Double.parseDouble(Double.java:540)
    at gorf.main(gorf.java:23)

here is part of my code:

    int lines = Integer.parseInt(sc.nextLine());
    double a;
    double b;
    double c;

    String line = "";
    Scanner abc = new Scanner(line);
    for(int count = 0; count < lines; count++)
    {
        line = sc.nextLine();
        abc = new Scanner(line);

        a = Double.parseDouble(abc.next());
        b = Double.parseDouble(abc.next());
        c = Double.parseDouble(abc.next());
    }
Pshemo
  • 122,468
  • 25
  • 185
  • 269
  • Is it possible that your file contains some special characters at start like BOM (byte order mark)? Try maybe encoding like UTF-8 without BOM. – Pshemo Dec 06 '15 at 00:55
  • Or nevermind. I misread error (thought it was for parsing `"3"` which was at start of your input). – Pshemo Dec 06 '15 at 00:57
  • 1
    Parsing `"1"` as a double should not be a problem. I think Pshemo's comment is still somewhat relevant in that there must be some non-visible character in there that is creating problems. Consider assigning the result of `Scanner.next()` to a variable before parsing, and then examine each of the characters individually to figure out which odd character sneaked in there. – sstan Dec 06 '15 at 01:01
  • Tried assigning it to a variable and it only printed `"1"` here is the code I changed. `str = abc.next(); System.out.println(str); a = Double.parseDouble(str);` Sorry for the formatting i can't get it to work for some reason. @sstan – Dane Williams Dec 06 '15 at 02:06
  • Printing the string if it has non-visible characters won't help. What you need to do is check the length of the string with `strVar.length()`, which I predict will return more than `1`. Then you can examine each of the characters individually, something like: `(int)strVar.charAt(n)`. – sstan Dec 06 '15 at 02:18
  • That was right it says it's length is 2. How am I supposed to figure out what character it is and how to handle it if I can't see it? This is my first year taking AP computer science at my high school so I'm still pretty new to this. @sstan – Dane Williams Dec 06 '15 at 02:28
  • Use the little snippet I gave you: `(int)strVar.charAt(n)`. This will give you the Unicode code point value for the character at the `n` position, even if the character itself is not visible. Just replace `n` with `0` and `1` to read both Unicode code point values. – sstan Dec 06 '15 at 02:32
  • Yes, the unicode values are kind of like ascii, at least for smaller values. In this case, `173` is a soft hyphen, and `49` is the number `1`. The soft hyphen is supposed to be visible, but apparently, this character has been known to cause problems, and is not always rendered correctly. In any case, do you have any idea how that character got in your file? Did you use a particular editor? Might want to make sure you use an editor that doesn't mess with your text. – sstan Dec 06 '15 at 03:00

1 Answers1

0

Your file is not clean. You have hidden non-visible characters in there.

Use the following code to uncover the character that is causing you grief:

int lines = Integer.parseInt(sc.nextLine());
double a;
double b;
double c;

String line = "";
Scanner abc = new Scanner(line);
for (int count = 0; count < lines; count++) {
    line = sc.nextLine();
    abc = new Scanner(line);

    a = Double.parseDouble(readNextAndLog(abc));
    b = Double.parseDouble(readNextAndLog(abc));
    c = Double.parseDouble(readNextAndLog(abc));
}

readNextAndLog method:

private static String readNextAndLog(Scanner sc) {
    String s = sc.next();

    for (int i = 0; i < s.length(); i++) {
        System.out.println((int)s.charAt(i));
    }

    return s;
}

Pay attention to the numbers printed just before the exception occurs. The problematic character's Unicode value will be printed there.

You'll then have to figure out how the unexpected got in your file in the first place and get rid of it, or recreate your file from scratch.

EDIT:

Your problematic character is the SOFT HYPHEN (U+00AD) character.

This character is not supposed to be invisible, but is known to be handled/rendered inconsistently. (Yes, SOFT HYPHEN is a hard problem)

Make sure your file doesn't contain that character. Make sure you use an editor that doesn't mess with your file in some unexpected way. An editor like Notepad++ for example.

sstan
  • 35,425
  • 6
  • 48
  • 66