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: Can't add a method without getting errors

  1. #1
    Junior Member
    Join Date
    Nov 2010
    Posts
    10
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Can't add a method without getting errors

    Hey I want to add the following method...
    	private static double Calculations(String Operator)
    {
    	return
    }

    ... to this program as follows (down at the bottom of this snippet)
    import javax.swing.*;
    import java.awt.event.*;
    import java.awt.*;
     
     
     
    class MyFrame extends JFrame
    {
    		JTextField ScreenOut;
    		JTextField ScreenIn;
     
    	public MyFrame()
    	{
    		//frame settings
    		setVisible (true);
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		setTitle("trololo");
    		setLocation(300,300);
    		setSize(300, 300);
     
    		//declarations
    		JButton[][] Keypad = new JButton[4][4];
    		String[][] KeypadLabels = {{"7","4","1","0"},{"8","5","2","C"},{"9","6","3","!"},{"+","-","*","="}};
    		JMenuBar Toolbar = new JMenuBar();
    		JMenu Options = new JMenu("Options");
    		JPanel Section1 = new JPanel();
    		JPanel Section2 = new JPanel();
    		JPanel Section3 = new JPanel();
     
     
    		//containers
    		java.awt.Container c = getContentPane();
    		c.setLayout(new java.awt.FlowLayout());
    		ScreenOut = new JTextField("", 15);
    		ScreenIn = new JTextField("", 15);
    		Section1.setLayout(new java.awt.FlowLayout());
    		Section1.add(ScreenIn);
    		Section2.setLayout(new java.awt.GridLayout(4,4));
    		Section3.setLayout(new java.awt.FlowLayout());
    		Section3.add(ScreenOut);
    		c.add(Section1);
    		c.add(Section2);
    		c.add(Section3);
    		Toolbar.add(Options);
    		setJMenuBar(Toolbar);
     
     
    		//keypad
    		for (int col =0; col<4;col++)
    		{
    			for (int row = 0; row<4;row++)
    			{
    				Keypad[col][row] = new JButton("");
    				Section2.add(Keypad[col][row]);;
    				Keypad[col][row].setLabel(KeypadLabels[row][col]);
    			}
    		}
     
    		//add functionality to numbers
    		for (int col=0;col<3;col++)
    		{
    			for (int row=0; row<3;row++)
    			{
    				Keypad[col][row].addActionListener(
    						new ActionListener()
    					{
     
    						public void actionPerformed(ActionEvent e)
    							{
    								JButton b = (JButton) e.getSource();
    								String Input = b.getLabel();
    								String Original = ScreenIn.getText();
    								ScreenIn.setText(Original + Input);
    							}
    					});
    			}
    		}
     
    		Keypad[3][0].addActionListener(
    			new ActionListener()
    			{
    				public void actionPerformed(ActionEvent e)
    				{
    					String Input = "0";
    					String Original = ScreenIn.getText();
    					ScreenIn.setText(Original + "0");
    				}
    			} );
     
    			//add functionailty to operators
    		for (int count=0; count<4; count++)
    			{
    				Keypad[count][3].addActionListener(
    					new ActionListener()
    					{
    						public void actionPerformed(ActionEvent e)
    						{
    							JButton b  = (JButton) e.getSource();
    							String s = b.getLabel();
    //							Calculations(s);
    						}
    					} ) ;
    			}
    	}
     
    ////	private static double Calculations(String Operator)
    ////	{
    ////				return
    ////	}   //this is line 109
     
    }//this is line 111

    class calculator
    {
    	public static void main(String Args[])
    	{
    		MyFrame f1 = new MyFrame();
    	}
    }

    Whenever I uncomment the method I get the following error
    --------------------Configuration: <Default>--------------------
    C:\Users\Peter\Documents\Peter\University\CSC 1011\Calculator\MyFrame.java:109: illegal start of expression
    }
    ^
    C:\Users\Peter\Documents\Peter\University\CSC 1011\Calculator\MyFrame.java:111: reached end of file while parsing
    }
    Process completed.
    I don't understand why though, whats wrong?
    Thanks in advance


  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: Can't add a method without getting errors

    You need a semicolon after return.

    But even if you fix that error, you still need to return a double from that 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:

    petemyster (April 15th, 2011)

  4. #3
    Junior Member
    Join Date
    Nov 2010
    Posts
    10
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Can't add a method without getting errors

    Quote Originally Posted by KevinWorkman View Post
    You need a semicolon after return.

    But even if you fix that error, you still need to return a double from that method.
    Thanks, just made it return 1.2; for testing purposes.

Similar Threads

  1. Can't fix my own errors
    By mrroberts2u in forum What's Wrong With My Code?
    Replies: 4
    Last Post: April 14th, 2011, 09:20 AM
  2. Can i call init() method in destroy method.?
    By muralidhar in forum Java Servlet
    Replies: 1
    Last Post: October 22nd, 2010, 11:18 AM
  3. Many Errors
    By Woody619 in forum What's Wrong With My Code?
    Replies: 10
    Last Post: July 16th, 2010, 09:36 PM
  4. Getting errors
    By Nonire in forum What's Wrong With My Code?
    Replies: 7
    Last Post: July 4th, 2010, 12:21 PM
  5. Why am I getting 62 errors?
    By DestinyChick1225 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: July 1st, 2010, 04:41 AM