我已经编码到从用户提供的txt文件中打印出数组的程度。我正在尝试运行我专门为将txt文件放入数组后的行求和而创建的线程,但是现在我被卡住了,因为我无法获得rowSum线程来实际对行求和。我发布了整个文件只是为了确保我提供了足够的信息以帮助他人。
public class Assignment1 {
public static void main(String[] args) throws FileNotFoundException {
// TODO Auto-generated method stub
rowSum t1 = new rowSum();
t1.start();
System.out.println("Enter txt file for summation.");
Scanner scan = new Scanner(System.in);
String txtFile = scan.nextLine();
File file = new File(txtFile);
int[][] matrix = new int[1000][1000];
int x=0, y=0;
try
{
BufferedReader in = new BufferedReader(new FileReader(file)); //reading files in specified directory
String line;
while ((line = in.readLine()) != null) //file reading
{
String[] values = line.split(" ");
for (String str : values )
{
int str_double = (int) Double.parseDouble(str);
matrix[x][y]=str_double;
System.out.print(matrix[x][y] + " ");
y=y+1; //you have inserted a value to the former y, need to increment
}
x=x+1; // finished the row, need to increment the row number
System.out.println("");
//in.close();
}
// int rowSize = matrix.length;
// int columnSize = matrix[0].length;
// System.out.println("rows=" + rowSize + "cols=" + columnSize);
}
catch( IOException ioException ) {}
}
class rowSum implements Runnable{
int c[][];
public rowSum(int c[][]) {
this. c = matrix[][];
}
public void start() {
// TODO Auto-generated method stub
}
}
public void run() {
int i,j,sum = 0;
System.out.print( "\nFinding Sum of each row:\n\n");
// finding the row sum
for (i = 0; i < 4; ++i) {
for (j = 0; j < 4; ++j) {
// Add the element
sum = sum + matrix[i][j];
}
// Print the row sum
System.out.println( "Sum of the row "
+ i + " = " + sum);
// Reset the sum
sum = 0;
}
}
}
The program crashes on me. I think my problem is passing the matrix to the thread but I could be wrong.