Page 1 of 2 12 LastLast
Results 1 to 20 of 27

Thread: reading a file

  1. #1
    gandalf5166 is offline Member
    Join Date
    Feb 2010
    Posts
    75
    Rep Power
    0

    Default reading a file

    Ok, I have code that reads a file. It says I need to declare these two exceptions for it to work. IOException and FileNotFoundException. I really don't care, since I'll be making sure that the file IS there. What's the simplest way to take care of this?

  2. #2
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    The simplest thing is to add a throws declaration to your method.

    Java Code:
            public void readFile(String fileName) throws IOException, FileNotFoundException {
             }
    -Gary-

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,570
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by gcalvin View Post
    Java Code:
            public void readFile(String fileName) throws IOException, FileNotFoundException {
             }
    Note that a FileNotFoundException extends an IOException so if the OP really doesn't care the following will do as well:

    Java Code:
            public void readFile(String fileName) throws IOException {
             }
    kind regards,

    Jos

  4. #4
    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

    Why you really want to let them away. I mean exceptions are there for the safety for your applications. So I think rather passing them in simple way, better to handle in it's best possible way.

  5. #5
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    Quote Originally Posted by Eranga View Post
    Why you really want to let them away. I mean exceptions are there for the safety for your applications. So I think rather passing them in simple way, better to handle in it's best possible way.
    I disagree in this case. OP has said he intends to make sure the Exceptions don't happen in the first place, so if they do, a stack trace and abort seems as good as anything. I think in general, unless you know exactly what you want to do with an Exception (such as prompt for another file name) you should leave them alone and let the default Exception handler work.

    -Gary-

  6. #6
    gandalf5166 is offline Member
    Join Date
    Feb 2010
    Posts
    75
    Rep Power
    0

    Default

    Thank you! This is my homework, its not a program that's going to be widely used, so the file will be there. As such, I don't really need the whole try-catch thing. I'm glad there's an easier way. :P

  7. #7
    gandalf5166 is offline Member
    Join Date
    Feb 2010
    Posts
    75
    Rep Power
    0

    Default

    One more thing. What I'm trying to make is Boggle. I have to read in a dictionary. Problem: I'm pretty sure it only reads the first line. I'm not sure that that's the problem, but that's the assumption. Here's the code for my reader and printer.
    Java Code:
    while((temp = readFile.readLine()) != null) {
        		if (checkWord(temp)) {  
        			real.add(temp);
        		}
        	}
    Java Code:
    for (String i : real) {
        		System.out.println(i);
        	}
    The checkWord method just checks to see if the word is in the board. If you can't find a problem with these two bits of code, then I'll post the code for the method.

  8. #8
    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

    Quote Originally Posted by gcalvin View Post
    I disagree in this case. OP has said he intends to make sure the Exceptions don't happen in the first place, so if they do, a stack trace and abort seems as good as anything. I think in general, unless you know exactly what you want to do with an Exception (such as prompt for another file name) you should leave them alone and let the default Exception handler work.

    -Gary-
    I think you misunderstand what I've said. What OP says,

    I really don't care, since I'll be making sure that the file IS there.

    simply what happen if the file deleted by accident.

  9. #9
    gandalf5166 is offline Member
    Join Date
    Feb 2010
    Posts
    75
    Rep Power
    0

    Default

    My teacher has the file, he gave it to me in the first place. And like I've said, this is a homework assignment. I don't have to worry about what could happen if some idiot was using it. Just what would happen while my teacher's using it. Also, what would the catch do? It wouldn't be very helpful, since there's really nothing you can do if the file's not there. What I would probably tell it to do is abort, so I just let it abort.

  10. #10
    gandalf5166 is offline Member
    Join Date
    Feb 2010
    Posts
    75
    Rep Power
    0

    Default

    Quote Originally Posted by gandalf5166 View Post
    One more thing. What I'm trying to make is Boggle. I have to read in a dictionary. Problem: I'm pretty sure it only reads the first line. I'm not sure that that's the problem, but that's the assumption. Here's the code for my reader and printer.
    Java Code:
    while((temp = readFile.readLine()) != null) {
        		if (checkWord(temp)) {  
        			real.add(temp);
        		}
        	}
    Java Code:
    for (String i : real) {
        		System.out.println(i);
        	}
    The checkWord method just checks to see if the word is in the board. If you can't find a problem with these two bits of code, then I'll post the code for the method.
    Anyone? I really need help with this. If you can't find anything wrong, then tell me that, cause that means there's a problem with the method my teacher gave me.

  11. #11
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    I don't see a problem in your code. Of course, we can't see checkWord() or how readFile is set up, or how real is set up. Or what the file looks like, for that matter.

    -Gary-

  12. #12
    gandalf5166 is offline Member
    Join Date
    Feb 2010
    Posts
    75
    Rep Power
    0

    Default

    Ok, thank you for looking at this. Here's the code for checkword. I know its ridiculous, but I didn't make it. My teacher gave it to me.
    Java Code:
        private static void findWord(int row, int col, char[] word, int startLetterIndex) { 
    char saveChar;
    if (startLetterIndex == word.length) { 
    wordFound = true; 
    } else if (row >= 0 && row < ROWS && 
    col >= 0 && col < COLS && 
    board[row][col] == word[startLetterIndex]) { 
    saveChar = board[row][col]; 
    board[row][col] = ' '; 
    findWord(row+1, col, word, startLetterIndex+1); 
    if (!wordFound) 
    findWord(row-1, col, word, startLetterIndex+1); 
    if (!wordFound) 
    findWord(row, col+1, word, startLetterIndex+1); 
    if (!wordFound) 
    findWord(row, col-1, word, startLetterIndex+1); 
    if (!wordFound) 
    findWord(row-1, col-1, word, startLetterIndex+1); 
    if (!wordFound) 
    findWord(row+1, col-1, word, startLetterIndex+1); 
    if (!wordFound) 
    findWord(row-1, col+1, word, startLetterIndex+1); 
    if (!wordFound) 
    findWord(row+1, col+1, word, startLetterIndex+1); 
    board[row][col] = saveChar; 
    } else { 
    wordFound = false; 
    } 
    } 
        public static boolean checkWord(String word) {
                    boolean found = false;
                    int row, col;
                    char[] wordLetters;
    
                    wordLetters = word.toCharArray();
                    row = 0;
                    while (row < ROWS && !found) {                                                        //find the first letter of word
                            col =0;
                            while (col < COLS && !found) {
                                    if (wordLetters[0] == board[row][col]) {
                                            findWord(row, col, wordLetters, 0);                        //check around first letter for word
                                            found = wordFound;
                                    }
                                    if (!found) {
                                            col += 1;
                                    }
                            }
                            if (!found) {
                                    row += 1;
                            }
                    }
                    return(found);
            }
    I have no idea how it works, its an online course, and he's a horrible teacher. ReadFile is just a BufferedReader. real is a String ArrayList. The file is a "dictionary", really just a list of words. It has one word on each line. I would really appreciate any help you can give me with this. I can't figure out if this is a problem in my code or my teacher's.

  13. #13
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    Yeah, your teacher's code looks pretty bad. His findWord() method is trying to use recursion (a good idea) but doing it pretty badly. He's written a void method and he's updating a boolean instance variable, where he should have written a boolean method. He's doing a rather clumsy thing with a char[] and index, where it would be simpler if he just used a String. His checkWord() has some clumsy construction too. Having said that, it looks like they should probably work, depending on how board[][] is set up (if it's not sized larger than the actual boggle board, then findWord() is going to have some ArrayIndexOutOfBounds problems). It's hard to say for sure, because confused code is confusing code.

    I've formatted the code a bit better to make it easier for others to read.

    -Gary-

    Java Code:
            private static void findWord(int row, int col, char[] word, int startLetterIndex) { 
                    char saveChar;
                    if (startLetterIndex == word.length) { 
                            wordFound = true; 
                    } else if (row >= 0 && row < ROWS && 
                               col >= 0 && col < COLS && 
                               board[row][col] == word[startLetterIndex]) { 
                            saveChar = board[row][col]; 
                            board[row][col] = ' '; 
                            findWord(row+1, col, word, startLetterIndex+1); 
                            if (!wordFound) 
                                    findWord(row-1, col, word, startLetterIndex+1); 
                            if (!wordFound) 
                                    findWord(row, col+1, word, startLetterIndex+1); 
                            if (!wordFound) 
                                    findWord(row, col-1, word, startLetterIndex+1); 
                            if (!wordFound) 
                                    findWord(row-1, col-1, word, startLetterIndex+1); 
                            if (!wordFound) 
                                    findWord(row+1, col-1, word, startLetterIndex+1); 
                            if (!wordFound) 
                                    findWord(row-1, col+1, word, startLetterIndex+1); 
                            if (!wordFound) 
                                    findWord(row+1, col+1, word, startLetterIndex+1); 
                            board[row][col] = saveChar; 
                    } else { 
                            wordFound = false; 
                    } 
            }
     
            public static boolean checkWord(String word) {
                    boolean found = false;
                    int row, col;
                    char[] wordLetters;
    
                    wordLetters = word.toCharArray();
                    row = 0;
                    while (row < ROWS && !found) {  //find the first letter of word
                            col =0;
                            while (col < COLS && !found) {
                                    if (wordLetters[0] == board[row][col]) {
                                            findWord(row, col, wordLetters, 0);  //check around first letter for word
                                            found = wordFound;
                                    }
                                    if (!found) {
                                            col += 1;
                                    }
                            }
                            if (!found) {
                                    row += 1;
                            }
                    }
                    return(found);
            }

  14. #14
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    Actually, after another look I don't see how he's avoiding ArrayIndexOutOfBoundsException in this code. checkWord() is going to call findWord() with row=0 and col=0, and pretty quickly, findWord is going to try to look at board[-1][0], and that should cause an Exception. Are you seeing any Exceptions when you run this?

    EDIT: Never mind. I do see it now.

    -Gary-
    Last edited by gcalvin; 05-04-2010 at 09:37 PM. Reason: Bad reading

  15. #15
    gandalf5166 is offline Member
    Join Date
    Feb 2010
    Posts
    75
    Rep Power
    0

    Default

    Nope. No exceptions, just bad results.

  16. #16
    gandalf5166 is offline Member
    Join Date
    Feb 2010
    Posts
    75
    Rep Power
    0

    Default

    One strange thing. It never runs findWord. It calls on checkWord, but never uses findWord. I found this out with print statements.

  17. #17
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    Here's a crack at a better findWord() and checkWord() -- not tested or debugged or even compiled:

    Java Code:
            private static boolean findWord(int row, int col, String word) {
                    if (word.length() == 0) return true;
                    if (row < 0 || row >= ROWS) return false;
                    if (col < 0 || col >= COLS) return false;
                    if (word.charAt(0) != board[row][col]) return false;
                    char saveChar = board[row][col];
                    board[row][col] = ' ';
                    for (int r = row - 1; r <= row + 1; r++)
                            for (int c = col - 1; c <= col + 1; c++)
                                    // this will end up checking the cell we're in again,
                                    // but it will have a space character in it, so it will
                                    // return false above -- no harm done
                                    if (findWord(r, c, word.substring(1))) {
                                            board[row][col] = saveChar;
                                            return true;
                                    }
                            }
                    }
                    board[row][col] = saveChar;
                    return false;
            }
    
            private static boolean checkWord(String word) {
                    for (int row = 0; row < ROWS; row++) {
                            for (int col = 0; col < COLS; col++) {
                                    if (findWord(row, col, word)) return true;
                            }
                    }
                    return false;
            }
    -Gary-
    Last edited by gcalvin; 05-04-2010 at 09:55 PM. Reason: forgot to restore saveChar

  18. #18
    gandalf5166 is offline Member
    Join Date
    Feb 2010
    Posts
    75
    Rep Power
    0

    Default

    I'll have to go for a little while. Thanks for all your help!

  19. #19
    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

    Quote Originally Posted by gandalf5166 View Post
    My teacher has the file, he gave it to me in the first place. And like I've said, this is a homework assignment. I don't have to worry about what could happen if some idiot was using it. Just what would happen while my teacher's using it. Also, what would the catch do? It wouldn't be very helpful, since there's really nothing you can do if the file's not there. What I would probably tell it to do is abort, so I just let it abort.
    I don't care what your application does is. You may have nothing to do with it, if the file is missing. But those things are based in what you are doing in your application. If more clear the code, more easy to understand to all.

  20. #20
    gandalf5166 is offline Member
    Join Date
    Feb 2010
    Posts
    75
    Rep Power
    0

    Default

    Ok, I checked out your code. Problem: The for loop never executes, since at the start, the col/row value is 0. You subtract one, and you have negative one, so it automatically returns false. How do we solve that?

Page 1 of 2 12 LastLast

Similar Threads

  1. Replies: 9
    Last Post: 10-20-2009, 10:52 AM
  2. reading a file
    By new_coder in forum New To Java
    Replies: 6
    Last Post: 08-19-2009, 03:59 AM
  3. Replies: 3
    Last Post: 05-09-2009, 11:31 PM
  4. Reading a File
    By zoom1017 in forum New To Java
    Replies: 4
    Last Post: 02-20-2009, 03:04 AM
  5. help in reading file.
    By wanw84 in forum New To Java
    Replies: 2
    Last Post: 10-21-2008, 03:17 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
  •