Results 1 to 3 of 3
  1. #1
    busdude is offline Member
    Join Date
    Oct 2008
    Posts
    25
    Rep Power
    0

    Default Can someone please help fix this code so the program works? (2D arrays)

    This program is supposed to accept a list of words from the user( user enters 'q' to stop the list), put the first word in the middle of 20 x 20 grid of chars, 2D array, and then goes through the list of words and arrange the words one by one into a crossword puzzle inside the grid of chars, if it is possible.

    It compiles, but doesn't run properly right now.


    Java Code:
    import java.util.Scanner;
    
    public class Puzzle
    {
        public String[] read (String f)
        {
            String[] words = new String [20];
          
    
            Scanner in = new Scanner (System.in);
            int count = 0;
            while (in.hasNextLine())
            {
                words[count] = in.nextLine();
                count++;
            }
            return (words);
        }
        public void write (String f)
        {
            Scanner in = new Scanner(System.in);
            String word = "";
    
            System.out.println ("Enter the new words, 'q' to stop");
            while (word != "q");
            {
                if (word != "q")
                System.out.println(in.next());
            }
    
        }
       
    }
    and


    Java Code:
    /**
     * Write a description of class A2 here.
     * 
     * @author (your name) 
     * @version (a version number or a date)
     */
    
    import java.util.Scanner;
    
    public class A2
    {
        public static void main (String[] args)
        {    
            char[][] grid = new char [20][20];  //The puzzle
            Scanner in = new Scanner(System.in);
            String f; //File name
            Puzzle puzzle = new Puzzle(); //New puzzle
            
            f = in.nextLine();
            String[] words = puzzle.read(f); //Reading words from file
            
     for (int i = 0 ; i < words[0].length(); i++) //Placing the first word
            grid[10 + i][10] = words[0].charAt(i);
        int a = 1, b = -1; //Word number, Vert/Horiz
        while (words[a] !=null)
        {
            int ok = 0;
            if (  b == 1) //If horizontal
            {
                
                for (int i = 0; i<words[a].length();i++)
                {
                    for (int j = 1; j < 19; j++)
                    for (int k = 1; k < 19; k++)
                    {
                        if (ok != 1)
                        if (grid[j][k] == words[a].charAt(i) && (j > i) && ( j +(words[a].length() - (i + 1))) < 20)
                        {
                            ok=0;
                        
                            for (int m = j - 1; m >= (j - i); m--)
                            {
                                if (grid[m][k - 1] > 0||grid[m][k] > 0||grid[m][k + 1 ] > 0)
                                ok = -1;
                                if (k < 0||k > 19||m < 0)
                                ok = -1;
                                if (grid[m - 1][k] > 0)
                                ok = -1;
                            }
                            for (int m = j + 1; m <= j + (words[a].length()-(i + 1));m++)
                            {
                                if (grid[m][k - 1] > 0||grid[m][k] > 0||grid[m][k + 1] > 0)
                                ok= -1;
                                if (k < 0||k > 19||m >= 20)
                                ok = -1;
                                if (grid[m + 1][k] > 0)
                                ok = -1;
                            }//Checking adjacent
                            if (ok == 0)
                            {
                                ok=1;
                                for (int y = j - i; y < (j - i) + words[a].length();y++)
                                grid[y][k] = words[a].charAt (y -(j - i));
                                System.out.println ("Yes" + words[a]);
                            }
                            
                        }//If letters the same
                    }//Checking puzzle for letters
                }//Going through letters of word
                if (ok == 0)
                ok = -1;
            }//If horizontal
            if (b == -1) //If vertical
            {
                if (ok!= 1)
                for (int i = 0; i<words[a].length();i++)
                {
                    for (int k = 1; k < 19; k++)
                    for (int j = 1; j < 19; j++)
                    {
                        if (ok!=1)
                        if (grid[j][k] == words[a].charAt(i) && (k > i) && (k + (words[a].length()-(i + 1)))<20) 
                        {
                            ok = 0;
                        for (int m = k - 1; m >= (k - i); m--)
                        {
                            if (grid[j - 1][m] > 0||grid[j][m] > 0||grid[j + 1][m] > 0)
                            ok = -1;
                            if (j < 0||j > 19||m <0)
                            ok = -1;
                            if (grid[j][m - 1]>0)
                            ok = -1;
                        }
                        for (int m = k + 1; m <= words[a].length() - (i + 1); m++)
                        {
                            if (grid[j - 1][m] > 0||grid[j][m] > 0||grid[j + 1][m]>0)
                            ok = -1;
                            if (j < 0||j > 19||m >= 20)
                            ok = -1;
                            if (grid[j][m+1] > 0)
                            ok = -1;
                        }//Checking adjacent                      
                        
                        if (ok == 0)
                        {
                            ok = 1;
                            for (int y = k - i; y <(k - i) + words[a].length(); y++)
                            grid[j][y]=words[a].charAt (y -(k - i));
                            System.out.println ("Vert " + words[a]);
                        }
                    }
                }//Checking for letters
            }//Going through letters of word
            if (ok == 0)
            ok = - 1;
        }//If vertical
        b =  b * -1;
        a++;
        }//While there are words
        }//Main
    }//Class

  2. #2
    matzahboy is offline Member
    Join Date
    Nov 2008
    Posts
    67
    Rep Power
    0

    Default

    What's wrong with it? What happens when it runs? It's much harder to debug when you don't know what the problem is.

  3. #3
    busdude is offline Member
    Join Date
    Oct 2008
    Posts
    25
    Rep Power
    0

    Default

    currently the problem is the list doesn't end when the person enters 'q'. this program originally had a GUI which I'm trying to remove so I don't know what is leftover from it. it could be some error with a file reader or something

Similar Threads

  1. Month and year program code!!!!HELP PLEASE!!!
    By chitwood in forum New To Java
    Replies: 4
    Last Post: 10-13-2010, 02:21 PM
  2. Graph program code,hardnut
    By Javashak in forum Advanced Java
    Replies: 0
    Last Post: 03-24-2008, 04:25 PM
  3. Help needed with java arrays code
    By d24706 in forum New To Java
    Replies: 2
    Last Post: 03-07-2008, 02:11 AM
  4. Peculiarty in code of simple program...
    By Kreuz14 in forum New To Java
    Replies: 4
    Last Post: 01-23-2008, 04:27 AM
  5. Replies: 2
    Last Post: 01-07-2008, 08:45 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
  •