text file into 2d char array using bufferedReader

  • (5 Pages)
  • +
  • 1
  • 2
  • 3
  • Last »

63 Replies - 18568 Views - Last Post: 28 October 2010 - 03:52 PM Rate Topic: -----

#1 sport10  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 181
  • Joined: 21-September 10

text file into 2d char array using bufferedReader

Posted 21 October 2010 - 12:44 PM

My problem is I am trying to read a text file into a 2d array of char, but I can't get it to work. I know the solution is to open the file then read it, and get how many lines there are and use that as the first index of the 2d char array. Then I have to reopen it and read the file again and then use charAt() to put the chars into the 2nd index of the 2d array. Heres what I have:
import java.io.*;
import java.util.*;

public class Driver {
	private File mapFile = new File("map.txt");
	private char[][]maze;
	
	public static void main(String[] args) {
		Driver d = new Driver();
	}
	public Driver(){
		loadMap(mapFile);
	}
	private void loadMap(File f){
		try{
			String line;
			int ndx=0;
			FileReader fReader;
			BufferedReader reader;
			fReader = new FileReader(f);
			reader = new BufferedReader(fReader);
			String[] str;
			line = reader.readLine();
			while ( line != null ) {
				line = reader.readLine();
				ndx++;
			}
			line = reader.readLine();
			while(line != null){
				reader = new BufferedReader(fReader);
				maze = new char [ndx][];
				for(int i = 0; i < maze.length; i++) {
				    if (line!=null) {
				        String insert = line;
				        for(int x = 0; x < line.length(); x++) {
				            maze[i][x] = insert.charAt(x);
				        }
				    }
				}
				reader.close();
			}
		}catch(IOException e){
			System.out.println("INVALID FILE");
		}
	}
}



I have no idea where I should be opening and closing the bufferedReader, and I don't know if this will store the chars either. Thanks for the help in advance.
P.S I can't use Scanner for this portion of the program.

This post has been edited by sport10: 21 October 2010 - 12:46 PM


Is This A Good Question/Topic? 0
  • +

Replies To: text file into 2d char array using bufferedReader

#2 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: text file into 2d char array using bufferedReader

Posted 21 October 2010 - 02:01 PM

The String class has a toCharArray() method. So you can assign it's output directly to the maze. So after reading the file to find out how many lines are in it,
close and reopen the reader using a new FileReader and BufferedReader. Then:

...
ndx = 0;

while ((line = reader.readLine()) != null) {
    maze[ndx++] = line.toCharArray();
}
...


Was This Post Helpful? 0
  • +
  • -

#3 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2831
  • View blog
  • Posts: 11,993
  • Joined: 20-September 08

Re: text file into 2d char array using bufferedReader

Posted 21 October 2010 - 02:44 PM

This looks remarkably like another question about char[][] and a maze...
Was This Post Helpful? 0
  • +
  • -

#4 sport10  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 181
  • Joined: 21-September 10

Re: text file into 2d char array using bufferedReader

Posted 25 October 2010 - 12:00 PM

I am still having trouble with this.... Here is what I have:
import java.io.*;
import java.util.*;

public class Driver {
	private File mapFile = new File("map.txt");
	private char[][]maze;
	
	public static void main(String[] args) {
		Driver d = new Driver();
	}
	public Driver(){
		loadMap(mapFile);
	}
	private void loadMap(File f){
		try{
			String line,line2;
			int ndx=0;
			FileReader fReader;
			BufferedReader reader,reader2;
			fReader = new FileReader(f);
			reader = new BufferedReader(fReader);
			line = reader.readLine();
			while ( line != null ) {
				line = reader.readLine();
				ndx++;
			}reader.close();
			reader2 = new BufferedReader(fReader);
			line2=reader2.readLine();
			maze = new char [ndx][];
			while(line2 != null){
				line2=reader2.readLine();
				for(int i = 0; i < maze.length; i++) {
				        for(int x = 0; x < maze[i].length; x++) {
				            maze[i][x] = line2.charAt(x);
				        }
				}
			}reader2.close();
		}catch(IOException e){
			System.out.println("INVALID FILE");
		}
	}
}


