Page 3 of 3 FirstFirst 123
Results 41 to 51 of 51
Like Tree5Likes

Thread: Writing file input in a matrix

  1. #41
    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 Norm View Post
    try it and let us know what happens. Extensions are NOT required
    If there is no extension, than instance of File class is representing directory, but NOT a file.

  2. #42
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Writing file input in a matrix

    Quote Originally Posted by diamonddragon View Post
    If there is no extension, than instance of File class is representing directory, but NOT a file.
    Have you tried this to verify that this is so? Can you show your code if you have?

  3. #43
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,778
    Rep Power
    25

    Default Re: Writing file input in a matrix

    If you have not written the code to show your point, you're wasting peoples time by making things up.
    Write a program, execute it and post the results.
    Fubarable likes this.

  4. #44
    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 Fubarable View Post
    Have you tried this to verify that this is so? Can you show your code if you have?
    Does it mean "If there is no extension, than instance of File class is representing directory, but NOT a file.", is not correct?

  5. #45
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Writing file input in a matrix

    Quote Originally Posted by diamonddragon View Post
    Does it mean "If there is no extension, than instance of File class is representing directory, but NOT a file.", is not correct?
    We're starting to sound like a broken record here. Again:

    have you written code to test this out yourself?

    For example, something as simple as this:

    Java Code:
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.PrintWriter;
    
    public class FooDoc {
       private static final String FILE_NAME = "Foo";
    
       public static void main(String[] args) {
          File file = new File(FILE_NAME);
          PrintWriter pw = null;
          try {
             pw = new PrintWriter(file);
             pw.println("Hello world");
          } catch (FileNotFoundException e) {
             e.printStackTrace();
          } finally {
             if (pw != null) {
                pw.close();
             }
          }
       }
    }
    Last edited by Fubarable; 01-26-2012 at 01:12 AM.
    Norm and diamonddragon like this.

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

    Default Re: Writing file input in a matrix

    Hey everyone,
    thanks for all your help, turns out that I didnt have to "system out println();" in my for each loop, such a dumb mistake.
    And for the confusing with the "YourFile" part, I just put that there to imply that you put the name fo your actual file that you want to read there.

    Java Code:
      #
                         #####
                        #######
                       #########              #
                     ############            ##
                   ################         ###
      B         ####################       ####         A
    ##########################################################￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿
    Now i get the correct output, the only problem are those weird signs at the bottom. They're not that bad, but if anyone would know how to get rid of them, that'd be awesome :D

    greetings neo

  7. #47
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  8. #48
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,778
    Rep Power
    25

    Default Re: Writing file input in a matrix

    those weird signs at the bottom.
    Your code must not fill the array and there are empty, undefined slots with values of 0..
    If you can not figure out how to change your code to properly fill the array,
    you could start at the end of the array and scan towards the front, replacing empty slots (value = 0) with spaces: ' '
    Tolls likes this.

  9. #49
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,172
    Rep Power
    20

    Default Re: Writing file input in a matrix

    Quote Originally Posted by Neovenator View Post
    Hey everyone,
    thanks for all your help, turns out that I didnt have to "system out println();" in my for each loop, such a dumb mistake.
    And for the confusing with the "YourFile" part, I just put that there to imply that you put the name fo your actual file that you want to read there.

    Java Code:
      #
                         #####
                        #######
                       #########              #
                     ############            ##
                   ################         ###
      B         ####################       ####         A
    ##########################################################???????????????????????????????????????????????????????????????????????????????????????????????????????????
    Now i get the correct output, the only problem are those weird signs at the bottom. They're not that bad, but if anyone would know how to get rid of them, that'd be awesome :D

    greetings neo
    As Norm says, that array is not being filled, which goes back to my original post I think.
    If the lines in the file are not the same length you will be creating an array that is too big.

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

    Default Re: Writing file input in a matrix

    this is the final code: can't get rid of the empty spaces at the end though, but that's not a problem for now :)

    Java Code:
    public void getLandscape() {
    
    
            try {
                FileInputStream in = new FileInputStream(new File("C:/Users/Tini/Documents/School/OO programeren/" +
                        "Opdrachten/intelliJ/Leden/src/be/kdg/inf103a/Project/Landschappen/Landschap.txt"));
    
                BufferedReader br = new BufferedReader(new InputStreamReader(in));
                Scanner scanner = new Scanner(new File("C:/Users/Tini/Documents/School/OO programeren/" +
                        "Opdrachten/intelliJ/Leden/src/be/kdg/inf103a/Project/Landschappen/Landschap.txt"));
    
    
    
                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];
                System.out.println("height: " + height + " width: " + 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); }
    
                }
    
    
    
    
               in.close();
            } catch (Exception e) {
                System.err.println("Error: " + e.getMessage());
            }
        }

  11. #51
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,172
    Rep Power
    20

    Default Re: Writing file input in a matrix

    Those wierd signs are a char 0, so you could check (on the output) for 0 and simply stop printing anything out after you encounter one of them?

Page 3 of 3 FirstFirst 123

Similar Threads

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