我正在尝试用doubles读取一个文件。当我试图读取十进制数字时,我发现了这个问题。而在整数的情况下,一切都很好。我收到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
我还想了解更多信息。
一般而言:http://docs.oracle.com/javase/1.4.2/docs/api/java/util/NoSuchElementException.html
这是枚举器的结束。
有一个想法
Scanner sc = new Scanner("1.0");
sc.nextDouble();
sc.nextDouble();
throws java.util.NoSuchElementException
正如API扫描仪.nextDouble所说的
* @throws NoSuchElementException if the input is exhausted
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*/已读取公共类{
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;
}
}此处
我不明白我在txt文件中用逗号更改了点,一切都正常!!