扫描仪出错

时间:2017-09-04 12:21:22

标签: java

我试图从互联网上获取数据库,但这个错误已经消失了。

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

值计数为6717.我试图找到链接https://learnjavathehardway.org/txt/avg-daily-temps-atx.txt中的最低温度。

错误是这样的:

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)

1 个答案:

答案 0 :(得分:0)

Scanner中,您可以将其与Locale一起使用,将Locale指定为dot .作为小数点分隔符:

Scanner fin = new Scanner((new URL(url)).openStream()).useLocale(Locale.ENGLISH);