reading from text file to 2d array


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 5 of 5

Thread: reading from text file to 2d array

  1. #1
    Join Date
    Apr 2004
    Posts
    3

    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();
    }

  2. #2
    Join Date
    Dec 2002
    Posts
    83
    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

  3. #3
    Join Date
    Apr 2004
    Posts
    3
    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 "";
    	}	
    	
    }

  4. #4
    Join Date
    Feb 2004
    Posts
    808
    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];
    The 6th edict:
    "A thing of reference thing can hold either a null thing or a thing to any thing whose thing is assignment compatible with the thing of the thing" - ArchAngel, www.dictionary.com et al.
    JAR tutorial GridBag tutorial Inherited Shapes Inheritance? String.split(); FTP?

  5. #5
    Join Date
    Apr 2004
    Posts
    3
    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
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center