Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/87.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 写入一个双倍数字,以便以后使用Scanner.nextDouble()读取_Java_Double_Java.util.scanner - Fatal编程技术网

Java 写入一个双倍数字,以便以后使用Scanner.nextDouble()读取

Java 写入一个双倍数字,以便以后使用Scanner.nextDouble()读取,java,double,java.util.scanner,Java,Double,Java.util.scanner,我必须在文件中写入一个双精度数字,然后读取该文件和该双精度数字,但我有一个IMPUTMISMATCH异常。我已经调试了代码,问题是我用来写文件的PrintWriter写的数字带有一个点,就像这样:12.3。如果输入不是这样的,我用来读取该数字的Scanner.nextDouble()将返回InputMismatchException:12,3 以下是我要编写的代码: public void crearVentaNueva(int codigo, double precio, String nom

我必须在文件中写入一个双精度数字,然后读取该文件和该双精度数字,但我有一个IMPUTMISMATCH异常。我已经调试了代码,问题是我用来写文件的PrintWriter写的数字带有一个点,就像这样:12.3。如果输入不是这样的,我用来读取该数字的Scanner.nextDouble()将返回InputMismatchException:12,3

以下是我要编写的代码:

public void crearVentaNueva(int codigo, double precio, String nombre) throws IOException {
    FileWriter fw = new FileWriter(archivoVentas, true);
    PrintWriter pw = new PrintWriter(fw);

    pw.println(codigo + " dato " + nombre + " dato " + precio + " dato ");

    pw.close();
    fw.close();

    nVentas++;
    ventas.add(new Venta(codigo, precio, nombre));
}
下面是我的阅读代码:

private void leerArchivoVentas() throws IOException {

    int codigo;
    double precio;
    String nombre;

    try {

        FileReader fr = new FileReader(archivoVentas);
        Scanner lector = new Scanner(fr);
        nVentas = 0;
        while (lector.hasNextLine()) {
            nVentas++;
            lector.nextLine();
        }
        lector.close();
        fr.close();

        ventas = new ArrayList<Venta>();

        fr = new FileReader(archivoVentas);
        lector = new Scanner(fr);
        lector.useDelimiter("\\s*dato\\s*");

        for (int i=0; i<nVentas; i++) {

            codigo = lector.nextInt();
            nombre = lector.next();
            precio = lector.nextDouble();

            ventas.add(new Venta(codigo, precio, nombre));

        }

        lector.close();
        fr.close();

    }
    catch(Exception e) {

        FileWriter fw = new FileWriter(archivoVentas);

        ventas = new ArrayList<Venta>();
        nVentas = 0;

        fw.close();
    }
}
private void leerArchivoVentas()引发IOException{
int codigo;
双精度;
字符串名称;
试一试{
FileReader fr=新的FileReader(archivoVentas);
扫描仪选择器=新扫描仪(fr);
nVentas=0;
while(lector.hasNextLine()){
nVentas++;
lector.nextLine();
}
lector.close();
fr.close();
ventas=新阵列列表();
fr=新文件读取器(archivoVentas);
选择器=新扫描仪(fr);
选择器.useDelimiter(“\\s*dato\\s*”);

对于(int i=0;i尝试使用适当的区域设置初始化
扫描仪
,以便正确处理点和逗号,如下所示:

FileReader fr = new FileReader(archivoVentas);
Scanner scanner = new Scanner(fr).useLocale(Locale.US);

您可以使用重载的
String.format
方法并指定适当的区域设置,如中所示:

String.format(Locale.FRANCE, "%.2f", someDouble);