The program prints out INVALID FILE when it is run. I am just trying to store the characters from a text file into a 2d array of chars. Any suggestions would be greatly appreciated, since I have been stuck on this for awhile now.

This post has been edited by sport10: 25 October 2010 - 12:10 PM

Was This Post Helpful? 0
  • +
  • -

#5 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: text file into 2d char array using bufferedReader

Posted 25 October 2010 - 12:11 PM

You are not creating a new FileReader. As I indicated in my previous post you have to create a new FileReader as well as a new BufferedReader:

Quote

close and reopen the reader using a new FileReader and BufferedReader


The reason is that when the BufferedReader is closed it releases the resources it uses.


See close
Was This Post Helpful? 1
  • +
  • -

#6 sport10  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 181
  • Joined: 21-September 10

Re: text file into 2d char array using bufferedReader

Posted 25 October 2010 - 12:19 PM

Thanks for that n8wxs. I added the filereader like you suggested. I am getting a NullPointerException now. It occurs at the bolded line in the code:
import java.io.*;
import java.util.*;

public class Driver {
	private File mapFile = new File("map.txt");
	private char[][]maze;
	
	public static void main(String[] args) {
		Driver d = new Driver();
	}
	public Driver(){
		loadMap(mapFile);
	}
	private void loadMap(File f){
		try{
			String line,line2;
			int ndx=0;
			FileReader fReader,fReader2;
			BufferedReader reader,reader2;
			fReader = new FileReader(f);
			reader = new BufferedReader(fReader);
			line = reader.readLine();
			while ( line != null ) {
				line = reader.readLine();
				ndx++;
			}reader.close();
			fReader2 = new FileReader(f);
			reader2 = new BufferedReader(fReader2);
			line2=reader2.readLine();
			maze = new char [ndx][];
			while(line2 != null){
				line2=reader2.readLine();
				for(int i = 0; i < maze.length; i++) {
				        for(int x = 0; x < maze[i].length; x++) { ///////////////// error here
				            maze[i][x] = line2.charAt(x);
				        }
				}
			}reader2.close();
		}catch(IOException e){
			System.out.println("INVALID FILE");
		}
	}
}


I am not sure why it is throwing a null pointer exception because I gave it a defined length using the ndx. Any suggestions?

This post has been edited by n8wxs: 25 October 2010 - 12:22 PM
Reason for edit:: removed bold tags - interfere with code tags

Was This Post Helpful? 0
  • +
  • -

#7 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: text file into 2d char array using bufferedReader

Posted 25 October 2010 - 12:30 PM

This is wrong.

