How to read double in java from a file

2019-07-16 19:06发布

问题:

I'm trying to read a double from a file but I have this exception: java.util.InputMismatchException. I've tried to do the useLocale(Locale.US) but it doesn't work.

This is my code

public static void main(String[] args){
     System.out.println("Introduce the name of the file");
     Scanner teclat = new Scanner(System.in);
     teclat.useLocale(Locale.US);
     Scanner fitxer = new Scanner(new File(teclat.nextLine()));
     while(fitxer.hasNext()){
            String origen=fitxer.next();
            String desti=fitxer.next();
            double distancia=fitxer.nextDouble();
            System.out.println(origen);
            System.out.println(desti);
            System.out.println(distancia);
            ...

    }
}

Now here is the content of the file that I have to read.

city1 city2 distance(km)

string string double

Barcelona Madrid 3005.15
Barcelona Valencia 750
Los_Angeles Toronto 8026.3
......

回答1:

You can like this:

String str = "Barcelona Madrid 3005.15";
double value = Double.parseDouble(str.split(" ")[2]);

Or if you want to use regex you also can do it as below:

Pattern pattern = Pattern.compile("\\d+\\.\\d+");
Matcher matcher = pattern.matcher("Barcelona Madrid 3005.15");
if (matcher.find()) {
   double value = Double.parseDouble(matcher.group());
   System.out.println("value = " + value);
}

Hope this help.



回答2:

Since you have the tuples origin, destination and distance in a single line, it is better to read the line first, and then split to words. I see in your last example that even when the name has two parts, they are written with underscores _ instead of space. So we can safely split with space.

Try with this code:

import java.io.*;

public class Test {
    public static void main(String [] args) {

        String fileName = "file.txt";

        String line = null;

        try {
            FileReader fileReader = new FileReader(fileName);

            BufferedReader bufferedReader = new BufferedReader(fileReader);

            while((line = bufferedReader.readLine()) != null) {
                String[] parts = line.split(" ");
                String origen=parts[0];
                String desti=parts[1];
                double distancia=Double.parseDouble(parts[2]);
                System.out.println(origen);
                System.out.println(desti);
                System.out.println(distancia);
            }   

            bufferedReader.close();         
        }
        catch(FileNotFoundException ex) {
            System.out.println("Unable to open file '" + fileName + "'");                
        }
        catch(IOException ex) {
            System.out.println("Error reading file '" + fileName + "'");
        }
    }
}


回答3:

You aren't setting the Locale for the second Scanner you actually read your double from.

Add this, and your code should work :

fitxer.useLocale(Locale.US);

Note that you don't need to set the Locale for the first scanner, it is only there to pass strings, not to work with double formatting.