Results 1 to 4 of 4
  1. #1
    aortell24 is offline Member
    Join Date
    Jun 2012
    Location
    St.louis, Missouri
    Posts
    49
    Rep Power
    0

    Default four consecutive characters

    Hello all, I am having trouble writing an applet with a 6 by 7 matrix of text-fields if any four consecutive characters are the same i need to high lite them when the button on the applet is pressed. I am having a hard time to tie in the methods i used from a previous assignment that did not use the gui it just printed out connect four it these conditions were met.i have tried passing texteFields.getText to these methods but it did not like that. i even tried to change the method signatures to TextField instead of CHAR.Any help would be greatly appreciated.Thanks

    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    /**
     * Created with IntelliJ IDEA.
     * User: root
     * Date: 6/14/12
     * Time: 8:17 PM
     * To change this template use File | Settings | File Templates.
     */
    public class Exercise18_33 extends JApplet {
        static JTextField[][] textFields = new JTextField[6][7];
        JPanel panel1 = new JPanel();
        JPanel panel2 = new JPanel();
        JButton button = new JButton();
        JLabel label1 = new JLabel();
        JLabel label2 = new JLabel();
        JLabel label3 = new JLabel();
        JLabel label4 = new JLabel();
        int count =0;
        Exercise18_33()
        {
            setLayout(new BorderLayout());
            add(panel1, BorderLayout.CENTER);
            add(panel2, BorderLayout.SOUTH);
            panel1.setLayout(new GridLayout(6, 7, 5, 5));
            panel2.setLayout(new GridLayout(0,5));
            panel2.add(label1);
            panel2.add(label2);
            panel2.add(button);
            panel2.add(label3);
            panel2.add(label4);
            button.setText("Solve");
            button.addActionListener(new buttonEventHandler());
    
            for(int i =0; i < textFields.length; i++)
            {
                for(int j =0; j < textFields[i].length; j++)
                {
                    textFields[i][j] = new JTextField();
                }
            }
            for(int i =0; i < textFields.length; i++)
            {
                for(int j =0; j < textFields[i].length; j++)
                {
                    panel1.add(textFields[i][j]);
                }
            }
        }
        public static boolean isConsecutiveFour(char[][] values) {
            int numberOfRows = values.length;
            int numberOfColumns = values[0].length;
    
            // Check rows
            for (int i = 0; i < numberOfRows; i++) {
                if (isConsecutiveFour(values[i]))
                    return true;
            }
    
            // Check columns
            for (int j = 0; j < numberOfColumns; j++) {
                char[] column = new char[numberOfRows];
                // Get a column into an array
                for (int i = 0; i < numberOfRows; i++)
                    column[i] = values[i][j];
    
                if (isConsecutiveFour(column))
                    return true;
            }
    
            // Check major diagonal (lower part)
            for (int i = 0; i < numberOfRows - 3; i++) {
                int numberOfElementsInDiagonal
                        = Math.min(numberOfRows - i, numberOfColumns);
                char[] diagonal = new char[numberOfElementsInDiagonal];
                for (int k = 0; k < numberOfElementsInDiagonal; k++)
                    diagonal[k] = values[k + i][k];
    
                if (isConsecutiveFour(diagonal))
                    return true;
            }
    
            // Check major diagonal (upper part)
            for (int j = 1; j < numberOfColumns - 3; j++) {
                int numberOfElementsInDiagonal
                        = Math.min(numberOfColumns - j, numberOfRows);
                char[] diagonal = new char[numberOfElementsInDiagonal];
                for (int k = 0; k < numberOfElementsInDiagonal; k++)
                    diagonal[k] = values[k][k + j];
    
                if (isConsecutiveFour(diagonal))
                    return true;
            }
    
            // Check sub-diagonal (left part)
            for (int j = 3; j < numberOfColumns; j++) {
                int numberOfElementsInDiagonal
                        = Math.min(j + 1, numberOfRows);
                char[] diagonal = new char[numberOfElementsInDiagonal];
    
                for (int k = 0; k < numberOfElementsInDiagonal; k++)
                    diagonal[k] = values[k][j - k];
    
                if (isConsecutiveFour(diagonal))
                    return true;
            }
    
            // Check sub-diagonal (right part)
            for (int i = 1; i < numberOfRows - 3; i++) {
                int numberOfElementsInDiagonal
                        = Math.min(numberOfRows - i, numberOfColumns);
                char[] diagonal = new char[numberOfElementsInDiagonal];
    
                for (int k = 0; k < numberOfElementsInDiagonal; k++)
                    diagonal[k] = values[k + i][numberOfColumns - k - 1];
    
                if (isConsecutiveFour(diagonal))
                    return true;
            }
    
            return false;
        }
    
        public static boolean isConsecutiveFour(char[] values) {
            for (int i = 0; i < values.length - 3; i++) {
                boolean isEqual = true;
                for (int j = i; j < i + 3; j++) {
                    if (values[j] == '\u0000' || values[j] != values[j + 1]) {
                        isEqual = false;
                        break;
                    }
                }
    
                if (isEqual) return true;
            }
    
            return false;
        }
        class buttonEventHandler implements ActionListener
        {
            public void actionPerformed(ActionEvent actionEvent)
            {
                 for(int i =0; i < textFields.length; i++)
                 {
                     for(int j =0; j <textFields[i].length; j++)
                     {
    
                     }
                 }
    
            }
        }
    }

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,449
    Rep Power
    25

    Default Re: four consecutive characters

    it did not like that
    Please post the full text of the error messages.

    Can you explain how you are trying to use 42 textfields to find 4 consecutive characters?
    Last edited by Norm; 06-15-2012 at 10:27 PM.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    aortell24 is offline Member
    Join Date
    Jun 2012
    Location
    St.louis, Missouri
    Posts
    49
    Rep Power
    0

    Default Re: four consecutive characters

    Sorry I have been working all weekend I need to display a 6 by 7 grid on an applet when the user enters the numbers into the text fields they press the button and if any
    four consecutive textfields are the same they are to be highlighted.I am quite sorry i always seam to not post all the relative information needed.I will post the errors.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,449
    Rep Power
    25

    Default Re: four consecutive characters

    A suggestion for testing: preload all the textfields with letters so the program's logic is ready to test when it starts.

    Define a String array and get an element from the array on line 42 when defining the test field.
    Last edited by Norm; 06-19-2012 at 02:34 AM.
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Consecutive null valors identifier - Suggestions
    By Bonfy in forum New To Java
    Replies: 4
    Last Post: 01-23-2012, 05:50 PM
  2. Replies: 23
    Last Post: 07-04-2011, 02:26 AM
  3. Comparing characters.
    By javajames in forum New To Java
    Replies: 11
    Last Post: 05-12-2011, 10:30 PM
  4. Non-ASCII Characters
    By AJArmstron@aol.com in forum New To Java
    Replies: 6
    Last Post: 05-06-2010, 10:16 PM
  5. how to get the characters one by one from a String?
    By Somitesh Chakraborty in forum New To Java
    Replies: 3
    Last Post: 08-20-2008, 08:56 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
  •