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
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++)
{
}
}
}
}
}
Re: four consecutive characters
Quote:
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?
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.
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.