Results 1 to 20 of 27
Thread: reading a file
- 05-04-2010, 06:36 AM #1
Member
- Join Date
- Feb 2010
- Posts
- 75
- Rep Power
- 0
- 05-04-2010, 06:58 AM #2
Senior Member
- Join Date
- Mar 2010
- Posts
- 953
- Rep Power
- 4
The simplest thing is to add a throws declaration to your method.
-Gary-Java Code:public void readFile(String fileName) throws IOException, FileNotFoundException { }
- 05-04-2010, 07:22 AM #3
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,400
- Blog Entries
- 7
- Rep Power
- 17
- 05-04-2010, 10:02 AM #4
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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.
- 05-04-2010, 10:16 AM #5
Senior Member
- Join Date
- Mar 2010
- Posts
- 953
- Rep Power
- 4
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-
- 05-04-2010, 03:40 PM #6
Member
- Join Date
- Feb 2010
- Posts
- 75
- Rep Power
- 0
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
- 05-04-2010, 05:26 PM #7
Member
- Join Date
- Feb 2010
- Posts
- 75
- Rep Power
- 0
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); } }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.Java Code:for (String i : real) { System.out.println(i); }
- 05-04-2010, 05:49 PM #8
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
- 05-04-2010, 05:54 PM #9
Member
- Join Date
- Feb 2010
- Posts
- 75
- Rep Power
- 0
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.
- 05-04-2010, 06:27 PM #10
Member
- Join Date
- Feb 2010
- Posts
- 75
- Rep Power
- 0
- 05-04-2010, 08:34 PM #11
Senior Member
- Join Date
- Mar 2010
- Posts
- 953
- Rep Power
- 4
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-
- 05-04-2010, 08:44 PM #12
Member
- Join Date
- Feb 2010
- Posts
- 75
- Rep Power
- 0
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.
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.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); }
- 05-04-2010, 09:22 PM #13
Senior Member
- Join Date
- Mar 2010
- Posts
- 953
- Rep Power
- 4
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); }
- 05-04-2010, 09:32 PM #14
Senior Member
- Join Date
- Mar 2010
- Posts
- 953
- Rep Power
- 4
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
- 05-04-2010, 09:37 PM #15
Member
- Join Date
- Feb 2010
- Posts
- 75
- Rep Power
- 0
Nope. No exceptions, just bad results.
- 05-04-2010, 09:41 PM #16
Member
- Join Date
- Feb 2010
- Posts
- 75
- Rep Power
- 0
One strange thing. It never runs findWord. It calls on checkWord, but never uses findWord. I found this out with print statements.
- 05-04-2010, 09:49 PM #17
Senior Member
- Join Date
- Mar 2010
- Posts
- 953
- Rep Power
- 4
Here's a crack at a better findWord() and checkWord() -- not tested or debugged or even compiled:
-Gary-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; }Last edited by gcalvin; 05-04-2010 at 09:55 PM. Reason: forgot to restore saveChar
- 05-04-2010, 09:53 PM #18
Member
- Join Date
- Feb 2010
- Posts
- 75
- Rep Power
- 0
I'll have to go for a little while. Thanks for all your help!
- 05-05-2010, 02:42 AM #19
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
- 05-05-2010, 08:57 PM #20
Member
- Join Date
- Feb 2010
- Posts
- 75
- Rep Power
- 0
Similar Threads
-
Reading and Writing the contents of a file to another file
By priyankatxs in forum New To JavaReplies: 9Last Post: 10-20-2009, 10:52 AM -
reading a file
By new_coder in forum New To JavaReplies: 6Last Post: 08-19-2009, 03:59 AM -
[SOLVED] how to reading binary file and writing txt file
By tOpach in forum New To JavaReplies: 3Last Post: 05-09-2009, 11:31 PM -
Reading a File
By zoom1017 in forum New To JavaReplies: 4Last Post: 02-20-2009, 03:04 AM -
help in reading file.
By wanw84 in forum New To JavaReplies: 2Last Post: 10-21-2008, 03:17 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks