Results 1 to 6 of 6
- 03-11-2011, 10:52 PM #1
Member
- Join Date
- Mar 2011
- Posts
- 1
- Rep Power
- 0
Problem reading a text file into a two-dimensional character array
Hi everyone,
I'm having an issue with a project. We have to create a Maze class, where the maze is a two dimensional character array filled with characters read in from a text file using Scanner. For example, one of the files looks like this:
V V N N N
N V V N N
N N V V V
N N V N V
I have tried several different things, but none have given me the proper output. Here is what I tried to use most recently:
public void fillMaze() throws IOException
{
Scanner sc = new Scanner(System.in);
String fileName;
String line;
String[] temp;
count = 1;
System.out.println("Enter the name of the file you'd like to fill the Maze with.");
fileName = sc.nextLine();
Scanner scFile = new Scanner(new FileReader(fileName));
while(scFile.hasNext())
{
line = scFile.nextLine();
temp = new String[count];
temp[count-1] = line;
count++;
}
}
Any help or feedback anyone can give me would be much appreciated.
Thanks!
- 03-11-2011, 11:07 PM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
I have tried several different things, but none have given me the proper output.
There's nothing in the code you posted that gives any output. Things are a lot easier if you post a compilable, runnable example and actually describe the mismatch between observed and expected output.
(In questions like this you can reduce the amount of code you post, and make it easier for others to see the problem, by replacing all the file i/o stuff with a single string containing the data that would be in a typical file.)
---------------------
Your code has "temp = new String[count];" inside the while loop. That doesn't make sense: why create (and throw away) a whole bunch of increasingly larger arrays? Try creating the array once - before you start filling it with strings. Give it a nice name which describes it, like linesArr rather than temp.
- 03-11-2011, 11:12 PM #3
Member
- Join Date
- Mar 2011
- Posts
- 26
- Rep Power
- 0
hi,
I am very very new to java but I don't see any two dimensional array in your code..!
Should't you create an array of arrays to do what you want???
- 03-11-2011, 11:19 PM #4
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
I might add that if you get compiler messages don't fight them! Or out maneuver them by moving things around until the compiler shuts up. They are generally alerting you to a problem which has to be understood first, then solved. If you are unsure about a compiler message, post it.
- 03-12-2011, 01:03 AM #5
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Try to solve one step at a time. I can see lot of issues in your code actually. All of them cause that you don't have a proper design for this. First try to make a proper design, if you have that in our hand converting it into a code is not a major thing. It'll avoid lots of errors as well (unless you don't know about the basis).
- 03-12-2011, 01:04 AM #6
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Think about,
and,Java Code:String[] temp; count = 1;
Java Code:temp = new String[count]; temp[count-1] = line; count++;
Similar Threads
-
[SOLVED] Reading a text file into an Array
By DonCash in forum New To JavaReplies: 13Last Post: 01-25-2011, 12:51 AM -
Reading text file into an array
By Mahesh_ps in forum New To JavaReplies: 1Last Post: 10-09-2009, 03:04 PM -
Problem with reading text from a .txt file
By Gigi in forum New To JavaReplies: 40Last Post: 01-22-2009, 03:22 AM -
reading text character by character
By bugger in forum New To JavaReplies: 2Last Post: 11-09-2007, 08:54 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks