Results 1 to 6 of 6
  1. #1
    wvu5004 is offline Member
    Join Date
    Mar 2011
    Posts
    1
    Rep Power
    0

    Default 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!

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    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.

  3. #3
    Stamoulohta is offline Member
    Join Date
    Mar 2011
    Posts
    26
    Rep Power
    0

    Default

    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???

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    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.

  5. #5
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    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).

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Think about,

    Java Code:
    String[] temp;
    count = 1;
    and,

    Java Code:
    temp = new String[count];
    temp[count-1] = line;
    count++;

Similar Threads

  1. [SOLVED] Reading a text file into an Array
    By DonCash in forum New To Java
    Replies: 13
    Last Post: 01-25-2011, 12:51 AM
  2. Reading text file into an array
    By Mahesh_ps in forum New To Java
    Replies: 1
    Last Post: 10-09-2009, 03:04 PM
  3. Problem with reading text from a .txt file
    By Gigi in forum New To Java
    Replies: 40
    Last Post: 01-22-2009, 03:22 AM
  4. reading text character by character
    By bugger in forum New To Java
    Replies: 2
    Last Post: 11-09-2007, 08:54 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •