1

I'm trying to get database from the internet but the bug has disapear.

package space;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.util.Scanner;
import java.net.URL;
public class ArrayIntro {
public static void main(String[] args) throws Exception {
        String urlbase = "https://learnjavathehardway.org/txt/";
        double[] temps = arrayFromUrl(urlbase + "avg-daily-temps-atx.txt");

        System.out.println( temps.length + " temperatures in database.");

        double lowest = 9999.99;

        for ( int i=0; i<temps.length; i++ ) {
            if ( temps[i] < lowest ) {
                lowest = temps[i];
            }
        }

        System.out.print( "The lowest average daily temperature was " );
        System.out.println( lowest + "F (" + fToC(lowest) + "C)" );
    }

    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;
    }

    public static double fToC( double f ) {
        return (f-32)*5/9;
    }
}

The value count is 6717. I'm trying to find the lowest temperature in the link https://learnjavathehardway.org/txt/avg-daily-temps-atx.txt.

And the error is this:

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 space.ArrayIntro.arrayFromUrl(ArrayIntro.java:43)
    at space.ArrayIntro.main(ArrayIntro.java:20)
C:\Users\Administrator\AppData\Local\NetBeans\Cache\8.2\executor-snippets\run.xml:53: Java returned: 1
BUILD FAILED (total time: 3 seconds)
Tom
  • 16,842
  • 17
  • 45
  • 54
Space
  • 13
  • 3

1 Answers1

0

In Scanner you can use it with Locale to specify Locale like dot . as decimal separator:

Scanner fin = new Scanner((new URL(url)).openStream()).useLocale(Locale.ENGLISH); 
Abdelhak
  • 8,299
  • 4
  • 22
  • 36