-
reading from text file to 2d array
Could someone please tell me why I get a NullPointerException?
System.out.print("Enter the name of the file: ");
String fileName = readInput();
BufferedReader br = new BufferedReader(new FileReader(fileName + ".txt"));
String line;
int rowIndex = 0;
String matrix [][] = null;
while ((line = br.readLine()) != null) {
StringTokenizer st = new StringTokenizer(line);
int colIndex = 0;
while (st.hasMoreTokens()) {
matrix [rowIndex][colIndex++] = st.nextToken().trim();
}
}
rowIndex = rowIndex + 1;
br.close();
}
-
You never actually assign matrix to a new array object. The line
Code:
String matrix [][] = null;
leaves matrix as null, so when you try to load a value into it with
Code:
matrix [rowIndex][colIndex++] = st.nextToken().trim();
it's trying to assign something to null so it throws a NullPointerException.
You'll have to set the size of matrix at some point, ie matrix = new String[10][10]
-- Steven
-
I can't seem to get this right! 
What I want to do is - Read in an adjacency matrix...
here is some example input:
2 ---> this is the order of the matrix
1 ---> this is the source node
3 4
5 6
the rest of it is the matrix
I want to put this matrix into a 2d Array, the problem is this line...String matrix [][] = new String [3][3];
I do not want it to be [3][3] I want it to be [order][order]
I've tried a few things but it doesn't work!
Any help would be really appreciated Thanks
Code:
import java.io.*;
import java.util.StringTokenizer;
public class Asgn2backup {
// public static String [][] matrix;
public static void main(String[] args) throws IOException {
System.out.print("Enter the name of the file: ");
String fileName = readInput();
BufferedReader br = new BufferedReader(new FileReader(fileName + ".txt"));
String line;
int order = 0;
String matrix [][] = new String [3][3];
int rowIndex = 0;
int counter = 0;
while ((line = br.readLine()) != null) {
counter++;
if(counter == 1){
order = Integer.parseInt(line);
System.out.println("order: " + order);
}
if(counter == 2){
String source = line;
System.out.println("source " + source);
}
if(counter !=2 && counter !=1){
StringTokenizer theLine = new StringTokenizer(line);
int colIndex = 0;
while(theLine.hasMoreTokens()){
String st = theLine.nextToken();//.trim();
matrix [rowIndex][colIndex] = st;
colIndex = colIndex + 1;
}
rowIndex = rowIndex + 1;
}
}
for(int x = 0; x<matrix.length; x++){
for(int p = 0; p<matrix.length; p++){
System.out.print(matrix[x][p] + " ");
}
}
br.close();
}
private static String readInput() {
try {
BufferedReader in = new BufferedReader(
new InputStreamReader(System.in));
return in.readLine();
}
catch (IOException e) {}
return "";
}
}
-
the following code will read a string from a file, convert it to an integer and create a single array based on it. you can modify it to achieve your goal:
Code:
BufferedReader br = new BufferedReader(new FileReader("c:\my.txt"));
String line = br.readLine();
if(line == null){
System.out.print("File is empty\n");
System.exit(-1);
}
int number = Integer.parseInt(line);
String[] s = new String[number];
-
Thanks cjard, I did try that before but something else was wrong which I fixed 
I have another problem now, that code works fine for one adjacency matrix for example:
2 --- order
1 ---- source
0 4
0 0
but what if the input was:
2
1
0 4
0 0
3
2
0 1 0
0 0 2
3 0 0
I'm trying to figure out how to store one matrix (which I have done) and then do the same thing for the next one.
Code:
import java.io.*;
import java.util.StringTokenizer;
public class Asgn2backup {
public static String [][] matrix;
public static void main(String[] args) throws IOException {
System.out.print("Enter the name of the file: ");
String fileName = readInput();
BufferedReader br = new BufferedReader(new FileReader(fileName + ".txt"));
String line;
int order = 0;
int rowIndex = 0;
int counter = 0;
while ((line = br.readLine()) != null) {
counter++;
if(counter == 1){
order = Integer.parseInt(line);
matrix = new String [order][order];
System.out.println("order: " + order);
}
if(counter == 2){
String source = line;
System.out.println("source: " + source);
}
if(counter !=2 && counter !=1){
StringTokenizer theLine = new StringTokenizer(line);
int colIndex = 0;
while(theLine.hasMoreTokens()){
String st = theLine.nextToken();//.trim();
matrix [rowIndex][colIndex] = st;
colIndex = colIndex + 1;
}
rowIndex = rowIndex + 1;
}
}
for(int x = 0; x<matrix.length; x++){
for(int p = 0; p<matrix.length; p++){
System.out.print(matrix[x][p] + " ");
}
}
br.close();
}
private static String readInput() {
try {
BufferedReader in = new BufferedReader(
new InputStreamReader(System.in));
return in.readLine();
}
catch (IOException e) {}
return "";
}
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks