-4

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

4 回答 4

2

有个想法

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

抛出 java.util.NoSuchElementException

正如 API Scanner.nextDouble 所说

 * @throws NoSuchElementException if the input is exhausted
于 2012-12-26T17:57:24.930 回答
2

我还想了解更多信息。

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

这是枚举器的结尾。

于 2012-12-26T17:48:46.967 回答
0

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

于 2012-12-26T21:25:59.237 回答
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 */ 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) {        
    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;
}

}这里

于 2012-12-26T18:17:14.167 回答