2016-03-31 9 views
2

Bir dosyaya bir Double number yazmalı ve daha sonra bu dosyayı ve Double (Çift) yazmam gerekiyor, ancak bir ImputMismatchException var. Kodu ayıkladım ve sorun şu ki, dosyayı yazarken kullandığım PrintWritter, sayıyı şöyle bir noktaya yazar: 12.3. 12,3 İşteDaha sonra okumak için bir çift numara yazılması Scanner.nextDouble()

writte benim kodudur: imput böyle isnt Ve eğer o sayı okumak için kullanılan Scanner.nextDouble() ImputMismatchException döndürür burada

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

Ve kodudur okumak için: I Dont için ne yapabilir

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(); 

    } 

} 

o ImputMismatchException varsa ve doğru numarayı okumak ??

Bu benim ilk yazım ve belki de gramerimle bazı hatalar yapıyorum çünkü ispanyolca ve çok iyi ingilizce bilmiyorum.

Zaman ayırdığınız için teşekkür ederiz. noktalar ve virgüller doğru biçimde işlenmesini

cevap

2

deneyin bu gibi uygun bir yerel kullanıyorsa Scanner başlatmak için:

FileReader fr = new FileReader(archivoVentas); 
Scanner scanner = new Scanner(fr).useLocale(Locale.US); 
2
Sen aşırı String.format yöntemi kullanmak ve olduğu gibi uygun yerel ayarı belirtebilirsiniz

:

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