views:

173

answers:

1

I have a few questions for this program, one the first thing I am trying to do is make it so it could compare and see if the textfield is equal to the colorValues[x] position. The second issue is the if statement says if inText == to colorValues.length - 1 to open a box that says congradulations that does not work either. 3rd issue even if it did get the Sorry message and or congradulations message how do you make it so that the textfield is not shown?

 import javax.swing.*;
 import java.awt.*;
 import java.awt.event.*;

  public class AlbertCardonaProg7 extends JFrame
 {
  private static final int WIDTH = 350;
  private static final int HEIGHT = 250;
  private static final String[] colorValues = {"red","white",
 "yellow","green","blue"};// I dentifies the colors 
  private JTextField nameBox;
  private JLabel greeting;
  private String[] message  = {"Input color number 1",
 "Input color number 2: ","Input color number 3: "
  ,"Input color number 4:","Input color number 5:"};
  private   JLabel namePrompt = new JLabel(this.message[0]);

   public AlbertCardonaProg7()
   {
   setTitle("MEMORY GAME"); 
   setSize(WIDTH, HEIGHT);
   setLayout(new FlowLayout(FlowLayout.CENTER));
   setDefaultCloseOperation(EXIT_ON_CLOSE);
   createContents();
   setVisible(true);
   }// end constructor
  //******************************************
 private void createContents()
 {
  nameBox = new JTextField(15);
  greeting = new JLabel();
  add(namePrompt);
  add(nameBox);
  add(greeting);
  nameBox.addActionListener(new Listener());
  }//end createContents

 //************************************************
private class Listener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
  int inText;
  for(inText =0; inText <  5; inText++)
   {
       if(nameBox.getText().equals(colorValues[inText] ))
    {
     namePrompt.setText( message[inText]); // its not working trying 
        //to see if it is equal to the proper spot 
        //in the colorValues[array]

         add(nameBox);
         nameBox.setText("");
         nameBox.requestFocus();
         inText++;
       }

         if(!nameBox.getText().equals(colorValues[inText]))
          {
             AlbertCardonaProg7 darn = new AlbertCardonaProg7();
             darn.namePrompt.setText("Sorry, drink more Ginseng ");

             add(namePrompt);
             break;
          }

        if( inText == (colorValues.length -1))
         {
      AlbertCardonaProg7 darn = new AlbertCardonaProg7();
      darn.namePrompt.setText("Congradulations,
            Your mind is Awesome!!!");

           add(namePrompt);
           break;
        }

      }// loop
     }//end action performed
    }// end class Listener

  //**************************************
   public static void main(String[] args)
   {
      String colors = "";
      for(int i = 0; i < colorValues.length; i++)
       colors += colorValues[i] + "  ";
      JOptionPane.showMessageDialog(null,"How good is your memory.\n
      See if you can memorize this sequence.\n\n" + colors,
      "Message", JOptionPane.INFORMATION_MESSAGE );

        AlbertCardonaProg7 outBox = new AlbertCardonaProg7();

       }// end main class
      }//end Class AlberCardonaProg7
A: 

First of all I'd suggest that you learn to format your code properly, as it makes it much easier to see whats going on.

Secondly, the code you've posted has syntax errors on lines 64 and 80 which cause it to fail compilation. The issue is that Java doesn't allow you to have multi line string literals within the source code, so you have to concatenate the two string together. For example:

