Results 1 to 2 of 2
  1. #1
    counterfox is offline Member
    Join Date
    Mar 2009
    Posts
    45
    Rep Power
    0

    Default compile error - calling methods from main

    Hi all doing college question again but stuck right at the end of it >.< pretty much is a simple program to just check if two command line arguements have been made, otherwise it just prints the last command line arguement (from args[1]) The only problem I have with this is that theres already a print method made which you have to use to output args[1] so you cant simply use System.out.println()

    Heres the code:

    public class Exercise {
    public void printout(String a) { //Not allowed to change
    System.out.println(a); //Not allowed to change
    } //Not allowed to change

    public static void main(String args[]) {

    int arraysize = args.length;
    String output = "";

    if(arraysize>2) {
    output = "Wrong input";
    }
    else if(arraysize<2) {
    output = "Wrong input";
    } //End if
    else {
    output = args[1];
    }

    printout(output);



    }
    }

    the problem is the last line when i try to call printout() it gives a complile error like non-static method can be referenced from a static context. And because the question your not allowed to change that method at all.

    Thanks in advance

  2. #2
    iluxa is offline Senior Member
    Join Date
    Mar 2010
    Posts
    266
    Rep Power
    5

    Default

    yeah, the compiler has a point.

    as a quick fix, do this:

    Java Code:
    public static void main (String[] args) {
      (new Exercise()).doStuff (args);
    }
    public void doStuff (String[] args) {
      //copy-paste in here whatever's in your main() now
    }
    but you really should read up on what "static" actually means...

Similar Threads

  1. Calling for methods
    By soccer_kid_6 in forum New To Java
    Replies: 3
    Last Post: 02-27-2010, 09:12 PM
  2. Calling main method
    By eva in forum New To Java
    Replies: 7
    Last Post: 11-06-2009, 01:37 PM
  3. compile classes with no main type
    By jan2321 in forum Eclipse
    Replies: 2
    Last Post: 07-20-2009, 08:01 PM
  4. calling array from main
    By nalinda in forum New To Java
    Replies: 1
    Last Post: 11-17-2007, 09:41 PM
  5. need help calling methods
    By lowpro in forum New To Java
    Replies: 2
    Last Post: 11-15-2007, 09:53 AM

Posting Permissions

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