for (int x = 0; x < maze[i].length; x++) { ///////////////// error here
    maze[i][x] = line2.charAt(x);



The whole point is that the size of the second dimension is unknown. Since the character array has not been assigned to maze yet there is no size property.

Look at the code snippet I posted in post #2
Was This Post Helpful? 1
  • +
  • -

#8 sport10  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 181
  • Joined: 21-September 10

Re: text file into 2d char array using bufferedReader

Posted 25 October 2010 - 12:45 PM

Thanks again x8wxs. The program is now working, but there is one minor error. When I try to print out a character from the 2d array it seems to be a row off. This is what my map looks like:
##########
#_________a
#______####
#_____#___#
#____#____#
#___#_____#
#___#_____#
#_________*
##########
** Ignore the underscores. It does not show up right without them.
When I print out maze[0][9] it prints out the 'a' char. Which it shouldn't it should be at [1][9].
This is the code I am now using:
import java.io.*;
import java.util.*;

public class Driver {
	private File mapFile = new File("map.txt");
	private char[][]maze;
	
	public static void main(String[] args) {
		Driver d = new Driver();
	}
	public Driver(){
		loadMap(mapFile);
	}
	private void loadMap(File f){
		try{
			String line,line2;
			int ndx=0,ndx2=0;
			FileReader fReader,fReader2;
			BufferedReader reader,reader2;
			fReader = new FileReader(f);
			reader = new BufferedReader(fReader);
			line = reader.readLine();
			while ( line != null ) {
				line = reader.readLine();
				ndx++;
			}
			reader.close();
			fReader2 = new FileReader(f);
			reader2 = new BufferedReader(fReader2);
			line2=reader2.readLine();
			maze = new char [ndx][];
			while ((line2 = reader2.readLine()) != null) {
			    maze[ndx2++] = line2.toCharArray();
			}
			System.out.println(ndx);
			System.out.println(maze[0][9]);
			reader2.close();
		}catch(IOException e){
			System.out.println("INVALID FILE");
		}
	}
}


I also tested the ndx to see if it was the correct number and it is at 9 when printed out.

This post has been edited by sport10: 25 October 2010 - 12:50 PM

Was This Post Helpful? 0
  • +
  • -

#9 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: text file into 2d char array using bufferedReader

Posted 25 October 2010 - 12:57 PM

could you post your data file please.

You are ignoring the first line of your file:

...
reader = new BufferedReader(fReader);
line = reader.readLine(); <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
while ( line != null ) {
   line = reader.readLine();
   ndx++;
}
reader.close();
fReader2 = new FileReader(f);
reader2 = new BufferedReader(fReader2);
line2=reader2.readLine(); <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
maze = new char [ndx][];
...


This post has been edited by n8wxs: 25 October 2010 - 01:02 PM

Was This Post Helpful? 1
  • +
  • -

#10 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2831
  • View blog
  • Posts: 11,993
  • Joined: 20-September 08

Re: text file into 2d char array using bufferedReader

Posted 25 October 2010 - 12:57 PM

You can simplify somewhat:

    private void loadMap(File f) {
	BufferedReader reader = null;
	try {
	    String line = null;
	    int ndx = 0;
	    reader = new BufferedReader(new FileReader(f));
	    reader.mark((int)f.length());
	    while ((line = reader.readLine()) != null) {
		line = reader.readLine();
		ndx++;
	    }

	    reader.reset();
	    maze = new char[ndx][];

	    ndx = 0;

	    while ((line = reader.readLine()) != null) {
		maze[ndx++] = line.toCharArray();
	    }


	} catch (IOException e) {
	    System.out.println("INVALID FILE");
	}
	finally {
	    try { reader.close() ; } catch(Exception e) { /* ignore */ }	

	}
    }


This post has been edited by g00se: 25 October 2010 - 12:58 PM

Was This Post Helpful? 1
  • +
  • -

#11 sport10  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 181
  • Joined: 21-September 10

Re: text file into 2d char array using bufferedReader

Posted 25 October 2010 - 01:00 PM

Heres the file.

Attached File(s)

  • Attached File  map.txt (106bytes)
    Number of downloads: 174

Was This Post Helpful? 0
  • +
  • -

#12 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: text file into 2d char array using bufferedReader

Posted 25 October 2010 - 01:03 PM

fat fingers :)

This post has been edited by n8wxs: 25 October 2010 - 01:04 PM

Was This Post Helpful? 0
  • +
  • -

#13 sport10  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 181
  • Joined: 21-September 10

Re: text file into 2d char array using bufferedReader

Posted 25 October 2010 - 01:09 PM

Is there any way to get around that? I have to read the line to execute the while loops.

**Nevermind I figured it out thanks so much for your help guys, appreciate it.

This post has been edited by sport10: 25 October 2010 - 01:13 PM

Was This Post Helpful? 0
  • +
  • -

#14 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: text file into 2d char array using bufferedReader

Posted 25 October 2010 - 01:12 PM

Both g00se and I showed you the 'correct' way to read a file using a while() loop.

This post has been edited by n8wxs: 25 October 2010 - 01:12 PM

Was This Post Helpful? 0
  • +
  • -

#15 sport10  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 181
  • Joined: 21-September 10

Re: text file into 2d char array using bufferedReader

Posted 25 October 2010 - 03:58 PM

Another minor, or could be major i don't really know, problem. When I go to System.out.println(maze) it doesn't print out the maze. It prints out the following [[C@5d0385c1
Does anyone know why this is?

This post has been edited by sport10: 25 October 2010 - 03:59 PM

Was This Post Helpful? 0
  • +
  • -

  • (5 Pages)
  • +
  • 1
  • 2
  • 3
  • Last »