2012-12-26 33 views
-3

我正在嘗試使用雙精度讀取文件。當我嘗試讀取十進制數字時,發現該問題。而在整數的情況下,一切工作正常。我收到NoSuchElementException例外。任何關於如何解決我的問題的想法?我的代碼:正在讀取一個文件NoSuchElementException

public class readd { 

protected Formatter output; 
protected Scanner input = new Scanner(System.in); 
private Scanner in = new Scanner(System.in); 
protected FileWriter out; 
protected BufferedWriter out1; 
private String ss; 
public int r=1,c=1; 
public double[][] output_matrix = null; 
public double[][] output_matrix2 = null; 
public double[] lap_time = null; 

public readd() { 
} 

public void OpenFileRead(String fileName) {   //anoigma tou arxeiou gia diavasma 
    try { 
     input = new Scanner(new File(fileName)); 
     System.out.println(fileName); 
    } catch (FileNotFoundException e) {   //sfalma kata tin evresi kai to anoigma tou arxeiou 
     System.err.println("Sfalma kata to anoigma toy arxeioy"); 
     System.exit(0);       //eksodos 
    } 
} 

public void Load() {   //anagnwsi dedomenwn apo arxeio 

    // double[][] w1 = null; 
    int count = 0; 
    int row = 0, col = 0; 
    // double [][] w1=null; 


    try { 

     while (input.hasNext()) {  //oso tha iparxei apothikeumeni eggrafi 


      count++; 
      if (count == 1) { 


       row = input.nextInt(); 
       r = row; 
       // System.out.println(row); 
       continue; 
      } else if (count == 2) { 
       col = input.nextInt(); 
       // System.out.println(col); 
       c = col; 
       continue; 
      } else { 
       //  System.out.println("col="+col); 

       output_matrix = new double[row][col]; 

       for (int i = 0; i < row; i++) { 
        for (int j = 0; j < col; j++) { 


         output_matrix[i][j] = input.nextDouble(); 

         //String ss=new Integer(input.nextInt()).toString(); 
         //w1[i][j]=Double.parseDouble(ss.trim()); 

         // String s1 = new Integer(input.nextInt()).toString(); 
         //double v = Double.parseDouble(s1.trim());           

         //String s2 = new Integer(input.nextInt()).toString(); 
         //int s = Integer.parseInt(s2.trim());   

         // System.out.print(output_matrix[i][j]+" "); 


        } 
        // System.out.println(" "); 
       } 

       //System.out.print(col); 
       //System.out.print(row); 
      } 

     } 


    } catch (NoSuchElementException e) { 
     System.err.println("Sfalma kata ti tropopoisisi toy arxeioy"); 
     System.err.println(e.getMessage());  //emfanisi tou minimatos sfalmatos 
     input.close(); 
     System.exit(0); 
    } catch (IllegalStateException e) { 
     System.err.println("Sfalma kata ti anagnosi toy arxeioy"); 
     System.exit(0); 
    } 


} 





} 
public static void main(String[] args) { 
    // TODO code application logic here 
    double[][] wa1; 

    readd w = new readd(); 
    w.OpenFileRead("W1.txt"); 
    w.Load(); 
    wa1 = w.output_matrix; 
}here 
+11

你能發表一些代碼嗎? –

+0

我認爲這個問題位於windows設置中,而不是在我的代碼中。 –

+0

@ FereRes:如果你的代碼導致一個意外的異常,那麼你的*代碼有錯誤的可能性是99.9999%,而不是別的。 –

回答

2

有個說法

Scanner sc = new Scanner("1.0"); 
sc.nextDouble(); 
sc.nextDouble(); 

拋出java.util.NoSuchElementException

就像API Scanner.nextDouble說

* @throws NoSuchElementException if the input is exhausted 
+0

你能更具體嗎? –

+0

好吧,如果我有一個可運行的代碼,我可以準確地說出發生了什麼,但在這種情況下,我可以假設在EOF之後嘗試讀取 –

2
enter package read; 

     import java.util.*; 
     import java.io.File; 
     import java.io.BufferedWriter; 
     import java.io.IOException; 
     import java.io.FileWriter; 
     import java.io.FileNotFoundException; 

/** * * @作者zenitis */ 公共類READD {

protected Formatter output; 
protected Scanner input = new Scanner(System.in); 
private Scanner in = new Scanner(System.in); 
protected FileWriter out; 
protected BufferedWriter out1; 
private String ss; 
public int r=1,c=1; 
public double[][] output_matrix = null; 
public double[][] output_matrix2 = null; 
public double[] lap_time = null; 

public readd() { 
} 

public void OpenFileRead(String fileName) {   
    try { 
     input = new Scanner(new File(fileName)); 
     System.out.println(fileName); 
    } catch (FileNotFoundException e) {   
     System.err.println("Sfalma kata to anoigma toy arxeioy"); 
     System.exit(0);       
    } 
} 

public void Load() {    

    int count = 0; 
    int row = 0, col = 0; 


    try { 

     while (input.hasNext()) {  

      count++; 
      if (count == 1) { 

       row = input.nextInt(); 
       r = row; 
       continue; 
      } else if (count == 2) { 
       col = input.nextInt(); 

       c = col; 
       continue; 
      } else { 


       output_matrix = new double[row][col]; 

       for (int i = 0; i < row; i++) { 
        for (int j = 0; j < col; j++) { 


         output_matrix[i][j] = input.nextDouble();  


        } 

       } 


      } 

     } 


    } catch (NoSuchElementException e) { 
     System.err.println("Sfalma kata ti tropopoisisi toy arxeioy"); 
     System.err.println(e.getMessage());  //emfanisi tou minimatos sfalmatos 
     input.close(); 
     System.exit(0); 
    } catch (IllegalStateException e) { 
     System.err.println("Sfalma kata ti anagnosi toy arxeioy"); 
     System.exit(0); 
    } 
} 

    public static void main(String[] args) { 
    double[][] wa1; 
    readd w = new readd(); 
    w.OpenFileRead("W1.txt"); 
    w.Load(); 
    wa1 = w.output_matrix; 
} 

}這裏

2

我不明白我改變我的txt文件,一切工作以逗號點!