1

This is my first question and it may sound stupid, but I can't find a solution. I'm learning Java, and I was doing an exercise that reads a txt from the internet with temperatures in double format in it (i.e 86.60).

The problem is, after correctly compiling it, when I tried to run it I got the following exception:

Exception in thread "main" java.util.InputMismatchException

This is the method in question:

public static double[] arrayFromUrl(String url) throws Exception {
        Scanner fin = new Scanner((new URL(url)).openStream());
        int count = fin.nextInt();

        double[] dubs = new double[count];

        for ( int i=0; i<dubs.length; i++)
            dubs[i] = fin.nextDouble();
        fin.close();

        return dubs;
    }

And this is the target file: https://learnjavathehardway.org/txt/avg-daily-temps-atx.txt

I finally figured out it was because of the format of the entries, my powershell doesn't accept doubles in xx.xx (dot) format, but only in xx,xx (comma). I think this is because I live in Italy and my system and region settings are according (we use commas for doubles). How can I change the locale of my JVM to make it accept xx.xx format doubles?

RingK
  • 83
  • 12
  • 1
    Posting entire exceptions here is not use full. Post context of it if you expect help from others. – SatyaTNV Jan 30 '16 at 10:55
  • Sorry, as I said, first question :) I thought it wasn't relveant to my question, but still I edited the post adding the method, and the link to the file it reads. – RingK Jan 30 '16 at 11:05
  • How is the data formatted? You are usually better off using a `BufferedReader` and parsing the lines yourself if using a line-by-line format. Further, please remember to use `try-with-resources` with **any** external resources. – Boris the Spider Jan 30 '16 at 11:13
  • To answer your actual question (maybe rewrite your question to make this clearer) you need to either 1) change the [locale of the JVM](https://stackoverflow.com/questions/8809098/how-do-i-set-the-default-locale-for-my-jvm) or 2) use a `BufferedReader` with a `NumberFormat` from the correct locale or 3) set the [locale of the `Scanner`](http://www.java2s.com/Tutorials/Java/java.util/Scanner/Java_Scanner_useLocale_Locale_locale_.htm) itself. Any locale that uses a dot decimal separator would do, for example `Locale.UK`. – Boris the Spider Jan 30 '16 at 11:15
  • Temperatures are in double format (86.6, 45.4 etc), I temporarily solved the problem by modifying the code and have it read a local file where i changed all the dots to commas, but what I'd like to know is if there's a way to make powershell accepting doubles in xx.xx format rather than xx,xx (what we use in italy). – RingK Jan 30 '16 at 11:18
  • Thanks Boris, you solved my problem! – RingK Jan 30 '16 at 11:19
  • @RingK sorry? I have explained how to achieve that in my comment. What I would like you to do is **edit** your **question** to reword it that it is is clear what the primary question is. Posting a comment with the same content as the appendix to your question is unhelpful. – Boris the Spider Jan 30 '16 at 11:20

2 Answers2

2

To make this work, you have to tell the Scanner object to ignore the user's locale when deciding how to parse numbers.

public static double[] arrayFromUrl(String url) throws Exception {
    Scanner fin = new Scanner((new URL(url)).openStream());
    fin.useLocale(Locale.US);
    int count = fin.nextInt();

    double[] dubs = new double[count];

    for ( int i=0; i<dubs.length; i++)
        dubs[i] = fin.nextDouble();
    fin.close();

    return dubs;
}

It is also possible to change the system locale for the whole JVM by adding the following flags to the java command line:

java -Duser.language=en -Duser.region=US MyClass
Andrew Young
  • 311
  • 2
  • 5
0

Set the locale on the Scanner object with the useLocale() method before you read. E.g.

fin.useLocale(Locale.US);

Robust code and data should not rely on the system default locale for exactly the reasons you're encountering.