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 8 of 8

Thread: Troubles with My code

  1. #1
    Junior Member
    Join Date
    Nov 2011
    Location
    Canada
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Troubles with My code

    Hi, I'm trying to make a GUI that creates a random number and then you have to guess the number. I get some weird errors in the code. First of all my code is

    import javax.swing.*;
    import java.util.*;
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
     
    public class Rand1 extends JFrame implements ActionListener {
     
    	private int numberToGuess;
    	Random rand = new Random ();
    	numberToGuess = rand.nextInt(100) ;
    	private int numTries = 0;
    	private int guess;
    	private boolean win = false;
    	private JLabel instructions;
    	private JLabel status;
    	private JTextField enternum;
    	private JButton button;
    	private String convert;
     
     
     
    	public Rand1(){
    		setLayout(null);
    		setSize(400,400);
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		instructions = new JLabel("Enter name:");
    		button = new JButton("Enter");
    		enternum = new JTextField("");
     
     
    		instructions.setBounds(60, 30, 120, 30);
    		enternum.setBounds(80, 160, 130, 60);
    		button.setBounds(200, 30, 120, 30);
    		status.setBounds(200, 200, 120, 30);
    		public void actionPerformed(ActionEvent e){
    			if(e.getSource() == button){
    				convert = enternum.getText();
    				guess = Integer.parseInt(convert);
    				numTries++;
    				if(guess == numberToGuess){
    				win = true;	
    				}
    			}	
    		if(win == true){
    			status  = new JLabel("You Win");
    			status.setBounds(80, 160, 130, 60);
    		}
    	}
     
     
    	}

    This program is not finished but I cannot continue with these weird errors. When I compile the program I get the error:
    Exception in thread "main" java.lang.Error: Unresolved compilation problems:
    Syntax error on token ";", , expected
    Syntax error, insert "}" to complete MethodBody

    at Rand1.<init>(Rand1.java:10)
    at Rand2.main(Rand2.java:4)

    Any help would be greatly appreciated I am completely confused because I am pretty sure nothing is wrong with my code

    Thanks


  2. #2
    Junior Member
    Join Date
    Nov 2011
    Location
    china
    Posts
    12
    My Mood
    Sad
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: Troubles with My code

    public class Rand1 extends JFrame implements ActionListener
    {

    Random rand = new Random();
    private int numberToGuess = rand.nextInt(100);
    private int numTries = 0;
    private int guess;
    private boolean win = false;
    private JLabel instructions;
    private JLabel status;
    private JTextField enternum;
    private JButton button;
    private String convert;



    public Rand1()
    {

    numberToGuess = rand.nextInt(100);
    setLayout(null);
    setSize(400,400);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    instructions = new JLabel("Enter name:");
    button = new JButton("Enter");
    enternum = new JTextField("");


    instructions.setBounds(60, 30, 120, 30);
    enternum.setBounds(80, 160, 130, 60);
    button.setBounds(200, 30, 120, 30);
    status.setBounds(200, 200, 120, 30);
    }
    public void actionPerformed(ActionEvent e)
    {
    if(e.getSource() == button)
    {
    convert = enternum.getText();
    guess = Integer.parseInt(convert);
    numTries++;
    if(guess == numberToGuess)
    win = true;
    }
    if(win == true)
    {
    status = new JLabel("You Win");
    status.setBounds(80, 160, 130, 60);
    }
    }
    }
    首先你少了一个"}"
    其次private int numberToGuess = rand.nextInt(100);直接赋值
    -------------------------------------------------------------------------
    First you have one less "}"
    Second, private int numberToGuess = rand.nextInt(100); the direct assignment

  3. #3
    Junior Member
    Join Date
    Nov 2011
    Location
    Canada
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Troubles with My code

    What do you mean
    HTML Code:
    Second, private int numberToGuess = rand.nextInt(100); the direct assignment
    ?

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,307
    Thanks
    181
    Thanked 824 Times in 767 Posts
    Blog Entries
    5

    Default Re: Troubles with My code

    @jcloud, use the code tags and read the following: The Problem with Spoon-feeding

    grimrader22, outside methods (with a few exceptions - this not being one of them) you cannot assign a variable unless you do so in combination with its declaration
    private int first = 1;//Good
    private int second;
    second = 2;//compile time error
    //or through a method
    public void setSecond(int s){
        second = s;
    }

  5. #5
    Junior Member
    Join Date
    Nov 2011
    Location
    Canada
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Troubles with My code

    So how would I use that to fix my code?

  6. #6
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,307
    Thanks
    181
    Thanked 824 Times in 767 Posts
    Blog Entries
    5

    Default Re: Troubles with My code

    Read the advice given above, and the code that you quoted in post #3: declare and assign in the same statement

  7. #7
    Junior Member
    Join Date
    Nov 2011
    Location
    Canada
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Troubles with My code

    I'm really confused on what I am supposed to do.

  8. #8
    Junior Member
    Join Date
    Nov 2011
    Location
    china
    Posts
    12
    My Mood
    Sad
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: Troubles with My code

    @copeg

    maybe you are right .whatever I will abide by the provisions of this community
    Last edited by jcloud; November 21st, 2011 at 02:00 AM.

Similar Threads

  1. Counter troubles
    By GinoGore in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 28th, 2011, 09:28 AM
  2. .setVisible() troubles
    By pottsiex5 in forum AWT / Java Swing
    Replies: 2
    Last Post: October 5th, 2011, 03:28 AM
  3. array troubles
    By mike2452 in forum Collections and Generics
    Replies: 3
    Last Post: August 14th, 2011, 07:22 PM
  4. ArrayList troubles
    By javapenguin in forum What's Wrong With My Code?
    Replies: 20
    Last Post: November 18th, 2010, 05:03 PM
  5. JSP Troubles
    By sdkeslar in forum What's Wrong With My Code?
    Replies: 0
    Last Post: February 12th, 2010, 01:26 PM

Tags for this Thread