Results 1 to 12 of 12
  1. #1
    javajames is offline Member
    Join Date
    May 2011
    Posts
    32
    Rep Power
    0

    Question Baffled while calling method

    Java Code:
    import java.io.*;
    import java.util.Scanner;
    
    class project12 {
        public void mast(String[] args) { //METHOD DECLARED HERE
            int num1 = 10429;
            int num2 = 15;
            double dnum1 = 47.8;
            double dnum2 = 5;
            System.out.println("Hello World!");
            System.out.println(num1 + "x" + num2 + "=" + (num1+num2) + " Hooray for addition");
            System.out.println(dnum1 + " devided by " + dnum2 + " gives us " + (dnum1/dnum2) + ", But you couldn't do this with integers so i used doubles\n");
    
            //counting system: example of the for loop which can do all that a while loop can do
            int i;
            System.out.println("below is an example of for loops, it repeats a proccess while counting to 10:");
            for (i=1; i<=10; i++) {
                System.out.println("count:" + i);
            }
    
            //Use of condition statements to generate boolean variable output
            System.out.println("\nif the following statements are true then it should show:");
            System.out.println(num1 + " > " + num2 + " is " + (num1>num2));
            System.out.println(num1 + " < " + num2 + " is " + (num1<num2));
    
            //input and conditions (requires 'import java.io.*;' for reader.readLine()):
            System.out.println("would you like to use the manuel counter? (y/n): ");
            int cuse = 'y'; //counter-use? ~= cuse
            try {
                cuse = System.in.read();
            }catch(java.io.IOException exp){ exp.printStackTrace();}
    
            if (cuse=='y') {
                int cnum = 10;
                System.out.println("enter a number (prefferably within the known and feasible universe): ");
                Scanner scan = new Scanner(System.in); //required for scan.nextInt()
                cnum = scan.nextInt(); //scans for integer input
                int dd;
                System.out.println("counting to " + cnum + ":");
                for (dd=1; dd<=cnum; dd++) {
                    System.out.println(dd);
                }
            }
            else {
                System.out.println("??? wtf??? why... what... wtf???");
            }
        }
        public static void main(String[] args) {
            int repet = 1;
            while (repet==1) {
                mast(); //METHOD CALLED HERE
                int re = 'y';
                System.out.println("\n\nwould you like to stay? (y/n): ");
                try {
                    re = System.in.read();
                }catch(java.io.IOException exp){ exp.printStackTrace();}
                if (re=='y') {
                    continue;
                } else {
                    repet = 3;
                    break;
                }
            }
            System.out.println("bye for now\n");
        }
    }
    i am very new to java and so im basically picking things up and testing them as i go along, this program IS completely useless, and (from my knowledge of other languages) chaotic and badly laid out. i have tried to integrate a lot of different concepts to practice my java, and it all worked untill i tried to make the method that had previously been main (mast() where is says METHOD DECLARED HERE) a separate method and make a new main method which i call the mast() method from. I compiled it with javac in ubuntu and this is what i got:
    Java Code:
    james@james-laptop:~/Desktop/coding/Jcoding$ javac pro1-3.java
    pro1-3.java:54: main(java.lang.String[]) in project13 cannot be applied to ()
                main();
                ^
    1 error
    i figure it is something gone wrong with my call of the mast function - err.. i mean method - but i don't know the precise nature of the problem. Could somebody please explain where i have gone wrong and how to put it right?

    (or direct me to a previous post that i haven't found)

    thanks in advance

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,729
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by javajames View Post
    Java Code:
    class project12 {
        public void mast(String[] args) { } //METHOD DECLARED HERE
        public static void main(String[] args) {
                mast(); //METHOD CALLED HERE
        }
    }
    I removed (quite) a bit of your code: you mast( ... ) method wants a String[] argument as its parameter but you're calling it without any parameter(s) at all; your compiler doesn't like that (it was complaining about the mast( ... ) method, not the main( ... ) method).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    Join Date
    Apr 2011
    Posts
    17
    Rep Power
    0

    Default

    You are also attempting to call a non-static method from a static context

  4. #4
    javajames is offline Member
    Join Date
    May 2011
    Posts
    32
    Rep Power
    0

    Question What to do

    Java Code:
    class classname {
        public static void mast(String[] args) {
            code_goes_here();
        }
        public static void main(String[] args) {
            mast(java.lang.String[]);
        }
    Is this correct?
    What should my arguments be? i am currently in school so don't have a compiler with me but the java.lang... is just a geuss, could i put mast(void)? I just need to define a void function (method) as in c++. Could someone give me some example code for defining and calling void methods.

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,729
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by javajames View Post
    Java Code:
    class classname {
        public static void mast(String[] args) {
            code_goes_here();
        }
        public static void main(String[] args) {
            mast(java.lang.String[]);
        }
    Is this correct?
    What should my arguments be? i am currently in school so don't have a compiler with me but the java.lang... is just a geuss, could i put mast(void)? I just need to define a void function (method) as in c++. Could someone give me some example code for defining and calling void methods.
    Nope, Java.lang.String[] can't be an actual parameter, you have to pass it an array of Strings (as is done with the main( ... ) method; i.e. args is an array of Strings. You could pass it to your mast method: mast(args); you wrote that method yourself so you should know what that mast( ... ) method wants to do with its parameter and what it expects ...

    edit: I noticed that your mast method doesn't use its parameter at all; define it as follows:

    Java Code:
    public static void mast() { ... }
    kind regards,

    Jos
    Last edited by JosAH; 05-09-2011 at 10:25 AM.
    cenosillicaphobia: the fear for an empty beer glass

  6. #6
    javajames is offline Member
    Join Date
    May 2011
    Posts
    32
    Rep Power
    0

    Default

    will try this, get back to you if it is successful thanks for your help

  7. #7
    javajames is offline Member
    Join Date
    May 2011
    Posts
    32
    Rep Power
    0

    Default

    If mast is defined without a parameter, then am i able to call it without parameter i.e:
    Java Code:
    public static void mast() { [I]code[/I] }
    [I]in main:[/I]
        mast();

  8. #8
    javajames is offline Member
    Join Date
    May 2011
    Posts
    32
    Rep Power
    0

    Default

    i will admit that i wrote that method blindly, i simply used the same format as main, but now that i know how to create a void method i understand this a lot more - so i didn't know what mast(...) wanted to do with the parameters. I have learnt a lot here thank you

  9. #9
    javajames is offline Member
    Join Date
    May 2011
    Posts
    32
    Rep Power
    0

    Default

    could i do without the 'static' part

  10. #10
    Toll's Avatar
    Toll is offline Senior Member
    Join Date
    May 2011
    Location
    Sweden
    Posts
    393
    Rep Power
    4

    Default

    What I generally do is to use a constructor, and having main() only create a new instance of the class. So for instance:

    Java Code:
      public static void main(String[] args)
      {
        new ClassConstructor(args);
      }
      public ClassConstructor(String[] args)
      {
        // Do stuff here
      }
    This means that yuo don't have to use static methods or variables anywhere in your class, unless you really need it.

  11. #11
    Join Date
    Apr 2011
    Posts
    17
    Rep Power
    0

    Default

    Quote Originally Posted by javajames View Post
    could i do without the 'static' part
    You can think of 'static' methods and fields as globals in c++, if they are declared public you can access them from anywhere in your application.
    All methods and fields that does not have the 'static' part are called 'instance' methods and fields. These methods and fields can not be accessed without creating an object of the class first.

    This might give you a better understanding about static and non-static:
    Java Code:
    public class TestClass {
        
        public static void main(String[] args) {
            /*
             * The following call is valid. Remember, static methods can be called
             * from anywhere, including other static methods!
             */
            someStaticMethod();
            
            /*
             * The following call is not valid though, since this method is an
             * instance method (it's missing the 'static' keyword).
             * Uncomment the line below (remove the //) to see what I mean.
             */
            
            //someInstanceMethod();
            
            /*
             * So how do we access these methods? The only way to access them is by
             * creating an object first. Since 'someInstanceMethod()' is part of the
             * 'TestClass' class, we have to create an object of that class to access
             * the method. We will begin by declaring a new, local field.
             */
            
            TestClass someObject;
            
            /*
             * So have we created a new object now? Nope, not yet. All we have done
             * so far is declared a new field that will point to a 'TestClass' object.
             * Here is how you create a new object and assign it to the someObject
             * field.
             */
            
            someObject = new TestClass();
            
            /*
             * Finally we can access the instance method.
             */
            
            someObject.someInstanceMethod();
        }
        
        public static void someStaticMethod() {
            //Print some random garbage to the console.
            System.out.println("Hey there!");
        }
        
        public void someInstanceMethod() {
            //Print some random garbage to the console.
            System.out.println("Oh hi!");
        }
    }

  12. #12
    javajames is offline Member
    Join Date
    May 2011
    Posts
    32
    Rep Power
    0

Similar Threads

  1. Thread problem, calling method in run method
    By majk in forum Threads and Synchronization
    Replies: 4
    Last Post: 09-27-2010, 12:40 PM
  2. Calling The main method from another method
    By SwissR in forum New To Java
    Replies: 3
    Last Post: 07-27-2010, 12:03 PM
  3. calling method from main method
    By bob_bee in forum New To Java
    Replies: 4
    Last Post: 10-02-2009, 06:30 PM
  4. Replies: 29
    Last Post: 09-25-2008, 08:55 PM
  5. Help with Calling a method
    By Albert in forum New To Java
    Replies: 3
    Last Post: 07-10-2007, 04:27 PM

Tags for this Thread

Posting Permissions

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