Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: My Java program gets a strange error

  1. #1
    Junior Member 119mikkel's Avatar
    Join Date
    Aug 2013
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Exclamation My Java program gets a strange error

    I get a strange error message in my program here is my code:
    package code;
    import java.awt.FlowLayout;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JButton;
    import javax.swing.JOptionPane;
    import javax.swing.JTextField;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
    import java.util.Random;
     
    public class GUI extends JFrame{
    	private static final long serialVersionUID = 1L;
    	private JLabel label1;
    	private JButton button1;
    	private JTextField textbox1;
    	private JTextField textbox2;
    	private JTextField textbox3;
     
    	Random Rn = new Random();
    	int number;
     
    	String p1;
    	String p2;
    	String p3;
     
    	for(int counter=0; counter<1; counter++){
    		number = Rn.nextInt(3);
    	}
     
    	public GUI(){
    		setLayout(new FlowLayout());
     
    		label1 = new JLabel("Enter names.");
    		add(label1);
     
    		textbox1 = new JTextField("Player 1");
    		add(textbox1);
     
    		textbox2 = new JTextField("Player 2");
    		add(textbox2);
     
    		textbox3 = new JTextField("Player 3");
    		add(textbox3);
     
    		button1 = new JButton("OK");
    		add(button1);
     
    		HandlerClass handler = new HandlerClass();
    		button1.addActionListener(handler);
    	}
    	private class HandlerClass implements ActionListener{
    		public void actionPerformed(ActionEvent event) {
    			switch (Rn.nextInt()){
    			case 1:
    				JOptionPane.showMessageDialog(null, p1 + "IS AWESOME!");
    				break;
    			case 2:
    				JOptionPane.showMessageDialog(null, p2 + "IS AWESOME!");
    				break;
    			case 3:
    				JOptionPane.showMessageDialog(null, p3 + "IS AWESOME!");
    				break;
    			}
    		}
     
    	}
    }

    i get a error on line 25 saying: Syntax error on token ";",{ expected after this token
    an a error on line 29 saying: Syntax error, inset "}" to complete Block
    but if i remove
    for(int counter=0; counter<1; counter++){
    	      number = Rn.nextInt(3);
             }
    the error go's away but if i remove that the code wont work
    Check out Coding Univers


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: My Java program gets a strange error

    You can't have expressions like for loops outside a method.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. The Following User Says Thank You to KevinWorkman For This Useful Post:

    119mikkel (August 12th, 2013)

  4. #3
    Junior Member 119mikkel's Avatar
    Join Date
    Aug 2013
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: My Java program gets a strange error

    Soo i need to put in the main method and put it in ther? or what?

    --- Update ---

    I think i got it working now, now i just got to get my button event working right
    Check out Coding Univers

Similar Threads

  1. Replies: 1
    Last Post: January 17th, 2013, 07:04 AM
  2. Strange errors with java.nio.file
    By jnewb in forum What's Wrong With My Code?
    Replies: 9
    Last Post: January 4th, 2012, 02:37 PM
  3. Making a living from Java in strange ways :)
    By Squiffy in forum Member Introductions
    Replies: 3
    Last Post: November 21st, 2011, 05:10 AM
  4. Strange error message
    By javapenguin in forum What's Wrong With My Code?
    Replies: 7
    Last Post: January 11th, 2011, 02:03 PM
  5. Strange Compiling Error
    By crism85 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: October 13th, 2009, 12:59 AM

Tags for this Thread