2017-09-04 4 views
1

Ich versuche, Datenbank aus dem Internet zu bekommen, aber der Fehler ist verschwunden.Fehler mit Scanner

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

Der Wert Zählung 6717. Ich versuche, die niedrigste Temperatur in der Verbindung https://learnjavathehardway.org/txt/avg-daily-temps-atx.txt zu finden.

Und der Fehler ist dies:

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) 
+2

Und '.' eine gültige Dezimaltrennzeichen in deiner Umgebung? – Tom

+0

was meinst du? – Space

+0

Wie schreiben Sie "die eine Hälfte" in Ihrem Land? "0,5" oder "0,5"? Wenn es "0,5" ist, kann man natürlich keine Zahlen wie "0,5" lesen, da das Format falsch ist. – Tom

Antwort

0

In Scanner können Sie es mit Locale verwenden Locale wie dot. als Dezimalzeichen angeben:

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