Input file-
5
1 2 3 4 5
2 3 4 5 6
3 2 1 5 8
我的工作是我应该读取这个输入文件My .txt并交换其第一列和最后一列并将其输出到另一个文件comp.txt,但我得到一个空白文件comp.txt
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.PrintStream;
import java.io.*;
// to swap first and last column of 2D array
public class Swap
{
private static BufferedReader in = null;
private static int row = 0;
private static int column = 0;
private static int[][] matrix = null;
public static void main(String[] args) throws Exception
{
try
{
// String filepath = args[0];
int lineNum = 0;
int row = 0;
in = new BufferedReader(new FileReader("my.txt"));
String line = null;
while ((line = in.readLine()) != null)
{
lineNum++;
if (lineNum == 1)
{
column = Integer.parseInt(line);
}
else
{
String[] tokens = line.split(",");
for (int j = 0; j < tokens.length; j++)
{
if (j == 0) matrix[row][0] = Integer.parseInt(tokens[column]);
else matrix[row][j] = Integer.parseInt(tokens[j]);
}
row++;
}
}
}
catch (Exception ex)
{
System.out.println("The code throws an exception");
System.out.println(ex.getMessage());
}
finally
{
if (in != null) in.close();
}
try
{
PrintStream output = new PrintStream(new File("comp.txt"));
for (int i = 0; i < row; i++)
{
for (int j = 0; j < column; j++)
{
output.println(matrix[i][j] + " ");
}
}
output.close();
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
}
}
这是我在console-
得到的输出 The code throws an exception
null
除此之外,我得到一个空白的comp.txt文件
在我用
替换你的catch块之后 catch (Exception ex)
{
System.out.println("The code throws an exception");
System.out.println(ex.getMessage());
ex.printStackTrace();
}
我可以看出错误是
java.lang.NullPointerException
at test.example.code.Swap.main(Swap.java:37)
The code throws an exception
null
if (j == 0) matrix[row][0] = Integer.parseInt(tokens[column]);
因为你试图访问tokens
的第5列,但它实际上是一个5长度的数组,在Java和大多数基于c语言的索引通常是zero-indiced
,即有效的索引是从0到4。
另外,我不完全确定你想用这行做什么,所以我不会修复它,但这就是错误。
编辑:这不完全是一个合法的事情,但我解决了它,因为我自己会做,我将解释它之后。如果你不能阅读代码,因为你的"导师"会反对它,那么就阅读这篇文章的最后几行,并自己解决它。
// to swap first and last column of 2D array
public class Swap
{
private static BufferedReader in = null;
private static int column = 0;
private static List<int[]> matrix = null;
public static void main(String[] args) throws Exception
{
try
{
// String filepath = args[0];
in = new BufferedReader(new FileReader("my.txt"));
String line = null;
matrix = new ArrayList<int[]>(); //array length is variable length
//so using a 2D array without knowing the row size is not right
boolean isFirstLine = true;
while ((line = in.readLine()) != null)
{
if (isFirstLine)
{
column = Integer.parseInt(line); //first line determines column length
isFirstLine = false;
}
else
{
String[] tokens = line.split(" "); // there are no commas in input file so split on spaces instead
matrix.add(new int[column]);
for (int i = 0; i < tokens.length; i++)
{
matrix.get(matrix.size() - 1)[i] = Integer.parseInt(tokens[i]);
//store the lines of the currently read line in the latest added array of the list
}
}
}
}
catch (Exception ex)
{
System.out.println("The code throws an exception");
ex.printStackTrace(); //changed exception write out for better info
}
finally
{
if (in != null)
in.close();
}
// swapping the elements of first and last in each int array of list
for(int[] intLines : matrix)
{
int temp = intLines[0];
intLines[0] = intLines[intLines.length-1];
intLines[intLines.length-1] = temp;
}
BufferedWriter output = null;
try
{
output = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(new File(
"comp.txt")))); //i just prefer bufferedwriters don't mind me
output.write("" + column); //write the first line that is the array length into file
output.newLine();
for (int[] intLines : matrix) //write out each line into file
{
for (int i = 0; i < intLines.length; i++)
{
output.write("" + intLines[i]);
if (i < (intLines.length - 1))
{
output.write(" ");
}
}
output.newLine();
}
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
finally
{
if (output != null)
{
try
{
output.close();
}
catch (IOException e)
{
}
}
}
}
}
所以基本上我将2D数组更改为数组列表,在文件中读取,修复了您沿着逗号而不是空格分割的问题,并实际进行了交换。你不一定要采取这种方法,尤其是在违反规定的情况下。如果你不需要,甚至不要读代码
1)改变
System.out.println("The code throws an exception");
System.out.println(ex.getMessage());
获取异常原因的详细描述。
System.out.println("The code throws an exception");
ex.printStackTrace();
2)来自代码&异常消息,似乎有一个NullPointerException
作为我们的数组matrix
被初始化为null,而不是匹配大小的数组。
所以解决的例外是[假设你的核心逻辑是好的],在分配任何值给matrix
数组之前,初始化与相关的大小,即最大行&列