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();
}
}
}
以下是我在控制台获得的输出 -
The code throws an exception
null
除此之外,我得到一个空白的comp.txt文件
答案 0 :(得分:0)
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)来自代码&amp;异常消息似乎有一个NullPointerException
,因为你的数组matrix
被初始化为null而不是匹配大小的数组。
异常的解决方案是[假设你的核心逻辑很好],在为matrix
数组赋值之前,用相关大小进行初始化,即最大行数和最大值。列
答案 1 :(得分:0)
用
替换了你的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。
另外,我不完全确定你要对这条线做什么,所以我不会修复它,但那是错误。
编辑:
现在这不是一件合法的事情,但我解决了它,就像我自己做的那样,之后我会解释它。如果你不能阅读代码,因为你的导师&#34;会反对它,然后只读这篇文章的最后几行并自己解决。
// 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数组更改为数组列表,读入文件,修复了你用逗号而不是空格分割,并实际进行了交换。你不必采取这种方法,特别是如果这违反了规则。如果你不这样做,甚至不读代码。