darn.namePrompt.setText("Congradulations,
   Your mind is Awesome!!!");

should be:

darn.namePrompt.setText("Congradulations," + 
    " Your mind is Awesome!!!");

Now, unfortunately your question doesn't make it particularly clear as to what the intended behavior of the program should be. My interpretation of it, is that you want to provide the user with the text box asking them to enter the first color, and then display a dialog saying either congratulations or sorry depending on whether they got the answer correct. If they get the answer correct, then you want to display the input box for the second color, check the answer, etc.

My first suggestion is to create all of the controls when you instantiate the JFrame, but simply hide the other ones until the user has entered the correct value. Next, I'd suggest that you plan what the action listener is going to do before you go in and write the code.

In this case, the program needs to store the array index of the input field that the user is currently working on. The listener then needs to check this variable, and validate the corresponding field in the inputFields array. The listener needs to display a dialog to the user saying whether they got the answer correct, and if the user did, enable the next input field.

Putting it all together you get this:

import javax.swing.*;

import java.awt.*;
import java.awt.event.*;

public class AlbertCardonaProg7 extends JFrame {
 private static final int WIDTH = 350;
 private static final int HEIGHT = 250;
 private static final String[] colorValues = { "red", "white", "yellow",
  "green", "blue" };

 private final JLabel[] inputLabels = new JLabel[colorValues.length];
 private final JTextField[] inputFields = new JTextField[colorValues.length];
 private int index = 0;

 public AlbertCardonaProg7() {
  //Create the UI controls
  for (int i = 0; i < colorValues.length; i++) {
   inputLabels[i] = new JLabel("Input color number " + i + ":");
   inputLabels[i].setVisible(false);
   inputFields[i] = new JTextField(15);
   inputFields[i].setVisible(false);
   inputFields[i].addActionListener(new Listener());

   add(inputLabels[i]);
   add(inputFields[i]);
  }

  //Make the first set visible
  inputLabels[0].setVisible(true);
  inputFields[0].setVisible(true);

  setTitle("MEMORY GAME");
  setSize(WIDTH, HEIGHT);
  setLayout(new FlowLayout(FlowLayout.CENTER));
  setDefaultCloseOperation(EXIT_ON_CLOSE);
  setVisible(true);
 }

 private class Listener implements ActionListener {
  public void actionPerformed(ActionEvent e) {

   if (inputFields[index].getText().equals(colorValues[index])) {
    JOptionPane.showMessageDialog(null, "Congratulations, you got the answer correct");

    //See if there are more controls to make visible
    if (++index < colorValues.length) {
     inputLabels[index].setVisible(true);
     inputFields[index].setVisible(true);
    }
   } else {
    JOptionPane.showMessageDialog(null,
      "Sorry, your answer is wrong", "Error",
      JOptionPane.ERROR_MESSAGE);
   }
  }
 }

 public static void main(String[] args) {
  String colors = "";
  for (int i = 0; i < colorValues.length; i++) {
   colors += colorValues[i] + "  ";
  }

  JOptionPane.showMessageDialog(null, "How good is your memory.\n"
    + "See if you can memorize this sequence.\n\n" + colors,
    "Message", JOptionPane.INFORMATION_MESSAGE);

  AlbertCardonaProg7 outBox = new AlbertCardonaProg7();

 }
}

Edit: based on your comment below, I've modified the program to meet the expected behavior. The main changes are the constructor no longer hides the other controls, and the listener now has to loop over each input field to check they are all correct:

import javax.swing.*;

import java.awt.*;
import java.awt.event.*;

public class AlbertCardonaProg7 extends JFrame {
    private static final int WIDTH = 350;
    private static final int HEIGHT = 250;
    private static final String[] colorValues = { "red", "white", "yellow",
        "green", "blue" };

    private final JLabel[] inputLabels = new JLabel[colorValues.length];
    private final JTextField[] inputFields = new JTextField[colorValues.length];

    public AlbertCardonaProg7() {
        //Create the UI controls
        for (int i = 0; i < colorValues.length; i++) {
            inputLabels[i] = new JLabel("Input color number " + i + ":");
            inputFields[i] = new JTextField(15);
            inputFields[i].addActionListener(new Listener());

            add(inputLabels[i]);
            add(inputFields[i]);
        }

        setTitle("MEMORY GAME");
        setSize(WIDTH, HEIGHT);
        setLayout(new FlowLayout(FlowLayout.CENTER));
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setVisible(true);
    }

    private class Listener implements ActionListener {
        public void actionPerformed(ActionEvent e) {

            // See if there are any wrong answers
            boolean correct = true;
            for(int i = 0; i < colorValues.length; i++) {
                if (!inputFields[i].getText().equals(colorValues[i])) {
                    correct = false;
                }
            }

            if(correct) {
                JOptionPane.showMessageDialog(null, 
                        "Congratulations, you got the answer correct");
            } else {
                JOptionPane.showMessageDialog(null,
                        "Sorry, your answer is wrong", "Error",
                        JOptionPane.ERROR_MESSAGE);

            }
        }
    }

    public static void main(String[] args) {
        String colors = "";
        for (int i = 0; i < colorValues.length; i++) {
            colors += colorValues[i] + "  ";
        }

        JOptionPane.showMessageDialog(null, "How good is your memory.\n"
                + "See if you can memorize this sequence.\n\n" + colors,
                "Message", JOptionPane.INFORMATION_MESSAGE);

        AlbertCardonaProg7 outBox = new AlbertCardonaProg7();

    }
}
Jared Russell
Thank you very much, I do understand about the concatenate I only did that because it was not showing properly on my computer with this website, I had it in one line. Once again thank you very much for you answer I really do appreciate it.
daddycardona
that was about what i was going for I wanted them to remember all of the colors and then if they got them all right then say, "you are correct great job!"and if wrong say," you missed it but this is pretty awesome."
daddycardona
Ah I see. You should probable be able to see what needs changing, although I'll edit with the code that does what you're looking for.
Jared Russell