Results 1 to 18 of 18
  1. #1
    Yozobozo is offline Member
    Join Date
    Dec 2012
    Posts
    8
    Rep Power
    0

    Default 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:
    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)
    I am also referred to this:
    Java Code:
    C:\Users\Yusuf\.netbeans\6.8\var\cache\executor-snippets\run.xml:45:
    However I have no idea what this means.
    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                   
    }

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,523
    Blog Entries
    7
    Rep Power
    20

    Default 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,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default 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.

  4. #4
    Yozobozo is offline Member
    Join Date
    Dec 2012
    Posts
    8
    Rep Power
    0

    Default 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"));
        }

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default 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.

  6. #6
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Need help with creating a "Hangman" game

    "C:\Users\Yusuf\Desktop\HangMan\img5.JPG"
    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?)

    I put the string literal you used into a program and tried to compile it. This is what happened:

    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>
    Do you understand what the compiler means by saying that \U, \Y, \D etc are illegal escape characters?
    Last edited by pbrockway2; 12-09-2012 at 09:06 PM.

  7. #7
    Yozobozo is offline Member
    Join Date
    Dec 2012
    Posts
    8
    Rep Power
    0

    Default 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:
    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                   
    }
    On line 223 I am receiving the error: ")" expected, I have tried to work out where this may need to go with no success.
    Last edited by Yozobozo; 12-09-2012 at 09:36 PM.

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default 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.

  9. #9
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,237
    Rep Power
    19

    Default Re: Need help with creating a "Hangman" game

    Quote Originally Posted by pbrockway2 View Post
    (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?)
    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 &ndash; 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)

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  10. #10
    Yozobozo is offline Member
    Join Date
    Dec 2012
    Posts
    8
    Rep Power
    0

    Default 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.

  11. #11
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  12. #12
    Yozobozo is offline Member
    Join Date
    Dec 2012
    Posts
    8
    Rep Power
    0

    Default Re: Need help with creating a "Hangman" game

    Line 223.

  13. #13
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Need help with creating a "Hangman" game

    Quote Originally Posted by Yozobozo View Post
    Line 223.
    You're still not showing us the code on that line, and there is no way we can find which line is 223 in your file on your computer. Good luck with solving this problem.

  14. #14
    Yozobozo is offline Member
    Join Date
    Dec 2012
    Posts
    8
    Rep Power
    0

    Default Re: Need help with creating a "Hangman" game

    Quote Originally Posted by Yozobozo View Post
    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:
    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                   
    }
    On line 223 I am receiving the error: ")" expected, I have tried to work out where this may need to go with no success.
    You're still not showing us the code on that line, and there is no way we can find which line is 223 in your file on your computer. Good luck with solving this problem.
    I've already shown where it is, line 223 of the code that I posted above.

  15. #15
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default 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?

  16. #16
    Yozobozo is offline Member
    Join Date
    Dec 2012
    Posts
    8
    Rep Power
    0

    Default Re: Need help with creating a "Hangman" game

    Java Code:
     public static void main(String args[]) {
            java.awt.EventQueue.invokeLater(new Runnable() {
     
                public void run() {
                    new HangForm().setVisible(true);
                }
            }
    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.

  17. #17
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default 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:

    Java Code:
    public static void main(String args[]) {
    
    }
    then add the invokeLater:
    Java Code:
    public static void main(String args[]) {
      java.awt.EventQueue.invokeLater();
    }
    Then add the Runnable:

    Java Code:
    public static void main(String args[]) {
      java.awt.EventQueue.invokeLater(new Runnable() { });
    }
    Then add the Runnable's run method

    Java Code:
    public static void main(String args[]) {
      java.awt.EventQueue.invokeLater(new Runnable() { 
        public void run() {
    
        }
      });
    }
    Then add the new HangForm

    Java Code:
    public static void main(String args[]) {
      java.awt.EventQueue.invokeLater(new Runnable() { 
        public void run() {
          new HangForm().setVisible(true);
        }
      });  // **** here it is ****
    }
    So the location of the closing parenthesis just falls into place when you build the code stepwise.

  18. #18
    Yozobozo is offline Member
    Join Date
    Dec 2012
    Posts
    8
    Rep Power
    0

    Default Re: Need help with creating a "Hangman" game

    Thank you so much that works, I could've sworn I'd tried that myself but I guess my program was even more broken back then so it didn't work lol. Nevertheless I can't thank you enough for your help and sorry it took so long.

Similar Threads

  1. Replies: 0
    Last Post: 12-07-2012, 08:29 AM
  2. Replies: 3
    Last Post: 10-30-2012, 03:06 PM
  3. Replies: 1
    Last Post: 02-01-2012, 08:37 PM
  4. Replies: 10
    Last Post: 12-30-2008, 03:46 PM
  5. Replies: 1
    Last Post: 10-20-2008, 07:35 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •