Results 1 to 18 of 18
- 12-09-2012, 08:14 PM #1
Member
- Join Date
- Dec 2012
- Posts
- 8
- Rep Power
- 0
Need help with creating a "Hangman" game
I am currently in my first semester of my first year at uni and I'm trying to make a very simple Hangman game. The program worked fine until I added a new button to my form. As soon as I added the new button (hangImage.JButton) my whole code stopped working instantly and when I try to run I get the error:
I am also referred to this:Java Code:Exception in thread "AWT-EventQueue-0" java.lang.RuntimeException: Uncompilable source code - illegal start of expression at hangman.HangForm.tryLetterActionPerformed(HangForm.java:185) at hangman.HangForm.access$000(HangForm.java:3) at hangman.HangForm$1.actionPerformed(HangForm.java:53) at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018) at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341) at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402) at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259) at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252) at java.awt.Component.processMouseEvent(Component.java:6505) at javax.swing.JComponent.processMouseEvent(JComponent.java:3321) at java.awt.Component.processEvent(Component.java:6270) at java.awt.Container.processEvent(Container.java:2229) at java.awt.Component.dispatchEventImpl(Component.java:4861) at java.awt.Container.dispatchEventImpl(Container.java:2287) at java.awt.Component.dispatchEvent(Component.java:4687) at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832) at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492) at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422) at java.awt.Container.dispatchEventImpl(Container.java:2273) at java.awt.Window.dispatchEventImpl(Window.java:2719) at java.awt.Component.dispatchEvent(Component.java:4687) at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:723) at java.awt.EventQueue.access$200(EventQueue.java:103) at java.awt.EventQueue$3.run(EventQueue.java:682) at java.awt.EventQueue$3.run(EventQueue.java:680) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76) at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87) at java.awt.EventQueue$4.run(EventQueue.java:696) at java.awt.EventQueue$4.run(EventQueue.java:694) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76) at java.awt.EventQueue.dispatchEvent(EventQueue.java:693) at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:244) at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:163) at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:151) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:147) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:139) at java.awt.EventDispatchThread.run(EventDispatchThread.java:97)However I have no idea what this means.Java Code:C:\Users\Yusuf\.netbeans\6.8\var\cache\executor-snippets\run.xml:45:
The code for my program is below. Any help would be greatly appreciated, thanks.
Java Code:package hangman; public class HangForm extends javax.swing.JFrame { String FindWord = "hello"; /** Creates new form HangForm */ public HangForm() { initComponents(); } @SuppressWarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="Generated Code"> private void initComponents() { private void tryLetterActionPerformed(java.awt.event.ActionEvent evt) { int charPos = 2; String letter = aLetter.getText(); charPos = FindWord.indexOf(letter, charPos + 1); if (charPos == 0) { char0.setText(letter); } if (charPos == 1) { char1.setText(letter); } if (charPos == 2) { char2.setText(letter); } if (charPos == 3) { char3.setText(letter); } if (charPos == 4) { char4.setText(letter); } if (charPos == 5) { char5.setText(letter); } myMessage.setText("Position is " + charPos); charPos = FindWord.indexOf(letter); myMessage.setText("Position is " + charPos); if (charPos == 0) { char0.setText(letter); } if (charPos == 1) { char1.setText(letter); } if (charPos == 2) { char2.setText(letter); } if (charPos == 3) { char3.setText(letter); } if (charPos == 4) { char4.setText(letter); } if (charPos == 5) { char5.setText(letter); } myMessage.setText("Position is " + charPos); if (charPos == -1) { int wrong = Integer.parseInt(incorrect.getText()); wrong = wrong + 1; String ss = Integer.toString(wrong); incorrect.setText(ss); myMessage.setText("Wrong"); if (wrong > 8) { String gg = Integer.toString(wrong); myMessage.setText("GAME OVER"); aLetter.setVisible(false); } } private void hangImageActionPerformed(java.awt.event.ActionEvent evt) { int pic = Integer.parseInt(incorrect.getText()); if (pic == 1) { hangImage.setIcon(new javax.swing.ImageIcon("C:\Users\Yusuf\Desktop\HangMan\img1.JPG")); } if (pic == 2) { hangImage.setIcon(new javax.swing.ImageIcon("C:\Users\Yusuf\Desktop\HangMan\img2.JPG")); } if (pic == 3) { hangImage.setIcon(new javax.swing.ImageIcon("C:\Users\Yusuf\Desktop\HangMan\img3.JPG")); } if (pic == 4) { hangImage.setIcon(new javax.swing.ImageIcon("C:\Users\Yusuf\Desktop\HangMan\img4.JPG")); } if (pic == 5) { hangImage.setIcon(new javax.swing.ImageIcon("C:\Users\Yusuf\Desktop\HangMan\img5.JPG")); } if (pic == 6) { hangImage.setIcon(new javax.swing.ImageIcon("C:\Users\Yusuf\Desktop\HangMan\img6.JPG")); } if (pic == 7) { hangImage.setIcon(new javax.swing.ImageIcon("C:\Users\Yusuf\Desktop\HangMan\img7.JPG")); } if (pic == 8) { hangImage.setIcon(new javax.swing.ImageIcon("C:\Users\Yusuf\Desktop\HangMan\img8.JPG")); } if (pic == 9) { hangImage.setIcon(new javax.swing.ImageIcon("C:\Users\Yusuf\Desktop\HangMan\img9.JPG")); } } /** * @param args the command line arguments */ public static void main(String args[]) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new HangForm().setVisible(true); } } } // Variables declaration - do not modify private javax.swing.JTextField aLetter; private javax.swing.JLabel char0; private javax.swing.JLabel char1; private javax.swing.JLabel char2; private javax.swing.JLabel char3; private javax.swing.JLabel char4; private javax.swing.JLabel char5; private javax.swing.JButton hangImage; private javax.swing.JLabel incorrect; private javax.swing.JLabel myMessage; private javax.swing.JButton tryLetter; // End of variables declaration }
- 12-09-2012, 08:17 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,385
- Blog Entries
- 7
- Rep Power
- 17
Re: Need help with creating a "Hangman" game
Does the phrase "uncompilable source code" ring a bell? Maybe, just maybe, your source couldn't be compiled ...
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
-
Re: Need help with creating a "Hangman" game
Yep, and instead of trying to run uncompilable code, check the compiler error messages and fix what it tells you to fix.
- 12-09-2012, 08:26 PM #4
Member
- Join Date
- Dec 2012
- Posts
- 8
- Rep Power
- 0
Re: Need help with creating a "Hangman" game
The problem is I wasn't getting any of these problems until I simply added one button to my form, I wasn't getting a single error message. As soon as I added the button, before even adding any code behind the button, I started receiving these errors. If I was to remove the button now my program would perfectly as it doesn't change any of my other code. Here is the button code for reference:
Java Code:private void hangImageActionPerformed(java.awt.event.ActionEvent evt) { int pic = Integer.parseInt(incorrect.getText()); if (pic == 1) { hangImage.setIcon(new javax.swing.ImageIcon("C:\Users\Yusuf\Desktop\HangMan\img1.JPG")); } if (pic == 2) { hangImage.setIcon(new javax.swing.ImageIcon("C:\Users\Yusuf\Desktop\HangMan\img2.JPG")); } if (pic == 3) { hangImage.setIcon(new javax.swing.ImageIcon("C:\Users\Yusuf\Desktop\HangMan\img3.JPG")); } if (pic == 4) { hangImage.setIcon(new javax.swing.ImageIcon("C:\Users\Yusuf\Desktop\HangMan\img4.JPG")); } if (pic == 5) { hangImage.setIcon(new javax.swing.ImageIcon("C:\Users\Yusuf\Desktop\HangMan\img5.JPG")); } if (pic == 6) { hangImage.setIcon(new javax.swing.ImageIcon("C:\Users\Yusuf\Desktop\HangMan\img6.JPG")); } if (pic == 7) { hangImage.setIcon(new javax.swing.ImageIcon("C:\Users\Yusuf\Desktop\HangMan\img7.JPG")); } if (pic == 8) { hangImage.setIcon(new javax.swing.ImageIcon("C:\Users\Yusuf\Desktop\HangMan\img8.JPG")); } if (pic == 9) { hangImage.setIcon(new javax.swing.ImageIcon("C:\Users\Yusuf\Desktop\HangMan\img9.JPG")); }
-
Re: Need help with creating a "Hangman" game
Showing this code doesn't help us solve the problem. You must show the code that is causing the error, and show us in the code which line is causing the error. You're likely nesting a method inside of a method or doing something else which is illegal in Java.
- 12-09-2012, 09:02 PM #6
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,544
- Rep Power
- 11
Re: Need help with creating a "Hangman" game
It is really important that you get to see and respond to compiler messages. If NB won't let you do that you should compile your program from the command line. (I don't really use NB and I'm too lazy to look. Does anyone know if this "don't hurt me by pointing out the mistakes" feature can be turned off?)"C:\Users\Yusuf\Desktop\HangMan\img5.JPG"
I put the string literal you used into a program and tried to compile it. This is what happened:
Do you understand what the compiler means by saying that \U, \Y, \D etc are illegal escape characters?Java Code:C:\Users\Educator\Desktop>type Test.java public class Test { public static void main(String[] args) { System.out.printf("C:\Users\Yusuf\Desktop\HangMan\img5.JPG"); } } C:\Users\Educator\Desktop>javac Test.java Test.java:3: error: illegal escape character System.out.printf("C:\Users\Yusuf\Desktop\HangMan\img5.JPG"); ^ Test.java:3: error: illegal escape character System.out.printf("C:\Users\Yusuf\Desktop\HangMan\img5.JPG"); ^ Test.java:3: error: illegal escape character System.out.printf("C:\Users\Yusuf\Desktop\HangMan\img5.JPG"); ^ Test.java:3: error: illegal escape character System.out.printf("C:\Users\Yusuf\Desktop\HangMan\img5.JPG"); ^ Test.java:3: error: illegal escape character System.out.printf("C:\Users\Yusuf\Desktop\HangMan\img5.JPG"); ^ 5 errors C:\Users\Educator\Desktop>Last edited by pbrockway2; 12-09-2012 at 09:06 PM.
- 12-09-2012, 09:12 PM #7
Member
- Join Date
- Dec 2012
- Posts
- 8
- Rep Power
- 0
Re: Need help with creating a "Hangman" game
OK so I worked out what was wrong with my program and fixed it, I now have one last seemingly small problem that's bugging me now:
On line 223 I am receiving the error: ")" expected, I have tried to work out where this may need to go with no success.Java Code:package hangman; public class HangForm extends javax.swing.JFrame { String FindWord = "hello"; /** Creates new form HangForm */ public HangForm() { initComponents(); } @SuppressWarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="Generated Code"> private void initComponents() { char0 = new javax.swing.JLabel(); char1 = new javax.swing.JLabel(); char2 = new javax.swing.JLabel(); char3 = new javax.swing.JLabel(); char4 = new javax.swing.JLabel(); char5 = new javax.swing.JLabel(); aLetter = new javax.swing.JTextField(); incorrect = new javax.swing.JLabel(); myMessage = new javax.swing.JLabel(); tryLetter = new javax.swing.JButton(); hangImage = new javax.swing.JButton(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); setTitle("Hang Man"); char0.setText("_"); char1.setText("_"); char2.setText("_"); char3.setText("_"); char4.setText("_"); char5.setText("_"); aLetter.setMaximumSize(new java.awt.Dimension(1, 1)); incorrect.setText("0"); myMessage.setText("Look here"); tryLetter.setText("Try letter"); tryLetter.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { tryLetterActionPerformed(evt); } }); hangImage.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { hangImageActionPerformed(evt); } }); org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup() .add(91, 91, 91) .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) .add(layout.createSequentialGroup() .add(char0) .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED) .add(char1) .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED) .add(char2) .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED) .add(char3) .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED) .add(char4) .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED) .add(char5) .add(109, 109, 109) .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING) .add(layout.createSequentialGroup() .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 41, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE) .add(incorrect)) .add(org.jdesktop.layout.GroupLayout.LEADING, myMessage))) .add(layout.createSequentialGroup() .add(aLetter, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 22, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE) .add(18, 18, 18) .add(tryLetter))) .add(105, 105, 105) .add(hangImage, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 215, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE) .add(74, 74, 74)) ); layout.setVerticalGroup( layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) .add(layout.createSequentialGroup() .add(37, 37, 37) .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) .add(hangImage, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 245, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE) .add(layout.createSequentialGroup() .add(incorrect) .add(52, 52, 52) .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE) .add(myMessage) .add(char0) .add(char1) .add(char2) .add(char3) .add(char4) .add(char5)) .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 142, Short.MAX_VALUE) .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE) .add(aLetter, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE) .add(tryLetter)))) .addContainerGap(56, Short.MAX_VALUE)) ); pack(); }// </editor-fold> private void tryLetterActionPerformed(java.awt.event.ActionEvent evt) { int charPos = 2; String letter = aLetter.getText(); charPos = FindWord.indexOf(letter, charPos + 1); if (charPos == 0) { char0.setText(letter); } if (charPos == 1) { char1.setText(letter); } if (charPos == 2) { char2.setText(letter); } if (charPos == 3) { char3.setText(letter); } if (charPos == 4) { char4.setText(letter); } if (charPos == 5) { char5.setText(letter); } myMessage.setText("Position is " + charPos); charPos = FindWord.indexOf(letter); myMessage.setText("Position is " + charPos); if (charPos == 0) { char0.setText(letter); } if (charPos == 1) { char1.setText(letter); } if (charPos == 2) { char2.setText(letter); } if (charPos == 3) { char3.setText(letter); } if (charPos == 4) { char4.setText(letter); } if (charPos == 5) { char5.setText(letter); } myMessage.setText("Position is " + charPos); if (charPos == -1) { int wrong = Integer.parseInt(incorrect.getText()); wrong = wrong + 1; String ss = Integer.toString(wrong); incorrect.setText(ss); myMessage.setText("Wrong"); if (wrong > 8) { String gg = Integer.toString(wrong); myMessage.setText("GAME OVER"); aLetter.setVisible(false); } } } private void hangImageActionPerformed(java.awt.event.ActionEvent evt) { int pic = Integer.parseInt(incorrect.getText()); if (pic == 1) { hangImage.setIcon(new javax.swing.ImageIcon("img1.JPG")); } if (pic == 2) { hangImage.setIcon(new javax.swing.ImageIcon("img2.JPG")); } if (pic == 3) { hangImage.setIcon(new javax.swing.ImageIcon("img3.JPG")); } if (pic == 4) { hangImage.setIcon(new javax.swing.ImageIcon("img4.JPG")); } if (pic == 5) { hangImage.setIcon(new javax.swing.ImageIcon("img5.JPG")); } if (pic == 6) { hangImage.setIcon(new javax.swing.ImageIcon("img6.JPG")); } if (pic == 7) { hangImage.setIcon(new javax.swing.ImageIcon("img7.JPG")); } if (pic == 8) { hangImage.setIcon(new javax.swing.ImageIcon("img8.JPG")); } if (pic == 9) { hangImage.setIcon(new javax.swing.ImageIcon("img9.JPG")); } } /** * @param args the command line arguments */ public static void main(String args[]) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new HangForm().setVisible(true); } } } // Variables declaration - do not modify private javax.swing.JTextField aLetter; private javax.swing.JLabel char0; private javax.swing.JLabel char1; private javax.swing.JLabel char2; private javax.swing.JLabel char3; private javax.swing.JLabel char4; private javax.swing.JLabel char5; private javax.swing.JButton hangImage; private javax.swing.JLabel incorrect; private javax.swing.JLabel myMessage; private javax.swing.JButton tryLetter; // End of variables declaration }Last edited by Yozobozo; 12-09-2012 at 09:36 PM.
-
Re: Need help with creating a "Hangman" game
Look above that line. Is that method nested inside of another method? If so, please understand that you cannot nest methods in Java. You will want to carefully check all opening and closing curly braces to be sure that they match and that they line up well. Here is why code formatting is so important.
- 12-09-2012, 09:55 PM #9
Re: Need help with creating a "Hangman" game
Not from the NetBeans UI, no, or at least -- not yet. You need to edit <UserHome>/.netbeans/<Version>/config/Preferences/org/netbeans/modules/java/source/BuildArtifactMapperImpl.properties and change askBeforeRunWithErrors=false to askBeforeRunWithErrors=true
Reference: Bug 186776 – Ability to re-enable warning when trying to run a project with errors
(Note that the path given in that page is not where I found the file)
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 12-09-2012, 10:44 PM #10
Member
- Join Date
- Dec 2012
- Posts
- 8
- Rep Power
- 0
Re: Need help with creating a "Hangman" game
Well my program fully works now and I have finished but I still have that small ")" Expected error, will this be a problem and should I attempt to fix it, or can I leave it?
Last edited by Yozobozo; 12-09-2012 at 10:47 PM.
-
Re: Need help with creating a "Hangman" game
Again, what line causes the compiler to complain???
- 12-10-2012, 12:47 AM #12
Member
- Join Date
- Dec 2012
- Posts
- 8
- Rep Power
- 0
Re: Need help with creating a "Hangman" game
Line 223.
-
Re: Need help with creating a "Hangman" game
- 12-10-2012, 02:34 AM #14
Member
- Join Date
- Dec 2012
- Posts
- 8
- Rep Power
- 0
Re: Need help with creating a "Hangman" game
-
Re: Need help with creating a "Hangman" game
> n line 223 I am receiving the error: ")" expected, I have tried to work out where this may need to go with no success.
OK then the error is telling you exactly what's wrong, and all you need to do is in the area of the main method count your open ( and ) parenthesis. Do they match?
- 12-10-2012, 02:57 AM #16
Member
- Join Date
- Dec 2012
- Posts
- 8
- Rep Power
- 0
Re: Need help with creating a "Hangman" game
On the 2nd line in between invokeLater and new Runnable there is an open ( parenthesis, however if I remove it the code breaks, and I don't know how to close it.Java Code:public static void main(String args[]) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new HangForm().setVisible(true); } }
-
Re: Need help with creating a "Hangman" game
You need to build the invokeLater yourself and then you'll see where it goes. It starts like this:
then add the invokeLater:Java Code:public static void main(String args[]) { }
Then add the Runnable:Java Code:public static void main(String args[]) { java.awt.EventQueue.invokeLater(); }
Then add the Runnable's run methodJava Code:public static void main(String args[]) { java.awt.EventQueue.invokeLater(new Runnable() { }); }
Then add the new HangFormJava Code:public static void main(String args[]) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { } }); }
So the location of the closing parenthesis just falls into place when you build the code stepwise.Java Code:public static void main(String args[]) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new HangForm().setVisible(true); } }); // **** here it is **** }
- 12-10-2012, 03:15 AM #18
Member
- Join Date
- Dec 2012
- Posts
- 8
- Rep Power
- 0
Similar Threads
-
access denied("java.net.SocketPermission" "127.0.0.1:1099" "connect,resolve")
By klspepper in forum New To JavaReplies: 0Last Post: 12-07-2012, 08:29 AM -
Convert string operation symbols "+", "-", "/", "*" etc.
By Googol in forum New To JavaReplies: 3Last Post: 10-30-2012, 03:06 PM -
loop "play again" in an 8 ball game , loops but wont let me answer my "out.print"
By IareSmart in forum New To JavaReplies: 1Last Post: 02-01-2012, 08:37 PM -
pretty much completed hangman, but "new" button is not working
By diggitydoggz in forum New To JavaReplies: 10Last Post: 12-30-2008, 03:46 PM -
the dollar sign "$", prints like any other normal char in java like "a" or "*" ?
By lse123 in forum New To JavaReplies: 1Last Post: 10-20-2008, 07:35 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks