Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. Java
  4. User defined Method

User defined Method

Scheduled Pinned Locked Moved Java
javahelpquestion
7 Posts 2 Posters 30 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    MallardsReach
    wrote on last edited by
    #1

    Looked at many examples of Method and they all seem easy until I start to place them in my code. No matter where I insert the method or how I rewrite it I still get a problem. Below is my Method followed by my code. What am I not doing?

       void getSecond(int secondNum){
        displayTable.setText("\\n"); //Clear the displayTable 
        int firstNum, answerNum;
            for (firstNum = 1; firstNum <=12; firstNum ++){
                answerNum = firstNum \* secondNum;
                displayTable.append(firstNum + " x " + secondNum + " = " + answerNum +"\\n");
             }
    

    import javax.swing.*;
    import java.awt.Color;
    import java.awt.Font;
    import javax.swing.BorderFactory;
    import javax.swing.border.Border;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;

    public class MyTables
    {

    public static void main(String\[\] args) 
    {
    	JFrame frame = new JFrame("Tables"); // Creating instance of JFrame
        frame.setSize(800, 600); // Setting the width and height of frame
        frame.setDefaultCloseOperation(JFrame.EXIT\_ON\_CLOSE); 
    	JPanel panel = new JPanel(); //Creating panel. Inside panels we can add textfields, buttons and other components.
    	panel.setBorder(BorderFactory.createLineBorder(Color.RED));
        frame.add(panel); // Add panel to frame
        placeComponents(panel);  //Call user defined method for adding components to the panel.
    	frame.setVisible(true); // Setting the frame visibility to true
    }
    
    private static void placeComponents(JPanel panel) 
    {
    		panel.setLayout(null);
    		JTextArea displayTable = new JTextArea("Text Area");
    		displayTable.setEditable(false);
    		displayTable.setColumns(20);
    		displayTable.setFont(new Font("Times New Roman", 1, 14));
    		displayTable.setLineWrap(true);
    		displayTable.setRows(14);
    		displayTable.setWrapStyleWord(true);
    		displayTable.setBounds(200,20,230,260); 
    		displayTable.setBorder(BorderFactory.createLineBorder(Color.GREEN));
    		displayTable.setMaximumSize(new java.awt.Dimension(5, 22));
    		panel.add(displayTable);
    		
    		JLabel userLabel = new JLabel("Enter a Number");  // Creating JLabel
    		userLabel.setHorizontalAlignment(SwingConstants.CENTER);
    		userLabel.setFont(new Font("Verdana", Font.ITALIC, 16)); //set font
    		userLabel.setBounds(10,20,180,25); // Specifies the location and size of component. (x,y,x1,y1)
    		userLabel.setBorder(BorderFactory.createLineBorder(Color.BLUE));	
    		panel.add(userLabel); // Add Label
    
    L 1 Reply Last reply
    0
    • M MallardsReach

      Looked at many examples of Method and they all seem easy until I start to place them in my code. No matter where I insert the method or how I rewrite it I still get a problem. Below is my Method followed by my code. What am I not doing?

         void getSecond(int secondNum){
          displayTable.setText("\\n"); //Clear the displayTable 
          int firstNum, answerNum;
              for (firstNum = 1; firstNum <=12; firstNum ++){
                  answerNum = firstNum \* secondNum;
                  displayTable.append(firstNum + " x " + secondNum + " = " + answerNum +"\\n");
               }
      

      import javax.swing.*;
      import java.awt.Color;
      import java.awt.Font;
      import javax.swing.BorderFactory;
      import javax.swing.border.Border;
      import java.awt.event.ActionEvent;
      import java.awt.event.ActionListener;

      public class MyTables
      {

      public static void main(String\[\] args) 
      {
      	JFrame frame = new JFrame("Tables"); // Creating instance of JFrame
          frame.setSize(800, 600); // Setting the width and height of frame
          frame.setDefaultCloseOperation(JFrame.EXIT\_ON\_CLOSE); 
      	JPanel panel = new JPanel(); //Creating panel. Inside panels we can add textfields, buttons and other components.
      	panel.setBorder(BorderFactory.createLineBorder(Color.RED));
          frame.add(panel); // Add panel to frame
          placeComponents(panel);  //Call user defined method for adding components to the panel.
      	frame.setVisible(true); // Setting the frame visibility to true
      }
      
      private static void placeComponents(JPanel panel) 
      {
      		panel.setLayout(null);
      		JTextArea displayTable = new JTextArea("Text Area");
      		displayTable.setEditable(false);
      		displayTable.setColumns(20);
      		displayTable.setFont(new Font("Times New Roman", 1, 14));
      		displayTable.setLineWrap(true);
      		displayTable.setRows(14);
      		displayTable.setWrapStyleWord(true);
      		displayTable.setBounds(200,20,230,260); 
      		displayTable.setBorder(BorderFactory.createLineBorder(Color.GREEN));
      		displayTable.setMaximumSize(new java.awt.Dimension(5, 22));
      		panel.add(displayTable);
      		
      		JLabel userLabel = new JLabel("Enter a Number");  // Creating JLabel
      		userLabel.setHorizontalAlignment(SwingConstants.CENTER);
      		userLabel.setFont(new Font("Verdana", Font.ITALIC, 16)); //set font
      		userLabel.setBounds(10,20,180,25); // Specifies the location and size of component. (x,y,x1,y1)
      		userLabel.setBorder(BorderFactory.createLineBorder(Color.BLUE));	
      		panel.add(userLabel); // Add Label
      
      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      MallardsReach wrote:

      I still get a problem.

      That is one of the most common reports that we see here, and also the most useless. Unless you tell us exactly what the problem is (including exact error messages), and where it occurs, then we can do very little. Please try to help us to help you.

      M 1 Reply Last reply
      0
      • L Lost User

        MallardsReach wrote:

        I still get a problem.

        That is one of the most common reports that we see here, and also the most useless. Unless you tell us exactly what the problem is (including exact error messages), and where it occurs, then we can do very little. Please try to help us to help you.

        M Offline
        M Offline
        MallardsReach
        wrote on last edited by
        #3

        Sorry Richard, honestly thought I had given the error. If I place the code here

        public class MyTables
        {
        void getSecond(int secondNum){
        displayTable.setText("\n"); //Clear the displayTable
        int firstNum, answerNum;
        for (firstNum = 1; firstNum <=12; firstNum ++){
        answerNum = firstNum * secondNum;
        displayTable.append(firstNum + " x " + secondNum + " = " + answerNum +"\n");
        }
        public static void main(String[] args)

        I get this message

        Quote:

        c:\Java>javac MyTables.java MyTables.java:18: error: illegal start of expression public static void main(String[] args) ^ 1 error

        If I put it at the end

        loginButton.addActionListener(new ActionListener()
        {
        public void actionPerformed(ActionEvent e)
        {
        //getSecond(6);
        displayTable.setText("Button Pressed");
        }
        void getSecond(int secondNum){
        displayTable.setText("\n"); //Clear the displayTable
        int firstNum, answerNum;
        for (firstNum = 1; firstNum <=12; firstNum ++){
        answerNum = firstNum * secondNum;
        displayTable.append(firstNum + " x " + secondNum + " = " + answerNum +"\n");
        }

        	});
        }		
        

        }

        I get 3 error messages

        Quote:

        c:\Java>javac MyTables.java MyTables.java:69: error: illegal start of type }); ^ MyTables.java:70: error: ')' expected } ^ MyTables.java:71: error: reached end of file while parsing } ^ 3 errors

        L 1 Reply Last reply
        0
        • M MallardsReach

          Sorry Richard, honestly thought I had given the error. If I place the code here

          public class MyTables
          {
          void getSecond(int secondNum){
          displayTable.setText("\n"); //Clear the displayTable
          int firstNum, answerNum;
          for (firstNum = 1; firstNum <=12; firstNum ++){
          answerNum = firstNum * secondNum;
          displayTable.append(firstNum + " x " + secondNum + " = " + answerNum +"\n");
          }
          public static void main(String[] args)

          I get this message

          Quote:

          c:\Java>javac MyTables.java MyTables.java:18: error: illegal start of expression public static void main(String[] args) ^ 1 error

          If I put it at the end

          loginButton.addActionListener(new ActionListener()
          {
          public void actionPerformed(ActionEvent e)
          {
          //getSecond(6);
          displayTable.setText("Button Pressed");
          }
          void getSecond(int secondNum){
          displayTable.setText("\n"); //Clear the displayTable
          int firstNum, answerNum;
          for (firstNum = 1; firstNum <=12; firstNum ++){
          answerNum = firstNum * secondNum;
          displayTable.append(firstNum + " x " + secondNum + " = " + answerNum +"\n");
          }

          	});
          }		
          

          }

          I get 3 error messages

          Quote:

          c:\Java>javac MyTables.java MyTables.java:69: error: illegal start of type }); ^ MyTables.java:70: error: ')' expected } ^ MyTables.java:71: error: reached end of file while parsing } ^ 3 errors

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          Look at the code, it has two opening brace characters ({), one for the method, and one for the for loop. But it only has one closing brace, for the for loop. So you just need to add the one that closes the method block.

          M 1 Reply Last reply
          0
          • L Lost User

            Look at the code, it has two opening brace characters ({), one for the method, and one for the for loop. But it only has one closing brace, for the for loop. So you just need to add the one that closes the method block.

            M Offline
            M Offline
            MallardsReach
            wrote on last edited by
            #5

            That's done it, although I am annoyed as I knew the curly bracket was missing but when I added it I got different errors. After your reply I removed the method placed it in a new page and added the bracket so that it had 2 opening and 2 closing, placed it back in my project and it worked. I do remember that every opening ( or { had to have a matching one closing. Can now carry on with the project. Thank you

            L 1 Reply Last reply
            0
            • M MallardsReach

              That's done it, although I am annoyed as I knew the curly bracket was missing but when I added it I got different errors. After your reply I removed the method placed it in a new page and added the bracket so that it had 2 opening and 2 closing, placed it back in my project and it worked. I do remember that every opening ( or { had to have a matching one closing. Can now carry on with the project. Thank you

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              The thing to remember is when you start any code block is to add the braces first. You can then fill in the actual code afterwards:

              int someMethod(int parameter)
              {
              // TODO: the details must be added here
              }
              int someOtherMethod(int parameter)
              {
              while (some expression)
              {
              // TODO: more details required
              }
              if (some boolean expression
              {
              // TODO: check some stuff
              }
              }

              But always remember, making mistakes is how we all learned ...

              M 1 Reply Last reply
              0
              • L Lost User

                The thing to remember is when you start any code block is to add the braces first. You can then fill in the actual code afterwards:

                int someMethod(int parameter)
                {
                // TODO: the details must be added here
                }
                int someOtherMethod(int parameter)
                {
                while (some expression)
                {
                // TODO: more details required
                }
                if (some boolean expression
                {
                // TODO: check some stuff
                }
                }

                But always remember, making mistakes is how we all learned ...

                M Offline
                M Offline
                MallardsReach
                wrote on last edited by
                #7

                Thanks Richard that is a good piece of advice that I will remember.

                But always remember, making mistakes is how we all learned ...

                And this gives me some encouragement as it seems I'm making quite a few.

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups