Page 2 of 2 FirstFirst 12
Results 21 to 27 of 27

Thread: reading a file

  1. #21
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    952
    Rep Power
    5

    Default

    Quote Originally Posted by gandalf5166 View Post
    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?
    The loop will execute, and yes, its first call to findWord(-1, -1, word.substring(1)) will return false, but there's nothing wrong with that. It's exactly what we want. The next three calls ( (-1, 0), (-1, 1), (0, -1) ) will also return false, and that's OK too. The next call (0,0) is the cell we're already on, and that will return false because the character in that cell has been set to ' '. The next one after that (0,1) is the first real cell to check, but that won't be the case when we start on, say cell (3, 4). When we're on cell (0,0) there are only three out of a possible eight cells that are really valid to check, but it's better to write the method for the general case and make sure the edge cases come back false than to try to consider every special case. Hope that makes sense.

    -Gary-

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

    Default

    Okay, I fixed it so it wasn't making that mistake anymore, but its still not working. Here's my modified code:
    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] = ' ';
                    if (col < 0 && row < 0) {
                    	for (int r = row; r <= row + 1; r++) {
                            for (int c = col; c <= col + 1; c++) {
                                    if (findWord(r, c, word.substring(1))) return true;
                            }
                    	}
                    }
                    if (col < 0){
                    	for (int r = row - 1; r <= row + 1; r++) {
                            for (int c = col; c <= col + 1; c++) {
                                    if (findWord(r, c, word.substring(1))) return true;
                            }
                    	}
                    } if (row < 0) {
                    	for (int r = row; r <= row + 1; r++) {
                            for (int c = col - 1; c <= col + 1; c++) {
                                    if (findWord(r, c, word.substring(1))) return true;
                            }
                    	}
                    }
                    for (int r = row - 1; r <= row + 1; r++) {
                            for (int c = col - 1; c <= col + 1; c++) {
                                    if (findWord(r, c, word.substring(1))) return true;
                            }
                    }
                    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);
            }
    }
    Edit: That makes sense, but it doesn't explain why its not working. All I know is that the for loop never executes.
    Last edited by gandalf5166; 05-05-2010 at 10:35 PM. Reason: Didn't notice previous post

  3. #23
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    952
    Rep Power
    5

    Default

    Post all of your code as it stands now, and let's have a look at it. Please format it properly.

    -Gary-

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

    Default

    Java Code:
     import java.util.*;
     import java.lang.*;
     import java.io.*;
    public class MyBoggle2{
        static  final int ROWS = 4, COLS = 4;
    	static final int LOW = 'A', HIGH = 'Z';
    	static char[][] board = new char[4][4];
        public static void main(String[] args)  throws IOException, FileNotFoundException{
        	ArrayList<String> found = new ArrayList<String>();
    		ArrayList<String> wrong = new ArrayList<String>();
    		ArrayList<String> real = new ArrayList<String>();
    		String temp;
    		String word;
    		char restart = 'Y';	
    		Scanner input = new Scanner(System.in);
        	File dic = new File ("bogdict.txt");
    		FileReader in = new FileReader(dic);
    		BufferedReader readFile = new BufferedReader(in);
    		
        	while (restart != 'N') {
        	word = " ";
        	makeBoard();
        	real = new ArrayList<String>();
        	wrong = new ArrayList<String>();
        	found = new ArrayList<String>();
        	while((temp = readFile.readLine()) != null) {
        		if (checkWord(temp) == true) {
        			real.add(temp);
        		}
        	}
        	System.out.println("Let's play one game of Boggle: ");
        	printBoard();
        	System.out.println("Enter words or ZZ to quit: ");
        	while (word.equals("zz") == false){
        		word = input.next().toLowerCase();
        		System.out.print("");	
        	}
        	System.out.println("Words you guessed correctly: ")
        	System.out.println("Words the computer found that you could have guessed: " + real.size());
        	System.out.println("Words you could have guessed: ");
        	for (String i : real) {
        		System.out.println(i);
        	}
    
        }
        in.close();
        readFile.close();
        }
        public static void printBoard() {
        	for (int y = 0; y <= 3; y++) {
        		for (int x = 0; x <= 3; x++) {
        			System.out.print(board[y][x] + " ");
        		}
        		System.out.println("");
        	}
        }
        public static void makeBoard() {
        	for (int y = 0; y <= 3; y++) {
        		for (int x = 0; x <= 3; x++) {
        			char randomChar =(char)((int)'A'+Math.random()*((int)'Z'-(int)'A'+1));
        			board[y][x] = randomChar;
        		}
        	}
        }
        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++) {
                                    if (findWord(r, c, word.substring(1))) return true;
                            }
                    }
                    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 gandalf5166; 05-05-2010 at 11:06 PM.

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

    Default

    1. You missed my edits on the code I posted. You need to restore saveChar after checking the surrounding cells in the for loop (two places).

    2. You have one missing semicolon in your code.

    3. (The real problem) You are never getting into your for loop in findWord() because you never get a match on the first character. Why? Because your board is full of upper-case characters, and your words in your dictionary are all lower-case, and you are doing case-sensitive compares. This is partly a guess, because I don't have the actual dictionary you're using (I found another bogdict.txt on the web), but I bet it's a good one. The simplest workaround is probably to upper-case the dictionary word as you read it.

    -Gary-

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

    Default

    I fixed the case issue, and its still not working. I really don't understand what's wrong.
    Java Code:
    import java.util.*;
     import java.lang.*;
     import java.io.*;
    public class MyBoggle2{
        static  final int ROWS = 4, COLS = 4;
    	static final int LOW = 'A', HIGH = 'Z';
    	static char[][] board = new char[4][4];
        public static void main(String[] args)  throws IOException, FileNotFoundException{
        	ArrayList<String> found = new ArrayList<String>();
    		ArrayList<String> wrong = new ArrayList<String>();
    		ArrayList<String> real = new ArrayList<String>();
    		String temp;
    		String word;
    		char restart = 'Y';	
    		Scanner input = new Scanner(System.in);
        	File dic = new File ("bogdict.txt");
    		FileReader in = new FileReader(dic);
    		BufferedReader readFile = new BufferedReader(in);
    		
        	while (restart != 'N') {
        	word = " ";
        	makeBoard();
        	System.out.println("Let's play one game of Boggle: ");
        	printBoard();
        	real = new ArrayList<String>();
        	wrong = new ArrayList<String>();
        	found = new ArrayList<String>();
        	temp = readFile.readLine();
        	while(temp != null) {
        		if (checkWord(temp) == true) {
        			real.add(temp);
        		}
        		temp = readFile.readLine();
        	}
        	System.out.println("Enter words or ZZ to quit: ");
        	while (word.equals("zz") == false){
        		word = input.next();
        		System.out.print("");	
        	}
        	System.out.println("Words you guessed correctly: ");
        	System.out.println("Words the computer found that you could have guessed: " + real.size());
        	System.out.println("Words you could have guessed: ");
        	for (String i : real) {
        		System.out.println(i);
        	}
    
        }
        in.close();
        readFile.close();
        }
        public static void printBoard() {
        	for (int y = 0; y <= 3; y++) {
        		for (int x = 0; x <= 3; x++) {
        			System.out.print(board[y][x] + " ");
        		}
        		System.out.println("");
        	}
        }
        public static void makeBoard() {
        	for (int y = 0; y <= 3; y++) {
        		for (int x = 0; x <= 3; x++) {
        			board[y][x]=(char)((int)'a'+Math.random()*((int)'z'-(int)'a'+1));
        		}
        	}
        }
        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++) {
                                    if (findWord(r, c, word.substring(1))) {
                                    	board[row][col] = saveChar;
                                    	return true;
                            }
                    }
            }
            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);
            }
    }

  7. #27
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    952
    Rep Power
    5

    Default

    You really need to learn to ask better questions. You shouldn't expect us to copy, paste, compile and run your code for you.

    I told you there are two places where you need to restore saveChar. It looks like you have fixed one of them, but not the other. And it looks like where you fixed the one, you may have left out a semicolon. Format and indent your code more carefully and you will spot this sort of thing. If you can't take the time to pay attention to those kinds of details, then you are way too busy to become a programmer.

    -Gary-

    EDIT: Sorry, not a semicolon, but a closing curly brace.
    Last edited by gcalvin; 05-06-2010 at 05:48 AM.

Page 2 of 2 FirstFirst 12

Similar Threads

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