读取文件NoSuchElementException

时间:2012-12-26 17:42:31

标签: java file

我正在尝试用双打读取文件。当我尝试读取十进制数字时,我发现问题就出现了。在整数的情况下,一切正常。我收到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

4 个答案:

答案 0 :(得分:2)

我还想了解更多信息。

一般而言: http://docs.oracle.com/javase/1.4.2/docs/api/java/util/NoSuchElementException.html

这是一个普查员的结尾。

答案 1 :(得分:2)

有一个想法

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

抛出java.util.NoSuchElementException

正如API Scanner.nextDouble所说

 * @throws NoSuchElementException if the input is exhausted

答案 2 :(得分:0)

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;

/ **  *  * @author zenitis  * / 公共课阅读{

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

}这里

答案 3 :(得分:0)

我不明白我在txt文件中用逗号更改了点,一切正常!!