2

Doing my first steps with Java, here's my problem:

1.) the rawData.txt can be read:

import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;
import static java.lang.System.out;

class ReadAndWrite {

    public static void main(String args[]) 
                         throws FileNotFoundException {

        Scanner diskScanner = new Scanner(new File("rawData.txt")); 
        String diskString;
        diskString = diskScanner.nextLine();
        out.println(diskString);
        diskScanner.close();
    }
}

The result in eclipse console is :

>19.5 5 

So I guess the content can be read.

But: nextDouble() and nextInt() won't work:

import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;

class ReadAndWrite {

    public static void main(String args[]) 
                         throws FileNotFoundException {

        Scanner diskScanner = new Scanner(new File("rawData.txt"));
        double unitPrice, quantity;
        unitPrice = diskScanner.nextDouble();
        quantity = diskScanner.nextInt();

        [...]

        diskScanner.close();

    }
}

The error message from console is:

Exception in thread "main" java.util.InputMismatchException
    at java.util.Scanner.throwFor(Scanner.java:864)
    at java.util.Scanner.next(Scanner.java:1485)
    at java.util.Scanner.nextDouble(Scanner.java:2413)
    at ReadAndWrite.main(ReadAndWrite.java:16)

So what can I do to understand the problem?

xenteros
  • 15,586
  • 12
  • 56
  • 91
maen
  • 187
  • 1
  • 10
  • when I provide "19.5 5" to the scanner using the System.in the program runs without an issue. So can you add the content of the file to understand the issue better. There might be some character that screw up the program. – dammina Nov 10 '16 at 07:46
  • what input does rawData.txt has? – Aniruddha K.M Nov 10 '16 at 07:53
  • 1
    Show us the file - otherwise it's impossible to answer. [mcve]. It looks like the issue is somewhere in `[...]` – xenteros Nov 10 '16 at 08:07
  • @xenteros OP says what the file contains, just make a text file with `19.5 5` inside and you will insta-get the described error – joc Nov 10 '16 at 09:23
  • 1
    @joc I've found the issue – xenteros Nov 10 '16 at 09:31

2 Answers2

3

I have just made a test on my machine and I've found the problem. Expected double format depends on Locale. In some countries the decimal digits are separated with , and some with ..

    Scanner diskScanner = new Scanner(new File("rawData.txt")).useLocale(Locale.ENGLISH);

will solve the problem. Otherwise change your file from

19.5 5

to

19,5 5
xenteros
  • 15,586
  • 12
  • 56
  • 91
  • nice catch! `:=)` – joc Nov 10 '16 at 09:31
  • @joc I was sure that the default is the `.` but it looks like the default depends on your location? I don't know. – xenteros Nov 10 '16 at 09:32
  • There is a nice paragraph saying it use the default local in the doc : https://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html but this is some common mistake. This is a common problem in Europe... – AxelH Nov 10 '16 at 09:40
  • Yes, that was my problem and your solution. Thanks a lot! – maen Nov 10 '16 at 09:54
  • @maen This answer make more sense to me. If the format in the file change later, it will be easier to change the local – AxelH Nov 10 '16 at 10:01
  • @maen if it was the solution, please mark it as solution by clicking on a gray tick below the answer score. An upvote will be welcome as well. Why did you mark another answer as an answer? The accepted answer doesn't try to solve your problem... – xenteros Nov 10 '16 at 10:05
  • 1
    @xenteros Sorry for my misbehaving! Change it now. You're right! – maen Nov 10 '16 at 12:36
0

From javadoc regarding the exception :

Thrown by a Scanner to indicate that the token retrieved does not match the pattern for the expected type, or that the token is out of range for the expected type.

If you are pretty sure about the data type to be read, you can use the specific methods. otherwise go for the .next() method. that returns the String

And if you want to do calculations over the values, you can parse that strings using the parsers like Double.valueOf("5.2") or Integer.valueOf("5") and handling the parse exceptions in try catch blocks.

Umais Gillani
  • 608
  • 4
  • 9
  • But how will you use a String for calculation after that ? – AxelH Nov 10 '16 at 09:46
  • You can use the parsers as `Double.valueOf("5.2")` or `Integer.valueOf("5")` and then add the numbers. handle possible exceptions in try catch blocks as well. if you want to read with `.nextDouble();` you have to make sure that your file is well managed. – Umais Gillani Nov 10 '16 at 09:48
  • This should be in your answer, this was what I mean ;) – AxelH Nov 10 '16 at 09:49
  • Thanks, good to hear that i was helpful. let me edit my answer and adding this as well. – Umais Gillani Nov 10 '16 at 09:51