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

Thread: help with the method

  1. #1
    Junior Member
    Join Date
    Jul 2012
    Posts
    2
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Unhappy help with the method

    i have some problems with methods
    see in my code i am using a simple method but the compiler is showing strange errors
    import java.util.Scanner;
    class apple
    {
    public static void main(String args[])
    {	int i,j;
    	Scanner input = new Scanner(System.in);
     
    	int ship1[][]= new int[6][6],ship2[][]=new int[6][6];
     
    	public void ship() // error in this statement
    	{
    	for(i=0;i<6;i++)
    	{	System.out.println("");
    		for(j=0;j<6;j++)
    		{
    			System.out.print("*");
    		}
    	}
    	}
     
    }
    }
    ERRORS: its in the void ship () statement
    Multiple markers at this line
    - Syntax error, insert "enum Identifier" to complete
    EnumHeaderName
    - Syntax error on token "void", @ expected
    - Syntax error, insert "EnumBody" to complete BlockStatement



    so can any1 guide me to rectify this problem


  2. #2
    Junior Member ShadeDarkan's Avatar
    Join Date
    Jul 2012
    Location
    Houston, TX
    Posts
    13
    Thanks
    0
    Thanked 4 Times in 3 Posts

    Default Re: help with the method

    I copied your code into netbeans to look at it further. The following re-format let me compile with no errors:
    import java.util.Scanner;
    public class apple
    {
        public static void main(String args[])
        {	
                int i,j;
                Scanner input = new Scanner(System.in);
     
                int ship1[][]= new int[6][6];
                int ship2[][]= new int[6][6];           
        }
        public void ship() // error in this statement
        {
            for(int i=0;i<6;i++)
            {	
                System.out.println("");
                for(int j=0;j<6;j++)
                {
                    System.out.print("*");
                }
            }
        }
    }

    I can't remember, because I don't do it anyway, if you are allowed to define methods inside the Public Void Main method. I am going to say no since moving your Public Void Shift method outside of the Main method fixed that error.
    The other thing is you can't define ship2 in this manner int ship1[][]= new int[6][6],ship2[][]=new int[6][6];. It yields an error also.

  3. The Following User Says Thank You to ShadeDarkan For This Useful Post:

    fantasy99 (July 9th, 2012)

  4. #3
    Junior Member
    Join Date
    Jul 2012
    Posts
    3
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: help with the method

    import java.util.Scanner;
    class apple
    {
    public static void main(String args[])
    {
    Scanner input = new Scanner(System.in);

    int ship1[][]= new int[6][6],ship2[][]=new int[6][6];
    ship()
    }
    public static void ship() // error in this statement
    {
    int i,j;
    for(i=0;i<6;i++)
    { System.out.println("");
    for(j=0;j<6;j++)
    {
    System.out.print("*");
    }
    }
    }

    }

  5. The Following User Says Thank You to bejoykodiyan For This Useful Post:

    fantasy99 (July 9th, 2012)

  6. #4
    Member
    Join Date
    Jul 2012
    Posts
    119
    Thanks
    0
    Thanked 19 Times in 19 Posts

    Default Re: help with the method

    it's simpler to declare as following
    int[][] ship1= new int[6][6],ship2=new int[6][6];

  7. The Following User Says Thank You to Voodoo For This Useful Post:

    fantasy99 (July 9th, 2012)

  8. #5
    Junior Member
    Join Date
    Jul 2012
    Posts
    2
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: help with the method

    thnx for d reply guys
    was implementing a bit of c++
    problem is solved now

    but still guys if i wanna use any method in the main () then wats the syntax for dat
    coz i m not able to declare any method in main() and only static methods outside the main() could be called
    Last edited by fantasy99; July 9th, 2012 at 04:19 AM.

  9. #6
    Junior Member ShadeDarkan's Avatar
    Join Date
    Jul 2012
    Location
    Houston, TX
    Posts
    13
    Thanks
    0
    Thanked 4 Times in 3 Posts

    Default Re: help with the method

    This seems to be how Java works.
    The below link is to the DreamInCode.net forum thread where Martyr2 answers the question about calling methods from Main:
    How To Call A Non Static Function From Main Function? - Java | Dream.In.Code

Similar Threads

  1. Replies: 3
    Last Post: October 31st, 2011, 12:42 AM
  2. How do I call a method from the main method?
    By JavaStudent1988 in forum Java Theory & Questions
    Replies: 5
    Last Post: October 19th, 2011, 08:37 PM
  3. Can you pass parameters from one method into another method?
    By u-will-neva-no in forum Java Theory & Questions
    Replies: 2
    Last Post: April 14th, 2011, 07:46 AM
  4. Help with toString method and an addObject method?
    By Camisado in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 12th, 2011, 07:00 AM
  5. Can i call init() method in destroy method.?
    By muralidhar in forum Java Servlet
    Replies: 1
    Last Post: October 22nd, 2010, 11:18 AM