Results 1 to 7 of 7
  1. #1
    IWanna4get is offline Member
    Join Date
    Sep 2009
    Posts
    7
    Rep Power
    0

    Default Wordhunt in Java Program

    It's an assignment and im new to arrays and we are supposed to make a program that can search a txt file in any direction (horizontal , vertical, diagonal(left and right) , backwards(h,v,d))
    ive been at it for a week and the only thing i was able to do was set the txt file into a 2d array and make the word to be search for an array... I can't figure out how to search the 2d array can anyone help

    Code:
    Java Code:
    import java.io.*;
    import java.util.Scanner;
    
    
    public class Mp6 {
    
    	 
        public static void main(String[] args) throws IOException {
        	
        	FileReader a = new FileReader("data/Data.txt");
        	BufferedReader ss = new BufferedReader(a);
        	char wla[][];
        	 int x = 80;
             int y = 80;
         	wla = new char [x][y];
        	String d;
        	Scanner p = new Scanner(System.in);
        	int c = 0;
        	int n = 0 ; 
            
            try {
               
                    for( c = 0; c < x; c++){ 
                    	d = ss.readLine(); 
                    	for ( n = 0; n<y;n++){
                    		wla[c][n]=d.charAt(n);
                    		System.out.print(wla[c][n]);               
                    	}
                    	 System.out.println("");
                    }
                
                System.out.println("Search for:");
                 String h = p.nextLine();
                 char [] ph = new char[h.length()];
                 for (int count = 0 ; count < h.length(); count++){
                	 ph[count]= h.charAt(count);
                	 System.out.print(ph[count]);
               
                 }
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
          
           
        }
    }
    DATA.txt (the txt.file)
    Last edited by IWanna4get; 03-13-2011 at 11:28 AM.

  2. #2
    Iron Lion is offline Senior Member
    Join Date
    Nov 2010
    Posts
    210
    Rep Power
    4

    Default

    I'd suggest you write 4 methods, which search the grid and return the coordinates of the first letter if the word is found, or [-1, -1] if not found:

    private int[][] searchHorizontal(String str)
    private int[][] searchVertical(String str)
    private int[][] searchUpwardsDiagonal(String str)
    private int[][] searchDownwardsDiagonal(String str)

    (Edit: if the rest of the program doesn't care about where the word is found, int[][] can be replaced with boolean.)

    and then pass the target word and its backwards counterpart to each method. I'll leave the implementation of each of those methods for you to work out.

    Also, your code would be a lot more readable if you post it in [code][/code] tags.

  3. #3
    IWanna4get is offline Member
    Join Date
    Sep 2009
    Posts
    7
    Rep Power
    0

    Default

    Quote Originally Posted by Iron Lion View Post
    I'd suggest you write 4 methods, which search the grid and return the coordinates of the first letter if the word is found, or [-1, -1] if not found:

    private int[][] searchHorizontal(String str)
    private int[][] searchVertical(String str)
    private int[][] searchUpwardsDiagonal(String str)
    private int[][] searchDownwardsDiagonal(String str)

    (Edit: if the rest of the program doesn't care about where the word is found, int[][] can be replaced with boolean.)

    and then pass the target word and its backwards counterpart to each method. I'll leave the implementation of each of those methods for you to work out.

    Also, your code would be a lot more readable if you post it in [code][/code] tags.
    http://stackoverflow.com/questions/5...rogram-in-java

    my bad...
    and yes i've though of that but i can't figure out what code to use to compare user input to each letter in the grid... this is only step i think i'm missing in order to finish the program
    Last edited by IWanna4get; 03-14-2011 at 10:28 AM. Reason: http://stackoverflow.com/questions/5296866/wordhunt-program-in-java

  4. #4
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    i'm not sure how to help you but just a suggestion - if you can't figure out your code, write in pseudo-code first, and then convert to java syntax after

  5. #5
    IWanna4get is offline Member
    Join Date
    Sep 2009
    Posts
    7
    Rep Power
    0

    Default

    i dunno how to make the pseudo-code
    but this is the thing it must do
    1.)Loop through the array finding the first letter of the word.
    2.)After finding one - search in all directions for remaining letters.o codes but it kinda goes like this

  6. #6
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    So when you read the text file, it will be read in horizontal lines and stored in an array:

    while myScanner.hasNext() {
    stringArray[i] = myScanner.nextLine();
    }

    These strings can already be searched for Horizontal matches, forwards as they are, backwards by reversing the string.

    To search diagonally you want to convert it to a 2D array, which means that in pseudo-code:
    (you'll have to make a new array to store the values in)

    for each line in stringArray {
    temparray = split the string to a new array of single characters
    string2DArray[i] = new Array(temparray)
    }

    If each line has a different number of characters, you will need to define the array with the maximum width and fill in any shorter lines with blanks/whitespace.

    Now, as Iron Lion said, make your blank methods:

    searchHorizontal(String[][] any2Darray) {

    }

    searchVertical(String[][] any2Darray) {

    }

    searchDiagonalUp(String[][] any2Darray) {

    }

    searchDiagonalDown(String[][] any2Darray) {

    }

    to help us understand what we need to do, imagine a grid labelled A-H vertical and 1-8 horizontal.
    to get the vertical lines we need to build strings A[1-8], B[1-8], C[1-8] etc
    Java Code:
    searchVertical(String[][] any2Darray) {
      for each row index "i"{
      //imagine this is A-H
        tempString = "";
        for each column index "j" {
        //imagine this is 1-8
            tempString += any2Darray[i][j]
        }
      verticalStringArray[i] = tempString;
      }
    }
    once you have your vertical strings, you can search them forwards and backwards for matches, and then you are left with diagonals (bottom to top & top to bottom).

    this time around, "i" changes with the height, so with each loop we need to go higher or lower on the grid depending on our method (bottom-to-top or top-to-bottom).

    lets start with bottom to top.

    the first thing we need from our imaginary grid is A8.
    diagonalArrayBottomUp[0] = A1
    then going up:
    diagonalArrayBottomUp[1] = A2+B1
    diagonalArrayBottomUp[2] = A3+B2+C1
    diagonalArrayBottomUp[3] = A4+B3+C2+D1
    ...
    We see that "i" must loop through A, then {A,B}, then {ABC} uptil a max of {ABCDEFGH} on the 8th loop, and then go down again.

    We see that j goes from {1}, to {2,1}, to {3,2,1} etc until it reaches the maximum in this case: A8 to H1 (8 indexes) = {8,7,6,5,4,3,2,1} and then goes down again (B1 to H2) = {7,6,5,4,3,2,1}

    Now that you see a pattern, we can try to write it in a loop. I wrote a scabble software last year which searches a 2D array diagonally so i'm trying to find that but i think i may have deleted it. The for loops were complicated and you may need to use a third for loop 'k' to get the desired result

  7. #7
    IWanna4get is offline Member
    Join Date
    Sep 2009
    Posts
    7
    Rep Power
    0

    Default

    This is where im at i am now able to get the indexes of the 1st letter of the word now i need to search all directions...

    Java Code:
    public class Mp6 {
    
    	 
        public static void main(String[] args) throws IOException {
        	
        	FileReader a = new FileReader("data/Data.txt");
        	BufferedReader ss = new BufferedReader(a);
        	char wla[][];
        	char ph[];
        	 int x = 5;
             int y = 5;
           int[] nx;
            nx=new int[2];
            int[] ny;
            ny=new int[2];
         	wla = new char [x][y];
        	String d;
        	Scanner p = new Scanner(System.in);
        	int c = 0;
        	int n = 0 ; 
            
            try {
            	for( c = 0; c < x; c++){ 
                    	d = ss.readLine(); 
                for ( n = 0; n<y;n++){
                    	wla[c][n]=d.charAt(n);
                    	System.out.print(wla[c][n]); 
                    	}
                    	 System.out.println("");
                    }
                
                System.out.println("Search for:");
                 String h = p.nextLine();
                 h=h.toUpperCase();
                  ph = new char[h.length()];
                 int counter = 0;
                 for(counter = 0 ; counter < h.length();counter++){
                	 ph[counter]=h.charAt(counter);
                	 //System.out.print(ph[counter]);
                	
                 } System.out.println("");
                 //Search 1st Letter
                 int kj=0;
                 for (int k = 0; k<wla.length;k++) {
                	    for (int j=0; j<wla[k].length;j++) {
                	        //If the first letter is found
                	        if (wla[k][j] == ph[0]) {
                	            //Search in all directions
                	        	//System.out.println(k+","+j);
                	        	//stores the coordinates, decided to store the coordinates in an array since i dunno figure i could just play with it ._.
                	        	for(int c1 = 0; c1<1;c1++){
                	        		nx[c1]=k;
                	        		ny[c1]=j;
                	        		System.out.println(nx[c1]+","+ny[c1]);
                	        		//Horizontal
                	        		//LIne of edit
                	        		
                	        	}kj++;
                	        }
                	        }
                	    }System.out.println(kj);
               
                 
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } 
        }
       
    }

Similar Threads

  1. Call one Java Program from another Java Program
    By rajpalparyani in forum New To Java
    Replies: 3
    Last Post: 02-14-2011, 05:13 AM
  2. Replies: 4
    Last Post: 07-30-2010, 01:25 PM
  3. execute java program within java program
    By popey in forum New To Java
    Replies: 2
    Last Post: 10-22-2009, 06:32 PM
  4. Replies: 0
    Last Post: 04-04-2008, 03:40 PM
  5. Replies: 0
    Last Post: 10-04-2007, 10:33 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
  •