Results 1 to 6 of 6
  1. #1
    B3rtbot is offline Member
    Join Date
    Sep 2011
    Location
    Bristol, England
    Posts
    5
    Rep Power
    0

    Default Educational Project

    I'm creating this educational program for my college project. I'm quite near the start of coding, and I'm already stuck on why it isn't compiling and what I should be doing next to get the input from the user in Swing and if it detects if it is right or not.

    Java Code:
    import java.awt.event.ActionListener;
    import java.util.Random;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JOptionPane;
    
    public class NewClass {
    
    	/**
    	 * This is the code to go with my Extended Project. Creating a educational program for primary school children, consisting of
    	 * math's questions.
    	 * Version 1.0
    	 */
    	public static void main(String[]args) {
    		Random rand = new Random();
    		int num1 = rand.nextInt(25);
    		int num2 = rand.nextInt(25);
    		int num3 = num1 + num2;
    		int guess;
    	
    		JFrame frame = new JFrame("Input Dialog Box Frame");
    		JButton button = new JButton("Show Input Dialog Box");
    		button.addActionListener(new ActionListener(){
    			public void actionPerformed(ActionEvent ae, int num1, int num2, int num3){
    				String question = JOptionPane.showInputDialog("What is " + num1 + " + " + num2 +"?");
    			}
    		}
    		
    		
    	}
    }
    Any help would be greatly appreciated.

  2. #2
    Iron Lion is offline Senior Member
    Join Date
    Nov 2010
    Posts
    210
    Rep Power
    4

    Default Re: Educational Project

    If your code doesn't compile, it helps if you post any error messages you get. But I'll take a shot...

    Java Code:
    button.addActionListener(new ActionListener(){
    Where are you closing the set of parentheses that opens after addActionListener?

  3. #3
    B3rtbot is offline Member
    Join Date
    Sep 2011
    Location
    Bristol, England
    Posts
    5
    Rep Power
    0

    Default Re: Educational Project

    Sorry, here's the error log!


    Java Code:
    Exception in thread "main" java.lang.Error: Unresolved compilation problems: 
    	The type new ActionListener(){} must implement the inherited abstract method ActionListener.actionPerformed(ActionEvent)
    	ActionEvent cannot be resolved to a type
    	Syntax error, insert ")" to complete Expression
    	Syntax error, insert ";" to complete Statement
    
    	at NewClass.main(NewClass.java:23)

  4. #4
    Iron Lion is offline Senior Member
    Join Date
    Nov 2010
    Posts
    210
    Rep Power
    4

    Default Re: Educational Project

    The type new ActionListener(){} must implement the inherited abstract method ActionListener.actionPerformed(ActionEvent)
    Your implementation of actionPerformed() takes the wrong arguments. As the compiler says, its only argument should be an ActionEvent, but you're trying to pass 3 ints along with it.

    Try declaring num1, num2 and num3 as class variables, instead of declaring them in the main method. That way, the actionPerformed() method will be able to see them without having them passed to it.

    ActionEvent cannot be resolved to a type
    Java Code:
    import java.awt.event.ActionEvent;
    Syntax error, insert ")" to complete Expression
    Syntax error, insert ";" to complete Statement
    See my post above - you've opened a pair of parentheses for a method call without closing them or terminating the statement.

  5. #5
    B3rtbot is offline Member
    Join Date
    Sep 2011
    Location
    Bristol, England
    Posts
    5
    Rep Power
    0

    Default Re: Educational Project

    Here's my new code. I was wondering how I could use While to create a loop which keeps making it go back to the question until it is answered correctly. I tried to make one, but I was stuck in an infinite loop :(

    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.util.Random;

    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JOptionPane;
    import javax.swing.JPanel;

    public class NewClass {

    /**
    * This is the code to go with my Extended Project. Creating a educational program for primary school children, consisting of
    * math's questions.
    * Version 1.0
    */
    public static void main(String[]args) {

    JFrame frame = new JFrame("Maths Questions");
    JButton button = new JButton("Start the questions");
    button.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent ae) {
    Random rand2 = new Random();
    int num1 = rand2.nextInt(25);
    int num2 = rand2.nextInt(25);
    int num3 = num1+num2;
    String answer = JOptionPane.showInputDialog("What is " + num1 + " + " + num2 + "?");

    if (answer != null)
    JOptionPane.showMessageDialog(null, "Sorry, that is wrong, try again:");


    }



    });
    JPanel panel = new JPanel();
    panel.add(button);
    frame.add(panel);
    frame.setSize(300, 73);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
    frame.setVisible(true);

    }
    }

  6. #6
    Iron Lion is offline Senior Member
    Join Date
    Nov 2010
    Posts
    210
    Rep Power
    4

    Default Re: Educational Project

    Shouldn't you be converting answer to an int and comparing it with num3? How does the program know whether the answer is correct?

Similar Threads

  1. Educational practice in NYC
    By barss in forum Jobs Discussion
    Replies: 1
    Last Post: 08-19-2011, 01:22 PM
  2. How to convert from Ant project to maven project?
    By jiapei100 in forum New To Java
    Replies: 0
    Last Post: 06-08-2011, 10:01 AM
  3. [CodeFest] Virtual Combat : An extension of educational game
    By codefest11 in forum Reviews / Advertising
    Replies: 0
    Last Post: 02-10-2011, 07:52 PM
  4. What Java educational book to buy?
    By goffy in forum New To Java
    Replies: 3
    Last Post: 06-20-2010, 02:34 PM

Posting Permissions

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