Hello,
I am having problems with indexOf() and contains() they work if I put letters in the code to test, but I want it to test what the user enters. And that is not working. The user enters a letter into a JTextField, and I am storing that word to a string variable, and then testing that variable in contains() and indexOf() but that is always returning the number that means the letter isn't in the word. Do you know why this is?
I am asking this for a friend, so no the code and naming of variables is not how I would have done it. Please don't correct me on these issues, I am working on helping him fix how he names variables and what not.
//Hangman a game where you test your word-guessing skills against the worlds smartest computer simulation.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.Locale;
import java.text.*;
import java.awt.AWTEvent;
import javax.swing.plaf.metal.*;
import java.util.Locale;
import java.util.Random;
import java.applet.*;
import java.net.URL;
import java.awt.font.*;
import java.lang.String;
public class hangman {
public static void main(String[] args)
{
EventQueue.invokeLater
(
new Runnable()
{
public void run()
{
hangmanframe.setDefaultLookAndFeelDecorated(true);
hangmanframe frame = new hangmanframe();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
frame.setSize(1280, 770);
//UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
}});
}
}
class hangmanframe extends JFrame
{
public hangmanframe()
{
hangmanframe.setDefaultLookAndFeelDecorated(true);
this.setSize(1280, 770);
this.setTitle("Hangman");
JMenuBar greenMenuBar = new JMenuBar();
greenMenuBar.setOpaque(true);
greenMenuBar.setBackground(new Color(154, 165, 127));
greenMenuBar.setPreferredSize(new Dimension(200, 20));
JLabel blueLabel = new JLabel();
blueLabel.setPreferredSize(new Dimension(200, 180));
this.setVisible(true);
this.setJMenuBar(greenMenuBar);
this.getContentPane().add(blueLabel, BorderLayout.CENTER);
this.pack();
GUI panel = new GUI(greenMenuBar);
this.add(panel);
}
}
class GUI extends JPanel
{
public GUI(JMenuBar menu)
{
font1 = new Font("Book Antiqua", Font.PLAIN, 50);
Random generator = new Random();
generator1 = generator.nextInt(20)+1;
NumberFormat moneyFormat = NumberFormat.getCurrencyInstance(Locale.US);
array = new String[20];
array[0] = "Fascist";
array[1] = "Dictator";
array[2] = "Alligator";
array[3] = "Watermelon";
array[4] = "Impinge";
array[5] = "Strenuous";
array[6] = "Condescending";
array[7] = "Allocate";
array[8] = "Caught in the Act";
array[9] = "Elementary";
array[10] = "Rocketship";
array[11] = "Laminate";
array[12] = "Exuberance";
array[13] = "Vivacious";
array[14] = "Dismemberment";
array[15] = "Pat Sagack";
array[16] = "Double Money";
array[17] = "Watergate";
array[18] = "Utopia";
array[19] = "Indent";
int i = 0;
if (array[0].equals (true))
{
JTextArea l = new JTextArea("YOU WON " + moneyFormat.format(total));
l.setEditable(false);
}
while(i < array[generator1-1].length())
{
lines += "-";
i++;
}
JMenu helpMenu = new JMenu("Help");
JMenu gameMenu = new JMenu("Game");
JMenu quitMenu = new JMenu ("Quit");
JMenuItem instructions = new JMenuItem("Instructions");
JMenuItem copyright = new JMenuItem("Copyright");
JMenuItem scores = new JMenuItem("Scores");
JMenuItem newgame = new JMenuItem("New Game");
JMenuItem quitgame = new JMenuItem("Quit Game");
JMenuItem restartgame = new JMenuItem("Restart Game");
panel = new JPanel(new BorderLayout());
panel = new JPanel(new BorderLayout());
panel = new JPanel(new BorderLayout());
panel = new JPanel(new BorderLayout());
gameMenu.add(newgame);
gameMenu.add(scores);
quitMenu.add(quitgame);
quitMenu.add(restartgame);
helpMenu.add(copyright);
copyright.addActionListener(new helpButton1Action());
scores.addActionListener(new scoreButtonAction());
quitgame.addActionListener(new gameButton3Action());
restartgame.addActionListener(new gameButton4Action());
newgame.addActionListener(new gameButton2Action());
menu.add(gameMenu);
menu.add(quitMenu);
menu.add(helpMenu);
helpMenu.add(instructions);
quitMenu.add(quitgame);
instructions.addActionListener(new helpButtonAction());
hangmanButton = new JButton("Start");
panel.add(hangmanButton, BorderLayout.SOUTH);
hangmanButton.addActionListener(new guessButtonAction());
waterfall = new ImageIcon("C:/Program Files/800px-Lone_House.jpg");
this.add(panel);
pictureLabel = new JLabel(waterfall);
this.add(pictureLabel);
repaint();
}
private class gameButton4Action implements ActionListener
{
public void actionPerformed(ActionEvent event){
JTextArea g = new JTextArea("\n Press OK to restart the game.\n");
g.setEditable(false);
JOptionPane.showMessageDialog(null, g, " Restart Game" , JOptionPane.PLAIN_MESSAGE);
//Next code sequence
panel.setVisible(false);
hangmanframe m = new hangmanframe();
m.setSize(1280, 770);
}
}
private class hintButtonAction implements ActionListener
{
public void actionPerformed(ActionEvent event){
hangman1 l = new hangman1();
}
}
private class guessButtonAction implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
JTextArea g = new JTextArea("\n\tHang Man\n");
g.setEditable(false);
JOptionPane.showMessageDialog(null, g, " Hangman" , JOptionPane.PLAIN_MESSAGE);
JPanel gridPanel = new JPanel(new GridLayout(5,5));
JPanel southPanel = new JPanel(new BorderLayout());
JLabel hi = new JLabel(" ");
gridPanel.add(hi, 0,0);
waterfall = new ImageIcon("C:/Program Files/Hangman-0.png");
JButton hintButton = new JButton ("Hints");
JButton inputButton = new JButton("Input Letter");
southPanel.add(inputButton, BorderLayout.SOUTH);
hintButton.addActionListener(new hintButtonAction());
inputButton.addActionListener(new inputButtonAction());
southPanel.add(hintButton, BorderLayout.WEST);
guessField = new JTextField(" ");
guessField.setColumns(1);
southPanel.add(guessField, BorderLayout.EAST);
JLabel label1 = new JLabel(waterfall);
gridPanel.add(label1, 1,0);
JLabel line = new JLabel(" " + lines);
line.setFont(font1);
JLabel label2 = new JLabel(" Guess a Letter: " +array[generator1-1]);
JLabel label3 = new JLabel(" Guess the Word" +array[generator1-1]);
gridPanel.add(line, 1,1);
southPanel.add(label2, BorderLayout.CENTER);
//southPanel.add(label3, BorderLayout.);
panel.add(gridPanel, BorderLayout.CENTER);
panel.add(southPanel, BorderLayout.NORTH);
hangmanButton.setVisible(false);
pictureLabel.setVisible(false);
validate();
repaint();
myTextField p = new myTextField(20, 1);
//turtlegraphics1 n = new turtlegraphics1();
}
}
class Score extends JPanel{
public Score(){
//TextArea h = new TextArea ("Random Letter Now Inputted" +array[0]+ +array[1]+ +array[2]+ +array[3]+ +array[4]+ +array[5]);
}
}
private class inputButtonAction implements ActionListener
{
public void actionPerformed (ActionEvent event)
{
int a;
guess = guessField.getText();
String userGuess = "i";
String word = array[1];
a = array[1].indexOf(guess);
JOptionPane.showMessageDialog(null, "" + guess + " " + array[generator1-1] + " "+ a);
//JOptionPane.showMessageDialog(null, "Letter now added");
//JOptionPane.showMessageDialog(null, "Can't input more than 1 letter at a time");
Score area = new Score();
}
}
private class helpButtonAction implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
//panel.remove(hangmanButton);
JTextArea l = new JTextArea("\n\nWelcome to Hangman; a game that tests your skills, not in a numerical way, but in a alphabetical way. \nThe rules are simple, but can be altered in your brain but the premise of the game is easy. \nYou start off by clicking on the start game button. Then you are transported to a new area in the game, the game-area. \nOne you are there you will guess letters according to how much of the hangman has decreased. \nOn that note every time you guess the wrong letter one body part will be taken away from the hangman.\n\n");
l.setEditable(false);
JOptionPane.showMessageDialog(null, l, "Instructions for Hangman", JOptionPane.PLAIN_MESSAGE);
JPanel helpPanel = new JPanel();
helpPanel.setLayout (new BorderLayout());
revalidate();
}
}
private class helpButton1Action implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
//panel.remove(hangmanButton);
JTextArea l = new JTextArea("\n\n\n© Hangman Industries, 2009. Is proud to present Hangman. A game that puts you against all odds to save the Hangmans life. See you there.\n\n\n");
l.setEditable(false);
JOptionPane.showMessageDialog(null, l, "©", JOptionPane.PLAIN_MESSAGE);
JPanel helpPanel = new JPanel();
helpPanel.setLayout (new BorderLayout());
//URL urlClick = ("C:/Program Files/New Stories (Highway Blues).wma");
//click = Applet.newAudioClip(urlClick);
click.play();
revalidate();
}
}
private class gameButton2Action implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
JTextArea l = new JTextArea("\n\tNew Game\n");
l.setEditable(false);
JOptionPane.showMessageDialog(null, l, " New Game", JOptionPane.PLAIN_MESSAGE);
JPanel helpPanel = new JPanel();
helpPanel.setLayout (new BorderLayout());
revalidate();
} }
private class gameButton3Action implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
System.exit(0);
}}
private class scoreButtonAction implements ActionListener
{
JTextArea outputTextArea = new JTextArea();
public void actionPerformed(ActionEvent event)
{
JTextArea l = new JTextArea("\tCheck your previous scores here:\n\n \tRight: Wrong: " +array[0].equals(true)+ array[0].equals(false));
JOptionPane.showMessageDialog(null, l, " Scores" , JOptionPane.PLAIN_MESSAGE);
l.setEditable(false);
}
}
class hangman1{
JTextArea outputTextArea = new JTextArea();
public hangman1(){
outputTextArea.setEditable(false);
if (generator1-1 == 0){
outputTextArea.setText("This word is a form of dictatorship");
JOptionPane.showMessageDialog(null, outputTextArea, " Hint\t\t", JOptionPane.PLAIN_MESSAGE);
}
if (generator1-1 == 1){
outputTextArea.setText("An unfair Monarchy overall");
JOptionPane.showMessageDialog(null, outputTextArea, " Hint\t\t", JOptionPane.PLAIN_MESSAGE);
}
if (generator1-1 == 2){
outputTextArea.setText("The SYNONYM is Gator; for Alligator");
JOptionPane.showMessageDialog(null, outputTextArea, " Hint\t\t", JOptionPane.PLAIN_MESSAGE);
}
if (generator1-1 == 3){
outputTextArea.setText("This word is a type of a fruit");
JOptionPane.showMessageDialog(null, outputTextArea, " Hint\t\t", JOptionPane.PLAIN_MESSAGE);
}
if (generator1-1 == 4){
outputTextArea.setText("The SYNONYM is invade; for impinge");
JOptionPane.showMessageDialog(null, outputTextArea, " Hint\t\t", JOptionPane.PLAIN_MESSAGE);
}
if (generator1-1 == 5){
outputTextArea.setText("The SYNONYM is laborious; for strenuous");
outputTextArea.setText("The SYNONYM is depress; for indent");
JOptionPane.showMessageDialog(null, outputTextArea, " Hint\t\t", JOptionPane.PLAIN_MESSAGE);
}
if (generator1-1 == 6){
outputTextArea.setText("The SYNONYM is disdainful; for condescending");
outputTextArea.setText("The SNYONYM is dreamland; for utopia");
JOptionPane.showMessageDialog(null, outputTextArea, " Hint\t\t", JOptionPane.PLAIN_MESSAGE);
}
if (generator1-1 == 7){
outputTextArea.setText("This word is a form of changing an object");
outputTextArea.setText("The SYNONYM is energetic; for vivacious");
JOptionPane.showMessageDialog(null, outputTextArea, " Hint\t\t", JOptionPane.PLAIN_MESSAGE);
}
if (generator1-1 == 8){
outputTextArea.setText("This word is an unholy action");
JOptionPane.showMessageDialog(null, outputTextArea, " Hint\t\t", JOptionPane.PLAIN_MESSAGE);
}
if (generator1-1 == 9){
outputTextArea.setText("The SYNONYM is fundamental; for Elementary");
JOptionPane.showMessageDialog(null, outputTextArea, " Hint\t\t", JOptionPane.PLAIN_MESSAGE);
}
if (generator1-1 == 10){
outputTextArea.setText("The frist letter of the word is R; for Rocket Ship");
outputTextArea.setText("The SYNONYM is raider; for marauder");
JOptionPane.showMessageDialog(null, outputTextArea, " Hint\t\t", JOptionPane.PLAIN_MESSAGE);
}
if (generator1-1 == 10){
outputTextArea.setText("The SYNONYM is night; for midnight");
JOptionPane.showMessageDialog(null, outputTextArea, " Hint\t\t", JOptionPane.PLAIN_MESSAGE);
}
if (generator1-1 == 11){
outputTextArea.setText("The SNYONYM is enclose; for laminate");
JOptionPane.showMessageDialog(null, outputTextArea, " Hint\t\t", JOptionPane.PLAIN_MESSAGE);
}
if (generator1-1 == 12){
outputTextArea.setText("This word is a type of outgoing happy emotion");
JOptionPane.showMessageDialog(null, outputTextArea, " Hint\t\t", JOptionPane.PLAIN_MESSAGE);
}
if (generator1-1 == 13){
outputTextArea.setText("This word is an extremity of happiness");
JOptionPane.showMessageDialog(null, outputTextArea, " Hint\t\t", JOptionPane.PLAIN_MESSAGE);
}
if (generator1-1 == 14){
outputTextArea.setText("This word is close, to the affects of amnesia");
JOptionPane.showMessageDialog(null, outputTextArea, " Hint\t\t", JOptionPane.PLAIN_MESSAGE);
}}
}
class myTextField extends JTextField{
int limit;
public myTextField(int show, int limit){
super(show);
this.limit = limit;
addKeyListener(new KeyAdapter() {
public void keyTyped(KeyEvent evt) {
if (guessField.getText().length() >=myTextField.this.limit)
evt.consume();}});
}
}
String guess;
JTextField guessField;
JLabel pictureLabel;
int total=100;
int generator1;
String lines = "";
ImageIcon waterfall;
JButton hangmanButton;
JPanel panel;
String array[];
AudioClip click;
Font font1;