Results 1 to 2 of 2
- 04-16-2010, 04:49 PM #1
Member
- Join Date
- Mar 2009
- Posts
- 45
- Rep Power
- 0
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
- 04-16-2010, 05:00 PM #2
Senior Member
- Join Date
- Mar 2010
- Posts
- 266
- Rep Power
- 4
yeah, the compiler has a point.
as a quick fix, do this:
but you really should read up on what "static" actually means...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 }
Similar Threads
-
Calling for methods
By soccer_kid_6 in forum New To JavaReplies: 3Last Post: 02-27-2010, 09:12 PM -
Calling main method
By eva in forum New To JavaReplies: 7Last Post: 11-06-2009, 01:37 PM -
compile classes with no main type
By jan2321 in forum EclipseReplies: 2Last Post: 07-20-2009, 08:01 PM -
calling array from main
By nalinda in forum New To JavaReplies: 1Last Post: 11-17-2007, 09:41 PM -
need help calling methods
By lowpro in forum New To JavaReplies: 2Last Post: 11-15-2007, 09:53 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks