problem with indexOf() and contains()

Reply

Join Date: Apr 2008
Posts: 108
Reputation: christiangirl is an unknown quantity at this point 
Solved Threads: 1
christiangirl christiangirl is offline Offline
Junior Poster

problem with indexOf() and contains()

 
0
  #1
May 20th, 2009
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.

  1. //Hangman a game where you test your word-guessing skills against the worlds smartest computer simulation.
  2. import java.awt.*;
  3. import java.awt.event.*;
  4. import javax.swing.*;
  5. import java.util.Locale;
  6. import java.text.*;
  7. import java.awt.AWTEvent;
  8. import javax.swing.plaf.metal.*;
  9. import java.util.Locale;
  10. import java.util.Random;
  11. import java.applet.*;
  12. import java.net.URL;
  13. import java.awt.font.*;
  14. import java.lang.String;
  15.  
  16.  
  17.  
  18. public class hangman {
  19.  
  20. public static void main(String[] args)
  21. {
  22.  
  23.  
  24. EventQueue.invokeLater
  25. (
  26. new Runnable()
  27. {
  28.  
  29. public void run()
  30. {
  31.  
  32. hangmanframe.setDefaultLookAndFeelDecorated(true);
  33. hangmanframe frame = new hangmanframe();
  34. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  35. frame.setVisible(true);
  36. frame.setSize(1280, 770);
  37.  
  38.  
  39.  
  40.  
  41. //UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
  42.  
  43.  
  44. }});
  45. }
  46.  
  47. }
  48.  
  49.  
  50. class hangmanframe extends JFrame
  51. {
  52.  
  53. public hangmanframe()
  54. {
  55. hangmanframe.setDefaultLookAndFeelDecorated(true);
  56. this.setSize(1280, 770);
  57. this.setTitle("Hangman");
  58.  
  59. JMenuBar greenMenuBar = new JMenuBar();
  60. greenMenuBar.setOpaque(true);
  61. greenMenuBar.setBackground(new Color(154, 165, 127));
  62. greenMenuBar.setPreferredSize(new Dimension(200, 20));
  63. JLabel blueLabel = new JLabel();
  64. blueLabel.setPreferredSize(new Dimension(200, 180));
  65. this.setVisible(true);
  66. this.setJMenuBar(greenMenuBar);
  67. this.getContentPane().add(blueLabel, BorderLayout.CENTER);
  68. this.pack();
  69. GUI panel = new GUI(greenMenuBar);
  70. this.add(panel);
  71.  
  72. }
  73. }
  74. class GUI extends JPanel
  75. {
  76. public GUI(JMenuBar menu)
  77. {
  78. font1 = new Font("Book Antiqua", Font.PLAIN, 50);
  79. Random generator = new Random();
  80. generator1 = generator.nextInt(20)+1;
  81. NumberFormat moneyFormat = NumberFormat.getCurrencyInstance(Locale.US);
  82. array = new String[20];
  83. array[0] = "Fascist";
  84. array[1] = "Dictator";
  85. array[2] = "Alligator";
  86. array[3] = "Watermelon";
  87. array[4] = "Impinge";
  88. array[5] = "Strenuous";
  89. array[6] = "Condescending";
  90. array[7] = "Allocate";
  91. array[8] = "Caught in the Act";
  92. array[9] = "Elementary";
  93. array[10] = "Rocketship";
  94. array[11] = "Laminate";
  95. array[12] = "Exuberance";
  96. array[13] = "Vivacious";
  97. array[14] = "Dismemberment";
  98. array[15] = "Pat Sagack";
  99. array[16] = "Double Money";
  100. array[17] = "Watergate";
  101. array[18] = "Utopia";
  102. array[19] = "Indent";
  103. int i = 0;
  104.  
  105. if (array[0].equals (true))
  106. {
  107. JTextArea l = new JTextArea("YOU WON " + moneyFormat.format(total));
  108. l.setEditable(false);
  109.  
  110. }
  111.  
  112. while(i < array[generator1-1].length())
  113. {
  114. lines += "-";
  115. i++;
  116. }
  117.  
  118. JMenu helpMenu = new JMenu("Help");
  119. JMenu gameMenu = new JMenu("Game");
  120. JMenu quitMenu = new JMenu ("Quit");
  121. JMenuItem instructions = new JMenuItem("Instructions");
  122. JMenuItem copyright = new JMenuItem("Copyright");
  123. JMenuItem scores = new JMenuItem("Scores");
  124. JMenuItem newgame = new JMenuItem("New Game");
  125. JMenuItem quitgame = new JMenuItem("Quit Game");
  126. JMenuItem restartgame = new JMenuItem("Restart Game");
  127. panel = new JPanel(new BorderLayout());
  128. panel = new JPanel(new BorderLayout());
  129. panel = new JPanel(new BorderLayout());
  130. panel = new JPanel(new BorderLayout());
  131. gameMenu.add(newgame);
  132. gameMenu.add(scores);
  133. quitMenu.add(quitgame);
  134. quitMenu.add(restartgame);
  135. helpMenu.add(copyright);
  136. copyright.addActionListener(new helpButton1Action());
  137. scores.addActionListener(new scoreButtonAction());
  138. quitgame.addActionListener(new gameButton3Action());
  139. restartgame.addActionListener(new gameButton4Action());
  140. newgame.addActionListener(new gameButton2Action());
  141. menu.add(gameMenu);
  142. menu.add(quitMenu);
  143. menu.add(helpMenu);
  144. helpMenu.add(instructions);
  145. quitMenu.add(quitgame);
  146. instructions.addActionListener(new helpButtonAction());
  147. hangmanButton = new JButton("Start");
  148. panel.add(hangmanButton, BorderLayout.SOUTH);
  149. hangmanButton.addActionListener(new guessButtonAction());
  150. waterfall = new ImageIcon("C:/Program Files/800px-Lone_House.jpg");
  151. this.add(panel);
  152. pictureLabel = new JLabel(waterfall);
  153. this.add(pictureLabel);
  154. repaint();
  155.  
  156. }
  157.  
  158. private class gameButton4Action implements ActionListener
  159. {
  160.  
  161. public void actionPerformed(ActionEvent event){
  162.  
  163. JTextArea g = new JTextArea("\n Press OK to restart the game.\n");
  164. g.setEditable(false);
  165. JOptionPane.showMessageDialog(null, g, " Restart Game" , JOptionPane.PLAIN_MESSAGE);
  166.  
  167. //Next code sequence
  168. panel.setVisible(false);
  169. hangmanframe m = new hangmanframe();
  170. m.setSize(1280, 770);
  171.  
  172.  
  173. }
  174.  
  175. }
  176.  
  177.  
  178. private class hintButtonAction implements ActionListener
  179. {
  180. public void actionPerformed(ActionEvent event){
  181.  
  182.  
  183. hangman1 l = new hangman1();
  184.  
  185. }
  186.  
  187. }
  188.  
  189.  
  190. private class guessButtonAction implements ActionListener
  191. {
  192.  
  193. public void actionPerformed(ActionEvent event)
  194. {
  195.  
  196.  
  197.  
  198.  
  199. JTextArea g = new JTextArea("\n\tHang Man\n");
  200. g.setEditable(false);
  201. JOptionPane.showMessageDialog(null, g, " Hangman" , JOptionPane.PLAIN_MESSAGE);
  202.  
  203. JPanel gridPanel = new JPanel(new GridLayout(5,5));
  204. JPanel southPanel = new JPanel(new BorderLayout());
  205. JLabel hi = new JLabel(" ");
  206. gridPanel.add(hi, 0,0);
  207. waterfall = new ImageIcon("C:/Program Files/Hangman-0.png");
  208. JButton hintButton = new JButton ("Hints");
  209. JButton inputButton = new JButton("Input Letter");
  210. southPanel.add(inputButton, BorderLayout.SOUTH);
  211. hintButton.addActionListener(new hintButtonAction());
  212. inputButton.addActionListener(new inputButtonAction());
  213. southPanel.add(hintButton, BorderLayout.WEST);
  214. guessField = new JTextField(" ");
  215. guessField.setColumns(1);
  216. southPanel.add(guessField, BorderLayout.EAST);
  217. JLabel label1 = new JLabel(waterfall);
  218. gridPanel.add(label1, 1,0);
  219. JLabel line = new JLabel(" " + lines);
  220. line.setFont(font1);
  221. JLabel label2 = new JLabel(" Guess a Letter: " +array[generator1-1]);
  222. JLabel label3 = new JLabel(" Guess the Word" +array[generator1-1]);
  223. gridPanel.add(line, 1,1);
  224.  
  225. southPanel.add(label2, BorderLayout.CENTER);
  226. //southPanel.add(label3, BorderLayout.);
  227. panel.add(gridPanel, BorderLayout.CENTER);
  228. panel.add(southPanel, BorderLayout.NORTH);
  229. hangmanButton.setVisible(false);
  230. pictureLabel.setVisible(false);
  231.  
  232. validate();
  233.  
  234. repaint();
  235. myTextField p = new myTextField(20, 1);
  236. //turtlegraphics1 n = new turtlegraphics1();
  237. }
  238.  
  239. }
  240.  
  241. class Score extends JPanel{
  242.  
  243. public Score(){
  244.  
  245. //TextArea h = new TextArea ("Random Letter Now Inputted" +array[0]+ +array[1]+ +array[2]+ +array[3]+ +array[4]+ +array[5]);
  246.  
  247. }
  248.  
  249. }
  250.  
  251.  
  252.  
  253. private class inputButtonAction implements ActionListener
  254. {
  255.  
  256. public void actionPerformed (ActionEvent event)
  257. {
  258. int a;
  259. guess = guessField.getText();
  260. String userGuess = "i";
  261. String word = array[1];
  262. a = array[1].indexOf(guess);
  263.  
  264. JOptionPane.showMessageDialog(null, "" + guess + " " + array[generator1-1] + " "+ a);
  265.  
  266. //JOptionPane.showMessageDialog(null, "Letter now added");
  267.  
  268.  
  269.  
  270. //JOptionPane.showMessageDialog(null, "Can't input more than 1 letter at a time");
  271.  
  272.  
  273.  
  274. Score area = new Score();
  275.  
  276.  
  277. }
  278. }
  279.  
  280.  
  281. private class helpButtonAction implements ActionListener
  282. {
  283.  
  284. public void actionPerformed(ActionEvent event)
  285.  
  286. {
  287.  
  288. //panel.remove(hangmanButton);
  289.  
  290. 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");
  291. l.setEditable(false);
  292. JOptionPane.showMessageDialog(null, l, "Instructions for Hangman", JOptionPane.PLAIN_MESSAGE);
  293. JPanel helpPanel = new JPanel();
  294. helpPanel.setLayout (new BorderLayout());
  295. revalidate();
  296.  
  297.  
  298. }
  299.  
  300.  
  301.  
  302. }
  303. private class helpButton1Action implements ActionListener
  304. {
  305.  
  306. public void actionPerformed(ActionEvent event)
  307.  
  308. {
  309.  
  310. //panel.remove(hangmanButton);
  311.  
  312. 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");
  313. l.setEditable(false);
  314. JOptionPane.showMessageDialog(null, l, "©", JOptionPane.PLAIN_MESSAGE);
  315. JPanel helpPanel = new JPanel();
  316. helpPanel.setLayout (new BorderLayout());
  317. //URL urlClick = ("C:/Program Files/New Stories (Highway Blues).wma");
  318. //click = Applet.newAudioClip(urlClick);
  319. click.play();
  320. revalidate();
  321.  
  322.  
  323.  
  324. }
  325.  
  326.  
  327.  
  328. }
  329.  
  330. private class gameButton2Action implements ActionListener
  331. {
  332.  
  333. public void actionPerformed(ActionEvent event)
  334.  
  335. {
  336.  
  337.  
  338.  
  339. JTextArea l = new JTextArea("\n\tNew Game\n");
  340. l.setEditable(false);
  341. JOptionPane.showMessageDialog(null, l, " New Game", JOptionPane.PLAIN_MESSAGE);
  342. JPanel helpPanel = new JPanel();
  343. helpPanel.setLayout (new BorderLayout());
  344. revalidate();
  345.  
  346.  
  347. } }
  348.  
  349. private class gameButton3Action implements ActionListener
  350. {
  351.  
  352. public void actionPerformed(ActionEvent event)
  353.  
  354. {
  355.  
  356. System.exit(0);
  357.  
  358. }}
  359. private class scoreButtonAction implements ActionListener
  360. {
  361. JTextArea outputTextArea = new JTextArea();
  362. public void actionPerformed(ActionEvent event)
  363.  
  364. {
  365. JTextArea l = new JTextArea("\tCheck your previous scores here:\n\n \tRight: Wrong: " +array[0].equals(true)+ array[0].equals(false));
  366. JOptionPane.showMessageDialog(null, l, " Scores" , JOptionPane.PLAIN_MESSAGE);
  367. l.setEditable(false);
  368.  
  369. }
  370.  
  371.  
  372.  
  373. }
  374.  
  375.  
  376. class hangman1{
  377.  
  378.  
  379. JTextArea outputTextArea = new JTextArea();
  380. public hangman1(){
  381. outputTextArea.setEditable(false);
  382. if (generator1-1 == 0){
  383. outputTextArea.setText("This word is a form of dictatorship");
  384. JOptionPane.showMessageDialog(null, outputTextArea, " Hint\t\t", JOptionPane.PLAIN_MESSAGE);
  385. }
  386. if (generator1-1 == 1){
  387. outputTextArea.setText("An unfair Monarchy overall");
  388. JOptionPane.showMessageDialog(null, outputTextArea, " Hint\t\t", JOptionPane.PLAIN_MESSAGE);
  389. }
  390. if (generator1-1 == 2){
  391. outputTextArea.setText("The SYNONYM is Gator; for Alligator");
  392. JOptionPane.showMessageDialog(null, outputTextArea, " Hint\t\t", JOptionPane.PLAIN_MESSAGE);
  393. }
  394. if (generator1-1 == 3){
  395. outputTextArea.setText("This word is a type of a fruit");
  396. JOptionPane.showMessageDialog(null, outputTextArea, " Hint\t\t", JOptionPane.PLAIN_MESSAGE);
  397. }
  398. if (generator1-1 == 4){
  399. outputTextArea.setText("The SYNONYM is invade; for impinge");
  400. JOptionPane.showMessageDialog(null, outputTextArea, " Hint\t\t", JOptionPane.PLAIN_MESSAGE);
  401. }
  402. if (generator1-1 == 5){
  403. outputTextArea.setText("The SYNONYM is laborious; for strenuous");
  404.  
  405. outputTextArea.setText("The SYNONYM is depress; for indent");
  406. JOptionPane.showMessageDialog(null, outputTextArea, " Hint\t\t", JOptionPane.PLAIN_MESSAGE);
  407. }
  408. if (generator1-1 == 6){
  409. outputTextArea.setText("The SYNONYM is disdainful; for condescending");
  410. outputTextArea.setText("The SNYONYM is dreamland; for utopia");
  411. JOptionPane.showMessageDialog(null, outputTextArea, " Hint\t\t", JOptionPane.PLAIN_MESSAGE);
  412. }
  413. if (generator1-1 == 7){
  414. outputTextArea.setText("This word is a form of changing an object");
  415. outputTextArea.setText("The SYNONYM is energetic; for vivacious");
  416. JOptionPane.showMessageDialog(null, outputTextArea, " Hint\t\t", JOptionPane.PLAIN_MESSAGE);
  417. }
  418. if (generator1-1 == 8){
  419. outputTextArea.setText("This word is an unholy action");
  420.  
  421. JOptionPane.showMessageDialog(null, outputTextArea, " Hint\t\t", JOptionPane.PLAIN_MESSAGE);
  422. }
  423. if (generator1-1 == 9){
  424. outputTextArea.setText("The SYNONYM is fundamental; for Elementary");
  425. JOptionPane.showMessageDialog(null, outputTextArea, " Hint\t\t", JOptionPane.PLAIN_MESSAGE);
  426. }
  427. if (generator1-1 == 10){
  428. outputTextArea.setText("The frist letter of the word is R; for Rocket Ship");
  429. outputTextArea.setText("The SYNONYM is raider; for marauder");
  430. JOptionPane.showMessageDialog(null, outputTextArea, " Hint\t\t", JOptionPane.PLAIN_MESSAGE);
  431. }
  432. if (generator1-1 == 10){
  433. outputTextArea.setText("The SYNONYM is night; for midnight");
  434. JOptionPane.showMessageDialog(null, outputTextArea, " Hint\t\t", JOptionPane.PLAIN_MESSAGE);
  435. }
  436. if (generator1-1 == 11){
  437. outputTextArea.setText("The SNYONYM is enclose; for laminate");
  438. JOptionPane.showMessageDialog(null, outputTextArea, " Hint\t\t", JOptionPane.PLAIN_MESSAGE);
  439. }
  440. if (generator1-1 == 12){
  441. outputTextArea.setText("This word is a type of outgoing happy emotion");
  442. JOptionPane.showMessageDialog(null, outputTextArea, " Hint\t\t", JOptionPane.PLAIN_MESSAGE);
  443. }
  444. if (generator1-1 == 13){
  445. outputTextArea.setText("This word is an extremity of happiness");
  446. JOptionPane.showMessageDialog(null, outputTextArea, " Hint\t\t", JOptionPane.PLAIN_MESSAGE);
  447. }
  448. if (generator1-1 == 14){
  449. outputTextArea.setText("This word is close, to the affects of amnesia");
  450. JOptionPane.showMessageDialog(null, outputTextArea, " Hint\t\t", JOptionPane.PLAIN_MESSAGE);
  451. }}
  452.  
  453.  
  454. }
  455.  
  456. class myTextField extends JTextField{
  457.  
  458. int limit;
  459.  
  460. public myTextField(int show, int limit){
  461.  
  462. super(show);
  463.  
  464. this.limit = limit;
  465.  
  466. addKeyListener(new KeyAdapter() {
  467. public void keyTyped(KeyEvent evt) {
  468. if (guessField.getText().length() >=myTextField.this.limit)
  469. evt.consume();}});
  470.  
  471. }
  472. }
  473.  
  474.  
  475.  
  476. String guess;
  477. JTextField guessField;
  478. JLabel pictureLabel;
  479. int total=100;
  480. int generator1;
  481. String lines = "";
  482. ImageIcon waterfall;
  483. JButton hangmanButton;
  484. JPanel panel;
  485. String array[];
  486. AudioClip click;
  487. Font font1;
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 22
Reputation: StuartMillner is an unknown quantity at this point 
Solved Threads: 5
StuartMillner StuartMillner is offline Offline
Newbie Poster

Re: problem with indexOf() and contains()

 
0
  #2
May 21st, 2009
Have you tried debugging? Maybe the variable isn't equal to the right thing (or anything) at the time you're trying to check it. If there is a problem between entering a word in a JTextBox and then storing that information in a variable then there are a few common logic errors that may be interfering:

1.) You may be using the wrong function for the JTextBox object.

2.) You may have a scope issue. If there is another variable that has the same name at a different "scope" or visibility the variable might be mis-assigned.

3.) The variable is being overwritten or the code that assigns the variable is never being called.

I would check those three things out, and if you still can't figure it out debug! Most IDEs have a debugging feature, which takes you line by line and displays the information about all the variables in the program.

*It would also help if you put all the code in between the [ code=java ] and [ /code ] tags, so that it's easier to read.
Last edited by StuartMillner; May 21st, 2009 at 2:55 am.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 108
Reputation: christiangirl is an unknown quantity at this point 
Solved Threads: 1
christiangirl christiangirl is offline Offline
Junior Poster

Re: problem with indexOf() and contains()

 
0
  #3
May 21st, 2009
What the user enters in the JTextField is stored to the variable fine, I out put that variable just a couple of lines later. But when I test it in the indexOf() or contains() methods, it does not work.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 1,568
Reputation: BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all 
Solved Threads: 196
BestJewSinceJC BestJewSinceJC is offline Offline
Posting Virtuoso

Re: problem with indexOf() and contains()

 
0
  #4
May 21st, 2009
http://java.sun.com/javase/6/docs/ap...ng/String.html

What do you think indexOf does? I can't really clear up the problem without knowing what you expect it to do and what it is doing.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 108
Reputation: christiangirl is an unknown quantity at this point 
Solved Threads: 1
christiangirl christiangirl is offline Offline
Junior Poster

Re: problem with indexOf() and contains()

 
0
  #5
May 21st, 2009
I expect it to return where in the word the letter is.
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 22
Reputation: StuartMillner is an unknown quantity at this point 
Solved Threads: 5
StuartMillner StuartMillner is offline Offline
Newbie Poster

Re: problem with indexOf() and contains()

 
0
  #6
May 21st, 2009
It always returns a -1, correct? If the number it's returning is not a -1 then it's getting a value.

  1. //Hangman a game where you test your word-guessing skills against the worlds smartest computer simulation.
  2. import java.awt.*;
  3. import java.awt.event.*;
  4. import javax.swing.*;
  5. import java.util.Locale;
  6. import java.text.*;
  7. import java.awt.AWTEvent;
  8. import javax.swing.plaf.metal.*;
  9. import java.util.Locale;
  10. import java.util.Random;
  11. import java.applet.*;
  12. import java.net.URL;
  13. import java.awt.font.*;
  14. import java.lang.String;
  15.  
  16.  
  17.  
  18. public class hangman {
  19.  
  20. public static void main(String[] args)
  21. {
  22.  
  23.  
  24. EventQueue.invokeLater
  25. (
  26. new Runnable()
  27. {
  28.  
  29. public void run()
  30. {
  31.  
  32. hangmanframe.setDefaultLookAndFeelDecorated(true);
  33. hangmanframe frame = new hangmanframe();
  34. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  35. frame.setVisible(true);
  36. frame.setSize(1280, 770);
  37.  
  38.  
  39.  
  40.  
  41. //UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
  42.  
  43.  
  44. }});
  45. }
  46.  
  47. }
  48.  
  49.  
  50. class hangmanframe extends JFrame
  51. {
  52.  
  53. public hangmanframe()
  54. {
  55. hangmanframe.setDefaultLookAndFeelDecorated(true);
  56. this.setSize(1280, 770);
  57. this.setTitle("Hangman");
  58.  
  59. JMenuBar greenMenuBar = new JMenuBar();
  60. greenMenuBar.setOpaque(true);
  61. greenMenuBar.setBackground(new Color(154, 165, 127));
  62. greenMenuBar.setPreferredSize(new Dimension(200, 20));
  63. JLabel blueLabel = new JLabel();
  64. blueLabel.setPreferredSize(new Dimension(200, 180));
  65. this.setVisible(true);
  66. this.setJMenuBar(greenMenuBar);
  67. this.getContentPane().add(blueLabel, BorderLayout.CENTER);
  68. this.pack();
  69. GUI panel = new GUI(greenMenuBar);
  70. this.add(panel);
  71.  
  72. }
  73. }
  74. class GUI extends JPanel
  75. {
  76. public GUI(JMenuBar menu)
  77. {
  78. font1 = new Font("Book Antiqua", Font.PLAIN, 50);
  79. Random generator = new Random();
  80. generator1 = generator.nextInt(20)+1;
  81. NumberFormat moneyFormat = NumberFormat.getCurrencyInstance(Locale.US);
  82. array = new String[20];
  83. array[0] = "Fascist";
  84. array[1] = "Dictator";
  85. array[2] = "Alligator";
  86. array[3] = "Watermelon";
  87. array[4] = "Impinge";
  88. array[5] = "Strenuous";
  89. array[6] = "Condescending";
  90. array[7] = "Allocate";
  91. array[8] = "Caught in the Act";
  92. array[9] = "Elementary";
  93. array[10] = "Rocketship";
  94. array[11] = "Laminate";
  95. array[12] = "Exuberance";
  96. array[13] = "Vivacious";
  97. array[14] = "Dismemberment";
  98. array[15] = "Pat Sagack";
  99. array[16] = "Double Money";
  100. array[17] = "Watergate";
  101. array[18] = "Utopia";
  102. array[19] = "Indent";
  103. int i = 0;
  104.  
  105. if (array[0].equals (true))
  106. {
  107. JTextArea l = new JTextArea("YOU WON " + moneyFormat.format(total));
  108. l.setEditable(false);
  109.  
  110. }
  111.  
  112. while(i < array[generator1-1].length())
  113. {
  114. lines += "-";
  115. i++;
  116. }
  117.  
  118. JMenu helpMenu = new JMenu("Help");
  119. JMenu gameMenu = new JMenu("Game");
  120. JMenu quitMenu = new JMenu ("Quit");
  121. JMenuItem instructions = new JMenuItem("Instructions");
  122. JMenuItem copyright = new JMenuItem("Copyright");
  123. JMenuItem scores = new JMenuItem("Scores");
  124. JMenuItem newgame = new JMenuItem("New Game");
  125. JMenuItem quitgame = new JMenuItem("Quit Game");
  126. JMenuItem restartgame = new JMenuItem("Restart Game");
  127. panel = new JPanel(new BorderLayout());
  128. panel = new JPanel(new BorderLayout());
  129. panel = new JPanel(new BorderLayout());
  130. panel = new JPanel(new BorderLayout());
  131. gameMenu.add(newgame);
  132. gameMenu.add(scores);
  133. quitMenu.add(quitgame);
  134. quitMenu.add(restartgame);
  135. helpMenu.add(copyright);
  136. copyright.addActionListener(new helpButton1Action());
  137. scores.addActionListener(new scoreButtonAction());
  138. quitgame.addActionListener(new gameButton3Action());
  139. restartgame.addActionListener(new gameButton4Action());
  140. newgame.addActionListener(new gameButton2Action());
  141. menu.add(gameMenu);
  142. menu.add(quitMenu);
  143. menu.add(helpMenu);
  144. helpMenu.add(instructions);
  145. quitMenu.add(quitgame);
  146. instructions.addActionListener(new helpButtonAction());
  147. hangmanButton = new JButton("Start");
  148. panel.add(hangmanButton, BorderLayout.SOUTH);
  149. hangmanButton.addActionListener(new guessButtonAction());
  150. waterfall = new ImageIcon("C:/Program Files/800px-Lone_House.jpg");
  151. this.add(panel);
  152. pictureLabel = new JLabel(waterfall);
  153. this.add(pictureLabel);
  154. repaint();
  155.  
  156. }
  157.  
  158. private class gameButton4Action implements ActionListener
  159. {
  160.  
  161. public void actionPerformed(ActionEvent event){
  162.  
  163. JTextArea g = new JTextArea("\n Press OK to restart the game.\n");
  164. g.setEditable(false);
  165. JOptionPane.showMessageDialog(null, g, " Restart Game" , JOptionPane.PLAIN_MESSAGE);
  166.  
  167. //Next code sequence
  168. panel.setVisible(false);
  169. hangmanframe m = new hangmanframe();
  170. m.setSize(1280, 770);
  171.  
  172.  
  173. }
  174.  
  175. }
  176.  
  177.  
  178. private class hintButtonAction implements ActionListener
  179. {
  180. public void actionPerformed(ActionEvent event){
  181.  
  182.  
  183. hangman1 l = new hangman1();
  184.  
  185. }
  186.  
  187. }
  188.  
  189.  
  190. private class guessButtonAction implements ActionListener
  191. {
  192.  
  193. public void actionPerformed(ActionEvent event)
  194. {
  195.  
  196.  
  197.  
  198.  
  199. JTextArea g = new JTextArea("\n\tHang Man\n");
  200. g.setEditable(false);
  201. JOptionPane.showMessageDialog(null, g, " Hangman" , JOptionPane.PLAIN_MESSAGE);
  202.  
  203. JPanel gridPanel = new JPanel(new GridLayout(5,5));
  204. JPanel southPanel = new JPanel(new BorderLayout());
  205. JLabel hi = new JLabel(" ");
  206. gridPanel.add(hi, 0,0);
  207. waterfall = new ImageIcon("C:/Program Files/Hangman-0.png");
  208. JButton hintButton = new JButton ("Hints");
  209. JButton inputButton = new JButton("Input Letter");
  210. southPanel.add(inputButton, BorderLayout.SOUTH);
  211. hintButton.addActionListener(new hintButtonAction());
  212. inputButton.addActionListener(new inputButtonAction());
  213. southPanel.add(hintButton, BorderLayout.WEST);
  214. guessField = new JTextField(" ");
  215. guessField.setColumns(1);
  216. southPanel.add(guessField, BorderLayout.EAST);
  217. JLabel label1 = new JLabel(waterfall);
  218. gridPanel.add(label1, 1,0);
  219. JLabel line = new JLabel(" " + lines);
  220. line.setFont(font1);
  221. JLabel label2 = new JLabel(" Guess a Letter: " +array[generator1-1]);
  222. JLabel label3 = new JLabel(" Guess the Word" +array[generator1-1]);
  223. gridPanel.add(line, 1,1);
  224.  
  225. southPanel.add(label2, BorderLayout.CENTER);
  226. //southPanel.add(label3, BorderLayout.);
  227. panel.add(gridPanel, BorderLayout.CENTER);
  228. panel.add(southPanel, BorderLayout.NORTH);
  229. hangmanButton.setVisible(false);
  230. pictureLabel.setVisible(false);
  231.  
  232. validate();
  233.  
  234. repaint();
  235. myTextField p = new myTextField(20, 1);
  236. //turtlegraphics1 n = new turtlegraphics1();
  237. }
  238.  
  239. }
  240.  
  241. class Score extends JPanel{
  242.  
  243. public Score(){
  244.  
  245. //TextArea h = new TextArea ("Random Letter Now Inputted" +array[0]+ +array[1]+ +array[2]+ +array[3]+ +array[4]+ +array[5]);
  246.  
  247. }
  248.  
  249. }
  250.  
  251.  
  252.  
  253. private class inputButtonAction implements ActionListener
  254. {
  255.  
  256. public void actionPerformed (ActionEvent event)
  257. {
  258. int a;
  259. guess = guessField.getText();
  260. String userGuess = "i";
  261. String word = array[1];
  262. a = array[1].indexOf(guess);
  263.  
  264. JOptionPane.showMessageDialog(null, "" + guess + " " + array[generator1-1] + " "+ a);
  265.  
  266. //JOptionPane.showMessageDialog(null, "Letter now added");
  267.  
  268.  
  269.  
  270. //JOptionPane.showMessageDialog(null, "Can't input more than 1 letter at a time");
  271.  
  272.  
  273.  
  274. Score area = new Score();
  275.  
  276.  
  277. }
  278. }
  279.  
  280.  
  281. private class helpButtonAction implements ActionListener
  282. {
  283.  
  284. public void actionPerformed(ActionEvent event)
  285.  
  286. {
  287.  
  288. //panel.remove(hangmanButton);
  289.  
  290. 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");
  291. l.setEditable(false);
  292. JOptionPane.showMessageDialog(null, l, "Instructions for Hangman", JOptionPane.PLAIN_MESSAGE);
  293. JPanel helpPanel = new JPanel();
  294. helpPanel.setLayout (new BorderLayout());
  295. revalidate();
  296.  
  297.  
  298. }
  299.  
  300.  
  301.  
  302. }
  303. private class helpButton1Action implements ActionListener
  304. {
  305.  
  306. public void actionPerformed(ActionEvent event)
  307.  
  308. {
  309.  
  310. //panel.remove(hangmanButton);
  311.  
  312. 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");
  313. l.setEditable(false);
  314. JOptionPane.showMessageDialog(null, l, "©", JOptionPane.PLAIN_MESSAGE);
  315. JPanel helpPanel = new JPanel();
  316. helpPanel.setLayout (new BorderLayout());
  317. //URL urlClick = ("C:/Program Files/New Stories (Highway Blues).wma");
  318. //click = Applet.newAudioClip(urlClick);
  319. click.play();
  320. revalidate();
  321.  
  322.  
  323.  
  324. }
  325.  
  326.  
  327.  
  328. }
  329.  
  330. private class gameButton2Action implements ActionListener
  331. {
  332.  
  333. public void actionPerformed(ActionEvent event)
  334.  
  335. {
  336.  
  337.  
  338.  
  339. JTextArea l = new JTextArea("\n\tNew Game\n");
  340. l.setEditable(false);
  341. JOptionPane.showMessageDialog(null, l, " New Game", JOptionPane.PLAIN_MESSAGE);
  342. JPanel helpPanel = new JPanel();
  343. helpPanel.setLayout (new BorderLayout());
  344. revalidate();
  345.  
  346.  
  347. } }
  348.  
  349. private class gameButton3Action implements ActionListener
  350. {
  351.  
  352. public void actionPerformed(ActionEvent event)
  353.  
  354. {
  355.  
  356. System.exit(0);
  357.  
  358. }}
  359. private class scoreButtonAction implements ActionListener
  360. {
  361. JTextArea outputTextArea = new JTextArea();
  362. public void actionPerformed(ActionEvent event)
  363.  
  364. {
  365. JTextArea l = new JTextArea("\tCheck your previous scores here:\n\n \tRight: Wrong: " +array[0].equals(true)+ array[0].equals(false));
  366. JOptionPane.showMessageDialog(null, l, " Scores" , JOptionPane.PLAIN_MESSAGE);
  367. l.setEditable(false);
  368.  
  369. }
  370.  
  371.  
  372.  
  373. }
  374.  
  375.  
  376. class hangman1{
  377.  
  378.  
  379. JTextArea outputTextArea = new JTextArea();
  380. public hangman1(){
  381. outputTextArea.setEditable(false);
  382. if (generator1-1 == 0){
  383. outputTextArea.setText("This word is a form of dictatorship");
  384. JOptionPane.showMessageDialog(null, outputTextArea, " Hint\t\t", JOptionPane.PLAIN_MESSAGE);
  385. }
  386. if (generator1-1 == 1){
  387. outputTextArea.setText("An unfair Monarchy overall");
  388. JOptionPane.showMessageDialog(null, outputTextArea, " Hint\t\t", JOptionPane.PLAIN_MESSAGE);
  389. }
  390. if (generator1-1 == 2){
  391. outputTextArea.setText("The SYNONYM is Gator; for Alligator");
  392. JOptionPane.showMessageDialog(null, outputTextArea, " Hint\t\t", JOptionPane.PLAIN_MESSAGE);
  393. }
  394. if (generator1-1 == 3){
  395. outputTextArea.setText("This word is a type of a fruit");
  396. JOptionPane.showMessageDialog(null, outputTextArea, " Hint\t\t", JOptionPane.PLAIN_MESSAGE);
  397. }
  398. if (generator1-1 == 4){
  399. outputTextArea.setText("The SYNONYM is invade; for impinge");
  400. JOptionPane.showMessageDialog(null, outputTextArea, " Hint\t\t", JOptionPane.PLAIN_MESSAGE);
  401. }
  402. if (generator1-1 == 5){
  403. outputTextArea.setText("The SYNONYM is laborious; for strenuous");
  404.  
  405. outputTextArea.setText("The SYNONYM is depress; for indent");
  406. JOptionPane.showMessageDialog(null, outputTextArea, " Hint\t\t", JOptionPane.PLAIN_MESSAGE);
  407. }
  408. if (generator1-1 == 6){
  409. outputTextArea.setText("The SYNONYM is disdainful; for condescending");
  410. outputTextArea.setText("The SNYONYM is dreamland; for utopia");
  411. JOptionPane.showMessageDialog(null, outputTextArea, " Hint\t\t", JOptionPane.PLAIN_MESSAGE);
  412. }
  413. if (generator1-1 == 7){
  414. outputTextArea.setText("This word is a form of changing an object");
  415. outputTextArea.setText("The SYNONYM is energetic; for vivacious");
  416. JOptionPane.showMessageDialog(null, outputTextArea, " Hint\t\t", JOptionPane.PLAIN_MESSAGE);
  417. }
  418. if (generator1-1 == 8){
  419. outputTextArea.setText("This word is an unholy action");
  420.  
  421. JOptionPane.showMessageDialog(null, outputTextArea, " Hint\t\t", JOptionPane.PLAIN_MESSAGE);
  422. }
  423. if (generator1-1 == 9){
  424. outputTextArea.setText("The SYNONYM is fundamental; for Elementary");
  425. JOptionPane.showMessageDialog(null, outputTextArea, " Hint\t\t", JOptionPane.PLAIN_MESSAGE);
  426. }
  427. if (generator1-1 == 10){
  428. outputTextArea.setText("The frist letter of the word is R; for Rocket Ship");
  429. outputTextArea.setText("The SYNONYM is raider; for marauder");
  430. JOptionPane.showMessageDialog(null, outputTextArea, " Hint\t\t", JOptionPane.PLAIN_MESSAGE);
  431. }
  432. if (generator1-1 == 10){
  433. outputTextArea.setText("The SYNONYM is night; for midnight");
  434. JOptionPane.showMessageDialog(null, outputTextArea, " Hint\t\t", JOptionPane.PLAIN_MESSAGE);
  435. }
  436. if (generator1-1 == 11){
  437. outputTextArea.setText("The SNYONYM is enclose; for laminate");
  438. JOptionPane.showMessageDialog(null, outputTextArea, " Hint\t\t", JOptionPane.PLAIN_MESSAGE);
  439. }
  440. if (generator1-1 == 12){
  441. outputTextArea.setText("This word is a type of outgoing happy emotion");
  442. JOptionPane.showMessageDialog(null, outputTextArea, " Hint\t\t", JOptionPane.PLAIN_MESSAGE);
  443. }
  444. if (generator1-1 == 13){
  445. outputTextArea.setText("This word is an extremity of happiness");
  446. JOptionPane.showMessageDialog(null, outputTextArea, " Hint\t\t", JOptionPane.PLAIN_MESSAGE);
  447. }
  448. if (generator1-1 == 14){
  449. outputTextArea.setText("This word is close, to the affects of amnesia");
  450. JOptionPane.showMessageDialog(null, outputTextArea, " Hint\t\t", JOptionPane.PLAIN_MESSAGE);
  451. }}
  452.  
  453.  
  454. }
  455.  
  456. class myTextField extends JTextField{
  457.  
  458. int limit;
  459.  
  460. public myTextField(int show, int limit){
  461.  
  462. super(show);
  463.  
  464. this.limit = limit;
  465.  
  466. addKeyListener(new KeyAdapter() {
  467. public void keyTyped(KeyEvent evt) {
  468. if (guessField.getText().length() >=myTextField.this.limit)
  469. evt.consume();}});
  470.  
  471. }
  472. }
  473.  
  474.  
  475.  
  476. String guess;
  477. JTextField guessField;
  478. JLabel pictureLabel;
  479. int total=100;
  480. int generator1;
  481. String lines = "";
  482. ImageIcon waterfall;
  483. JButton hangmanButton;
  484. JPanel panel;
  485. String array[];
  486. AudioClip click;
  487. Font font1;

on which line do you get this error? I'm trying to find which function gos to the "input letter" button, but there are so many functions named GameButton#Action that it's hard to figure it out.
Last edited by StuartMillner; May 21st, 2009 at 10:57 pm.
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,438
Reputation: Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of 
Solved Threads: 510
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: problem with indexOf() and contains()

 
0
  #7
May 21st, 2009
I would just point out that you are checking the entry against the string in array[1] every single time, but showing a value based on your generator array[generator1-1] in the JOptionPane
  1. a = array[1].indexOf(guess);
  2. JOptionPane.showMessageDialog(null, "" + guess + " " + array[generator1-1] + " "+ a);
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 22
Reputation: StuartMillner is an unknown quantity at this point 
Solved Threads: 5
StuartMillner StuartMillner is offline Offline
Newbie Poster

Re: problem with indexOf() and contains()

 
0
  #8
May 21st, 2009
I figured out your problem. The variable "guess" is where the user input is stored. If the user typed in a d guess looks like this:

" d".

It will never find that in the code because there aren't that many spaces in a row in any of your words! Use the .trim() function on line 262 where you have the following code:

a = array[1].indexOf(guess);


The toLowerCase part so that it always compares to a lowercase value so that capitalization doesn't matter.

Also, the word will always be the second entry, or dictator. To fix this, enter generator1-1 where it says 1 in array[1].

Your code should look like this:

int a = 0;
guess = guessField.getText().trim();
String userGuess = "i";
String word = array[generator1 -1].toLowerCase();
a = word.indexOf(guess.trim());

JOptionPane.showMessageDialog(null, "" + guess + " " + array[generator1-1] + " "+ a);

the trim() to get rid of all the spacing, replacing the 1 with generator1-1 and adding the .tolowercase().
Last edited by StuartMillner; May 21st, 2009 at 11:26 pm.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 2,612
Reputation: adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of 
Solved Threads: 463
Moderator
adatapost's Avatar
adatapost adatapost is offline Offline
Posting Maven

Re: problem with indexOf() and contains()

 
0
  #9
May 22nd, 2009
Dear,

Decide first, whether you want to search a string from an array of string or a string from another string.
Failure is not fatal, but failure to change might be. - John Wooden
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC