0
votes

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());
    }
1
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
Or nevermind. I misread error (thought it was for parsing "3" which was at start of your input).Pshemo
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
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. @sstanDane Williams
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

1 Answers

0
votes

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.