Page 1 of 3 123 LastLast
Results 1 to 20 of 51
Like Tree5Likes

Thread: Writing file input in a matrix

  1. #1
    Neovenator is offline Member
    Join Date
    Jan 2012
    Posts
    17
    Rep Power
    0

    Default Writing file input in a matrix

    Hey everyone, I'm new to this forums so I don't really know if this is a beginner or an advanced question....

    For a school assignement I need to get input from a file(a drawing) and put it in a matrix so that i can edit it. However, I created a code which should do the trick but doesn't... It stops at around halve the height of the file.... can someone please tell me what I do wrong?

    Here is the code:

    Java Code:
    public class Landscape {
    
        public void getLandscape() {
    
    
            try {
                FileInputStream in = new FileInputStream("YourFile");
    
                BufferedReader br = new BufferedReader(new InputStreamReader(in));
                Scanner scanner = new Scanner("Yourfile");
    
                int height=0, width=0;
                String currentLine;
                while (scanner.hasNextLine()){
                    height++;
                    currentLine = scanner.nextLine();
                    width = Math.max(width,currentLine.length());
    
                }
                char[][] myArray;
                myArray = new char[height][width];
    
    
    
                for (int i = 0; i < height; i++) {
    
                    for (int j = 0; j < width; j++) {
                        myArray[i][j] = (char)br.read();
                    }
                }
    
                for(char[] rij: myArray){
                    for (char c: rij){
                        System.out.print(c); }
                    System.out.println();
                }
    
    
                in.close();
            } catch (Exception e) {
                System.err.println("Error: " + e.getMessage());
            }
        }
    
    
    }
    Thanks in advance

    greetings,
    Neo

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,015
    Rep Power
    20

    Default Re: Writing file input in a matrix

    Are all lines the same length (ie width)?

  3. #3
    Neovenator is offline Member
    Join Date
    Jan 2012
    Posts
    17
    Rep Power
    0

    Default Re: Writing file input in a matrix

    Don't think so, this is the drawing I'm using, it only displays the first 4 rows, (starting from the top), but at the forth row it suddenly stops.


    Java Code:
    
                           #
                         #####
                        #######
                       #########              #
                     ############            ##
                   ################         ###
      B         ####################       ####         A
    ##########################################################

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,457
    Rep Power
    25

    Default Re: Writing file input in a matrix

    It stops at around halve the height of the file.
    What does "it stops" mean?
    If there are messages printed out, copy and paste them here.

    Your catch block should include a call to printStackTrace() while you are debugging the code. The printed trace is useful in tracking down errors.

  5. #5
    Neovenator is offline Member
    Join Date
    Jan 2012
    Posts
    17
    Rep Power
    0

    Default Re: Writing file input in a matrix

    well, I don't get any errors, when I execute my file this is my output:

    Java Code:
                           #
                         #####
                        #######
                       #########
    So, my thought is, it just doesn't read the whole file?

  6. #6
    diamonddragon is offline Senior Member
    Join Date
    Jan 2012
    Posts
    210
    Rep Power
    3

    Default Re: Writing file input in a matrix

    What is this supposed to do?
    Java Code:
                while (scanner.hasNextLine()){
                    height++;
                    currentLine = scanner.nextLine();
                    width = Math.max(width,currentLine.length()); 
                }

  7. #7
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,457
    Rep Power
    25

    Default Re: Writing file input in a matrix

    Do some debugging by adding some printlns to show the logic flow as the code executes.

    Are you trying to have two classes reading from the same file?

    What does the Scanner read? Print out what it reads.

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,015
    Rep Power
    20

    Default Re: Writing file input in a matrix

    You have created a two dimensional array based on the number of lines in the file and the MAXIMUM line width.
    You then populate each "cell" in the array with a read() from the file.
    If not all lines are the maximum width then you will not fill the array.

    Using your code:
    1234567
    123456
    12345
    1234
    123
    12
    1
    will become (in the array):
    1234567
    1234561
    2345123
    412312.
    .......
    .......
    .......
    where the dots are blanks.

    Now, from your output this does not seem to be the case.

    Anyway, I'd print out the values of height and width just before you create the array and compare that to what you would expect.

  9. #9
    Neovenator is offline Member
    Join Date
    Jan 2012
    Posts
    17
    Rep Power
    0

    Default Re: Writing file input in a matrix

    Quote Originally Posted by diamonddragon View Post
    What is this supposed to do?
    Java Code:
                while (scanner.hasNextLine()){
                    height++;
                    currentLine = scanner.nextLine();
                    width = Math.max(width,currentLine.length()); 
                }
    That was supposed to calculate the height and width from the file.
    but I've done some debugging now and i get these resulst:
    height :1
    width:125
    so doesnt seem to work :s

  10. #10
    diamonddragon is offline Senior Member
    Join Date
    Jan 2012
    Posts
    210
    Rep Power
    3

    Default Re: Writing file input in a matrix

    Quote Originally Posted by Neovenator View Post
    That was supposed to calculate the height and width from the file.
    but I've done some debugging now and i get these resulst:
    height :1
    width:125
    so doesnt seem to work :s
    And what this supposed to do?

    Java Code:
    Scanner scanner = new Scanner("Yourfile");

  11. #11
    Neovenator is offline Member
    Join Date
    Jan 2012
    Posts
    17
    Rep Power
    0

    Default Re: Writing file input in a matrix

    Quote Originally Posted by Norm View Post
    Do some debugging by adding some printlns to show the logic flow as the code executes.

    Are you trying to have two classes reading from the same file?

    What does the Scanner read? Print out what it reads.
    Thanks for that, My height and width are wrong, my height is 1 and my width is 125 :s
    and i know i'm doing double work by using a scanner AND a fileinputstream, but i just couldnt get it to work without using them both. I use the scanner to calculate the height and width and the bufferreader to print the chars in my matrix.

  12. #12
    Neovenator is offline Member
    Join Date
    Jan 2012
    Posts
    17
    Rep Power
    0

    Default Re: Writing file input in a matrix

    Quote Originally Posted by diamonddragon View Post
    And what this supposed to do?

    Java Code:
    Scanner scanner = new Scanner("Yourfile");
    It's used to read in a file in the "scanner" variable.

  13. #13
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,457
    Rep Power
    25

    Default Re: Writing file input in a matrix

    Did you try printing out what was read by the Scanner class?

  14. #14
    diamonddragon is offline Senior Member
    Join Date
    Jan 2012
    Posts
    210
    Rep Power
    3

    Default Re: Writing file input in a matrix

    Quote Originally Posted by Neovenator View Post
    It's used to read in a file in the "scanner" variable.
    Does it?

  15. #15
    Neovenator is offline Member
    Join Date
    Jan 2012
    Posts
    17
    Rep Power
    0

    Default Re: Writing file input in a matrix

    Quote Originally Posted by Norm View Post
    Did you try printing out what was read by the Scanner class?
    ow yeah, I just got this:

    Java Code:
    java.util.Scanner[delimiters=\p{javaWhitespace}+][position=0][match valid=false][need input=false][source closed=false][skipped=false][group separator=\.][decimal separator=\,][positive prefix=][negative prefix=\Q-\E][positive suffix=][negative suffix=][NaN string=\Q�\E][infinity string=\Q∞\E]
    which I can't make any sense of :s

  16. #16
    Neovenator is offline Member
    Join Date
    Jan 2012
    Posts
    17
    Rep Power
    0

    Default Re: Writing file input in a matrix

    Quote Originally Posted by diamonddragon View Post
    Does it?
    I suppose not, trying to figure out how to fix that atm.

  17. #17
    diamonddragon is offline Senior Member
    Join Date
    Jan 2012
    Posts
    210
    Rep Power
    3

    Default Re: Writing file input in a matrix

    Quote Originally Posted by Neovenator View Post
    ow yeah, I just got this:

    Java Code:
    java.util.Scanner[delimiters=\p{javaWhitespace}+][position=0][match valid=false][need input=false][source closed=false][skipped=false][group separator=\.][decimal separator=\,][positive prefix=][negative prefix=\Q-\E][positive suffix=][negative suffix=][NaN string=\Q�\E][infinity string=\Q∞\E]
    which I can't make any sense of :s
    And what this supposed to do?

    Java Code:
    FileInputStream in = new FileInputStream("YourFile");

  18. #18
    Neovenator is offline Member
    Join Date
    Jan 2012
    Posts
    17
    Rep Power
    0

    Default Re: Writing file input in a matrix

    Quote Originally Posted by diamonddragon View Post
    And what this supposed to do?

    Java Code:
    FileInputStream in = new FileInputStream("YourFile");
    it reads that same file in the varibale 'in'. I use two different ways of reading the file because they both have other properties which I need, I'm sure I can do it with just one fo the two methods but I haven't figured out how to do that either.

  19. #19
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,457
    Rep Power
    25

    Default Re: Writing file input in a matrix

    What was the variable that you printed out?
    The Scanner class is used to read a String into a variable: currentLine
    What was the contents of that variable after you read something into it using the Scanner class?

  20. #20
    diamonddragon is offline Senior Member
    Join Date
    Jan 2012
    Posts
    210
    Rep Power
    3

    Default Re: Writing file input in a matrix

    Quote Originally Posted by Neovenator View Post
    it reads that same file in the varibale 'in'. I use two different ways of reading the file because they both have other properties which I need, I'm sure I can do it with just one fo the two methods but I haven't figured out how to do that either.
    Does it?
    I thought it creates a new File instance.

    My appologies, didn't see it is FileInputStream. :(
    Last edited by diamonddragon; 01-25-2012 at 05:45 PM. Reason: mistake

Page 1 of 3 123 LastLast

Similar Threads

  1. Replies: 14
    Last Post: 07-15-2011, 05:22 AM
  2. Replies: 1
    Last Post: 02-27-2010, 01:29 AM
  3. Replies: 9
    Last Post: 10-20-2009, 10:52 AM
  4. Replies: 3
    Last Post: 05-09-2009, 11:31 PM
  5. Replies: 9
    Last Post: 07-10-2008, 04:52 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
  •