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

Thread: Cannot find where extra brace is at causing my errors

  1. #1
    Junior Member
    Join Date
    Jun 2011
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Cannot find where extra brace is at causing my errors

    The following code i am having trouble with because of multiple errors saying class interface or enum expected. i was told it is extra or mismatched brace but cannot locate it any to help? Sorry if i didnt enclose code correctly still learning how to do that here

    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
     
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JTextField;
     
    public class BasicCalculator {
    	private static final int FRAME_WIDTH = 150;
    	private static final int FRAME_HEIGHT = 120;
     
    	// Keeps track of the current operation (subtract, add, etc)
    	private static final int NO_OPERATION = 0;
    	private static final int ADDITION = 1;
    	public static int operation = NO_OPERATION;
     
    	public static JTextField textFieldDisplay;
    	public static double Value1 = 0; // holds the value before the operation
     
    	public static void main(String[] args) {
    		// Set up the user interface
    		JFrame frame = new JFrame();
    		JPanel buttonPanel = new JPanel();
    		frame.add(buttonPanel);
     
    		// create two buttons, plus and equal and a text box for answers
    		textFieldDisplay = new JTextField(10);
    		buttonPanel.add(textFieldDisplay);
    		JButton buttonPlus = new JButton(" + ");
    		buttonPanel.add(buttonPlus);
    		JButton buttonEqual = new JButton(" = ");
    		buttonPanel.add(buttonEqual);
     
    		frame.setSize(FRAME_WIDTH, FRAME_HEIGHT);
    		frame.setTitle("Basic Calculator");
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.setVisible(true);
     
     
    		// called when the equal sign '=' is pressed
    		class EqualSignListener implements ActionListener {
     
    			public void actionPerformed(ActionEvent event)
    			{
    				double Value2 = Double.parseDouble(textFieldDisplay.getText());
    				if (operation == ADDITION) {
    					// plus sign pressed before the equal sign
    					Value2 += Value1;
    				}
    				// Convert from a answer to a string
    				Double answer = new Double(Value2);
    				textFieldDisplay.setText( answer.toString() );
    				// Reset the operation to show no current operation
    				operation = NO_OPERATION;
    			}
    		}
     
    		// called when a plus sign '+' is pressed
    		class PlusSignListener implements ActionListener {
    			public void actionPerformed(ActionEvent event)
    			{
    				Value1 = Double.parseDouble(textFieldDisplay.getText());
    				operation = ADDITION;
    			}
    		}
     
    		// Add the methods that will be called when these buttons are pressed
    		ActionListener plusSignListener = new PlusSignListener();
    		buttonPlus.addActionListener(plusSignListener);
     
    		ActionListener equalSignListener = new EqualSignListener();
    		buttonEqual.addActionListener(equalSignListener);
     
    	}
    }
    // called when a subtraction sign '-' is pressed
    		class SubtractionSignListener implements ActionListener {
    			public void actionPerformed(ActionEvent event)
    			{
    				Value1 = Double.parseDouble(textFieldDisplay.getText());
    				operation = Subtraction;
    			}
    		}
     
    		// Add the methods that will be called when these buttons are pressed
    		ActionListener subtractionSignListener = new SubtractionSignListener();
    		buttonPlus.addActionListener(subtractionSignListener);
     
    		ActionListener equalSignListener = new EqualSignListener();
    		buttonEqual.addActionListener(equalSignListener);
             }
    )
    // called when a division sign '/' is pressed
    		class DivisionSignListener implements ActionListener {
    			public void actionPerformed(ActionEvent event)
    			{
    				Value1 = Double.parseDouble(textFieldDisplay.getText());
    				operation = DIVISION;
    			}
    		}
     
    		// Add the methods that will be called when these buttons are pressed
    		ActionListener divisionSignListener = new DivisionSignListener();
    		buttonPlus.addActionListener(plusSignListener);
     
    		ActionListener equalSignListener = new EqualSignListener();
    		buttonEqual.addActionListener(equalSignListener);
              }
    ) 
    // called when a plus sign '*' is pressed
    		class MulitplicationSignListener implements ActionListener {
    			public void actionPerformed(ActionEvent event)
    			{
    				Value1 = Double.parseDouble(textFieldDisplay.getText());
    				operation = Multiplication;
    			}
    		}
     
    		// Add the methods that will be called when these buttons are pressed
    		ActionListener multiplicationSignListener = new MultiplicationSignListener();
    		buttonPlus.addActionListener(plusSignListener);
     
    		ActionListener equalSignListener = new EqualSignListener();
    		buttonEqual.addActionListener(equalSignListener);
     
    	}


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,321
    Thanks
    56
    Thanked 2,374 Times in 2,345 Posts

    Default Re: Cannot find where extra brace is at causing my errors

    If your editor does not have the tool to match a { with its paired }, your next option is to print it and use a pen to manually pair the { }s. Start at the middle, circle the { and find the paired } and draw a connecting line.
    Continue until the mis-match is found.

  3. #3
    Junior Member
    Join Date
    Jun 2011
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Cannot find where extra brace is at causing my errors

    someone else said my public static main was not closed,but when I tried to close by adding brace more errors occurred..am i missing brace here?


    #public static void main(String[] args) {
    // Set up the user interface
    JFrame frame = new JFrame();
    JPanel buttonPanel = new JPanel();
    frame.add(buttonPanel);#

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,321
    Thanks
    56
    Thanked 2,374 Times in 2,345 Posts

    Default Re: Cannot find where extra brace is at causing my errors

    Can you see the 5 lines of code you posted?
    Is there a } character there?

    Please use code tags when posting your code. See: BB Code List - Java Forums

  5. #5
    Junior Member
    Join Date
    Jun 2011
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Cannot find where extra brace is at causing my errors

    No..however when i add one to different lines i get errors like crazy..which line does it need to be on?

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,321
    Thanks
    56
    Thanked 2,374 Times in 2,345 Posts

    Default Re: Cannot find where extra brace is at causing my errors

    Where is the } that pairs with the { at the end of this line?
    public static void main(String[] args) {

    The code within those that pair of {} is what is in the main method. The main method should be fairly short and not include class definitions.

  7. #7
    Junior Member
    Join Date
    Jun 2011
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Cannot find where extra brace is at causing my errors

    fixed almost all my errors now just getting <identifier> expected error on the third and last lines from the lines of code posted



    [code]// Add the methods that will be called when these buttons are pressed
    ActionListener divisionSignListener = new DivisionSignListener();
    buttonPlus.addActionListener(plusSignListener);

    ActionListener equalSignListener = new EqualSignListener();
    buttonEqual.addActionListener(equalSignListener);[code]

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,321
    Thanks
    56
    Thanked 2,374 Times in 2,345 Posts

    Default Re: Cannot find where extra brace is at causing my errors

    The compiler is looking for a class but is finding code that should be in a method.

    Is the code you posted inside of a method?

  9. #9
    Junior Member
    Join Date
    Jun 2011
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Cannot find where extra brace is at causing my errors

    You have been AWESOME in your assistance..down to one error. it is a "reached end of file without parsing" error. have added and taken away braces at end..does not help. Currently have two "}" braces at end of code do i need more? or different braces?

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,321
    Thanks
    56
    Thanked 2,374 Times in 2,345 Posts

    Default Re: Cannot find where extra brace is at causing my errors

    You need to pair them up. Start at the middle and pair the {}. Work out to the next pair and continue until you find the unpaired {}.
    Either use an editor tool or print it on paper and use a pencil.

  11. #11
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,550
    My Mood
    Grumpy
    Thanks
    0
    Thanked 169 Times in 160 Posts

    Default Re: Cannot find where extra brace is at causing my errors

    ActionListener equalSignListener = new EqualSignListener();
    buttonEqual.addActionListener(equalSignListener);
    }
    )
    I'm not sure if this has been resolved or not but what is the round bracket for?

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,321
    Thanks
    56
    Thanked 2,374 Times in 2,345 Posts

    Default Re: Cannot find where extra brace is at causing my errors

    but what is the round bracket for?
    Do you mean a paranthesis? You use pairs of them with methods.

  13. #13
    Junior Member
    Join Date
    Jun 2011
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Cannot find where extra brace is at causing my errors

    each time i take one out that i think is mismatched it compounds the errors. Any willing to look at code to see if they can find the mismatched or extra brace? So i can clear my one error left?

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,321
    Thanks
    56
    Thanked 2,374 Times in 2,345 Posts

    Default Re: Cannot find where extra brace is at causing my errors

    I didn't imagine that finding the paired {} was so hard.
    Did you print it out on paper and use a pen to match the {}s?

    Or another approach: make a copy of the code. Find an inner most { and label it thus: {1
    then find its matching } and label it thus: 1}
    Then go out a level and do the same matching but with a different number {2 and 2}
    Sometimes there will be some at the same level so: {3a and 3a}
    Keep going until you find the mis-matched { or }

  15. #15
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,550
    My Mood
    Grumpy
    Thanks
    0
    Thanked 169 Times in 160 Posts

    Default Re: Cannot find where extra brace is at causing my errors

    Quote Originally Posted by Norm View Post
    Do you mean a paranthesis? You use pairs of them with methods.
    Gee thanks Norm, I never knew that! [/sarcasm]

    What I meant was I could not find a matching bracket in the code. It seemed to me that it should not be there.

  16. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,321
    Thanks
    56
    Thanked 2,374 Times in 2,345 Posts

    Default Re: Cannot find where extra brace is at causing my errors

    Sorry, I didn't notice it was you. I thought it was the OP posting that comment. I have no idea what he knows or doesn't know about Java syntax.

Similar Threads

  1. Cannot Find Variable/Symbol Errors... why?
    By bgroenks96 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: June 9th, 2011, 05:12 AM
  2. [SOLVED] Could someone help me find my errors on this program?? (homework)
    By r19ecua in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 20th, 2011, 11:25 PM
  3. Replies: 3
    Last Post: February 23rd, 2011, 01:27 AM
  4. Help with Cannot Find Symbol Variable errors
    By skboone in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 20th, 2010, 11:52 AM
  5. Area of a triangle (using 2 extra methods) error help
    By SilentPirate in forum What's Wrong With My Code?
    Replies: 9
    Last Post: September 12th, 2010, 07:08 PM