我无法从我的文件中读取任何double值 - Java

时间:2016-03-02 01:25:19

标签: java null double

每次我使用read.nextDouble()时都会收到此错误

java.util.InputMismatchException
null(in java.util.Scanner)

read.nextInt()read.nextLine()可以正常工作。

import java.util.*;
import java.io.*;
public class zad1
{

    public static void main() throws java.io.IOException {
        Scanner in = new Scanner (System.in);

        BufferedWriter file = new BufferedWriter(new FileWriter("array.txt"));

        System.out.println("Rows: ");
        int n = in.nextInt();

        System.out.println("Columns: ");
        int m = in.nextInt();

        double maketab[][] = new double [n][m];

        file.write("Array");
        file.newLine();
        file.write(""+n);
        file.newLine();
        file.write(""+m);
        file.newLine();
        double test = 2.2;
        file.write("" + test);
        file.newLine();
        for (int i=0; i<maketab.length; i++){
            for (int j=0; j<maketab[i].length; j++){
                maketab[i][j] = Math.random();
                file.write(maketab[i][j] + " ");
            }
            file.newLine();
        }

        file.close();

        System.out.println("Array saved.");

        Scanner read = new Scanner(new File("array.txt"));

        String name = (String) read.nextLine();
        System.out.println(nazwa);
        int w = read.nextInt();
        System.out.println(w);
        int k = read.nextInt(); 
        System.out.println(k);
        double test2 = read.nextDouble();

        double readtab[][] = new double[w][k];

        for (int i=0; i<readtab.length; i++){
            for (int j=0; j<readtab[i].length; j++){

                readtab[i][j] = read.nextDouble();
                System.out.print(readtab[i][j] + " ");

            }
        }
        System.out.println();

    }
}

输入文件:

Array
5    
5    
2.2    
0.9896001498457462 0.8934082951950629 0.457266460962939 0.002017129343240387 0.09653982445590603 0.393372683202170 0.7879437169250111 0.9985283138531686 0.285846773461949 0.9632833678080835 0.9027385338550603 0.0769347514333748 0.7005729026442885 0.38050407149893606 0.5782312365971878 0.2771563194298766 0.7932386981927726 0.7715461175169079 0.6689758153322996 0.39781450489844683 0.6495504440288017 0.5057655306472166 0.4976466087836059 0.9248534836699963 0.45108773183653694

1 个答案:

答案 0 :(得分:1)

我从你的程序中删除了所有其他的垃圾,这很好用:

import java.util.*;
import java.io.*;

public class zad1 {

    public static void main ( String[] args ) throws java.io.IOException {
        Scanner read = new Scanner(new File("array.txt"));

        String name = (String) read.nextLine();
        System.out.println(name);
        int w = read.nextInt();
        System.out.println(w);
        int k = read.nextInt(); 
        System.out.println(k);

        double readtab[][] = new double[w][k];

        for (int i = 0; i < w; ++i) {               // note change here
            for (int j = 0; j < k; ++j) {           //   and here
                readtab[i][j] = read.nextDouble();
                System.out.print(readtab[i][j] + " ");
            }
        }
        System.out.println();
    }
}

输入:

Array
5 5
2.2 0.9896001498457462 0.8934082951950629 0.457266460962939 0.002017129343240387 0.09653982445590603 0.3933726832021702 0.7879437169250111 0.9985283138531686 0.285846773461949 0.9632833678080835 0.9027385338550603 0.0769347514333748 0.7005729026442885 0.38050407149893606 0.5782312365971878 0.2771563194298766 0.7932386981927726 0.7715461175169079 0.6689758153322996 0.39781450489844683 0.6495504440288017 0.5057655306472166 0.4976466087836059 0.9248534836699963 0.45108773183653694