- Java code
package matrix;import java.io.*;import java.io.BufferedReader;import java.io.FileReader;import java.io.IOException;import java.util.Scanner;import java.util.Locale;public class Matrix { /** * @param args the command line arguments */ public static void main(String[] args) throws IOException{ // TODO code application logic here Scanner scanner = null; int count = 0; try { scanner = new Scanner(new BufferedReader(new FileReader("C:/Users/Documents/NetBeansProjects/Matrix/src/matrix/PersonA.txt" ))); while (scanner.hasNext()) { if (scanner.hasNextDouble()) { count++; } else { scanner.next(); } } } finally { scanner.close(); } for (int i = 0; i < (count/6); i++){ for (int j = 0; j < 6; j++){ double[][] array = new double[i][j]; array[i][j] = scanner.nextDouble(); System.out.println( array[i][j]); } } } }
这个PersonA.txt里面是共计一万多个类型为double的数子,应该是6个一组。
现在想把它们读取到二维数组里,共计n行6列。
像上面那样运行,半个小时也不出结果。不知道哪得问题,谁能给指点一下。
------解决方案--------------------
BufferedReader readLine试试。
------解决方案--------------------
while循环的代码逻辑有问题,如果检索到了double,你就不会执行next,修改为:
- Java code
while (scanner.hasNext()) { if (scanner.hasNextDouble()) { count++; } scanner.next(); }