所以基本上我正在尝试读取包含以下内容的.txt文件:
3 5
2 3 4 5 10
4 5 2 3 7
-3 -1 0 1 5
并将它们存储到2D数组中并将其打印在控制台上,我从控制台获得的内容很好,但只缺少第一行3 5,我不知道我的代码有什么问题让它忽略了第一行。 我现在得到的是什么:
2 3 4 5 10
4 5 2 3 7
-3 -1 0 1 5
import java.io.*;
import java.util.*;
public class Driver0 {
public static int[][] array;
public static int dimension1, dimension2;
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Welcome to Project 0.");
System.out.println("What is the name of the data file? ");
String file = input.nextLine();
readFile(file);
}
public static void readFile(String file) {
try {
Scanner sc = new Scanner(new File(file));
dimension1 = sc.nextInt();
dimension2 = sc.nextInt();
array = new int[dimension1][dimension2];
while (sc.hasNext()) {
for (int row = 0; row < dimension1; row++) {
for (int column = 0; column < dimension2; column++) {
array[row][column] = sc.nextInt();
System.out.printf("%2d ", array[row][column]);
}
System.out.println();
}
}
sc.close();
}
catch (Exception e) {
System.out
.println("Error: file not found or insufficient requirements.");
}
}
}
答案 0 :(得分:0)
前两个值被保存到dimension1和dimension2变量中,所以当稍后调用sc.nextInt时,它已经读取了前两个数字并移到下一行。所以那些第一个整数不会进入阵列。
答案 1 :(得分:0)
您正在阅读这部分代码中的这些数字:
dimension1 = sc.nextInt();
dimension2 = sc.nextInt();
因此dimension1
获取值3,dimension2
获取值5,但您不会将它们保存到数组中。
答案 2 :(得分:0)
试试这个代码......
import java.io.*;
import javax.swing.*;
import java.util.*;
import java.awt.*;
public class Proj4 {
public static int rows, cols;
public static int[][] cells;
/**
* main reads the file and starts
* the graphical display
*/
public static void main(String[] args) throws IOException {
Scanner s = new Scanner(System.in);
String file = JOptionPane.showInputDialog(null, "Enter the input file name: ");
Scanner inFile = new Scanner(new File(file));
rows = Integer.parseInt(inFile.nextLine());
cols = Integer.parseInt(inFile.nextLine());
cells = new int[rows][cols];
//this is were I need help
for(int i=0; i < rows; i++)
{
String line = inFile.nextLine();
line = line.substring(0);
}
inFile.close();
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
System.out.print(cells[i][j]);
}
System.out.print();
}
答案 3 :(得分:0)
需要考虑的一些事项:
Files.readAllLines()
一次性阅读整个文件。此函数返回List<String>
,其中包含文件的所有行。如果有任何空白行,请删除它们,现在您要为2d数组声明行数。String.split()
中每行的简单List<String>
将为您提供每行应具有的列数。例如:
public static void main(String[] args) throws Exception {
// Read the entire file in
List<String> myFileLines = Files.readAllLines(Paths.get("MyFile.txt"));
// Remove any blank lines
for (int i = myFileLines.size() - 1; i >= 0; i--) {
if (myFileLines.get(i).isEmpty()) {
myFileLines.remove(i);
}
}
// Declare you 2d array with the amount of lines that were read from the file
int[][] intArray = new int[myFileLines.size()][];
// Iterate through each row to determine the number of columns
for (int i = 0; i < myFileLines.size(); i++) {
// Split the line by spaces
String[] splitLine = myFileLines.get(i).split("\\s");
// Declare the number of columns in the row from the split
intArray[i] = new int[splitLine.length];
for (int j = 0; j < splitLine.length; j++) {
// Convert each String element to an integer
intArray[i][j] = Integer.parseInt(splitLine[j]);
}
}
// Print the integer array
for (int[] row : intArray) {
for (int col : row) {
System.out.printf("%5d ", col);
}
System.out.println();
}
}
结果:
3 5
2 3 4 5 10
4 5 2 3 7
-3 -1 0 1 5
答案 4 :(得分:0)
您可以使用Stream API轻松实现
SelectionChanged