Results 1 to 4 of 4
- 07-27-2010, 10:45 AM #1
Member
- Join Date
- Jul 2010
- Posts
- 39
- Rep Power
- 0
Calling The main method from another method
Hi Guys im trying to call my main method from another method. But im having trouble so far i've tried
main();
public static void main(String[] args);
public static void main();
And i keep getting compiler issues expecting semi-colons im not sure what im doing wrong.
I am calling the method from the same class.
regards S
My main method looks like this
Java Code:/** main method to find the resource name calling */ public static void main(String[] args) throws IOException { Find f = new Find("myDir", "<\\$", "=", "C:\\Documents and Settings\\Kieren McDonald\\Desktop\\Java\\Test\\filetest.txt"); }
- 07-27-2010, 10:55 AM #2
- 07-27-2010, 10:58 AM #3
Member
- Join Date
- Jul 2010
- Posts
- 39
- Rep Power
- 0
Thank you for that.
please could you explain to me why im not calling in the parameters of the method when i call it. and also if the value is null why does main(); not work???
However the compiler is still not liking the calling the compiler is throwing out several of the same 2 erros below.
Apologies is there are any formatiing errors. The forum doesn't seem to like code pasted from notepad++
at Find.main(Find.java:117)
at Find.<init>(Find.java:48)
Java Code:/** Imported Java Libraries */ import java.io.BufferedReader; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.util.ArrayList; import java.util.regex.Matcher; import java.util.regex.Pattern; import java.util.Collections; import java.util.List; import java.io.PrintWriter; import java.io.FileWriter; /** class to find string */ public class Find { BufferedReader br = null; /** method to find the value between the beginning and end of a string*/ public Find(String fileName, String begin, String end,String whereToWrite) throws java.io.IOException { try { /** read in file */ String resourceline = null; ArrayList<String> arrayList = new ArrayList<String>(); File myDir = new File("C:/Documents and Settings/Kieren McDonald/Desktop/Nick/Java/Test/resources"); if (myDir.exists() && myDir.isDirectory()) { File[] files = myDir.listFiles(); for (int i = 0; i < files.length; i++) { br = new BufferedReader(new FileReader(files[i])); while ((resourceline = br.readLine()) != null) { resourceline = find(begin, end, resourceline); if (resourceline != null) { resourceline = resourceline.trim(); boolean checkContains1=resourceline.contains("$"); boolean checkContains2=resourceline.contains("="); boolean checkContains3=resourceline.contains(">"); boolean checkContains4=resourceline.contains(":"); if (checkContains1 || checkContains2 || checkContains3 || checkContains4) { main(null); } if (arrayList.contains(resourceline)){ main(null); } } else{ arrayList.add(resourceline); } } System.out.println("/**This Is A List Of Includes Within The Component*/"); sortAndPrint(arrayList); System.out.println("/*******This Is The End Of The list Of Includes*******/"); writeResults(arrayList, "C:\\Documents and Settings\\Kieren McDonald\\Desktop\\Nick\\Java\\Test\\filetest.txt"); } } else { System.out.println("This is not a directory"); } /** * declaring string identifiers for beginning and end of string * aswel as one other line string to store the data between */ /** while line is not equal to null then find 3 string values */ } catch (FileNotFoundException ex) { ex.printStackTrace(); } finally { br.close(); } } /** method to sort and print the array collection */ private void sortAndPrint(List<String> results) { Collections.sort(results, String.CASE_INSENSITIVE_ORDER); for (String resourceline : results) { System.out.println(resourceline); } } /** method to find the value between the beginning and end of a string */ public String find(String beg, String end, String resourceline) { /** match pattern to store string between strings and match to line */ Pattern p = Pattern.compile(beg + "(.*)" + end); Matcher m = p.matcher(resourceline); return m.find() ? resourceline.substring(m.start(1), m.end(1)) : null; } /** method to print the results of the array into a new document */ private void writeResults(ArrayList<String> arrayList, String filename) { try { PrintWriter writer = new PrintWriter(filename); for (String resourceline : arrayList) { writer.println(resourceline); } System.err.println("/***Your Data Has Been Written To The File Successfully "); writer.close(); } catch (Exception e) { System.out.println("can't create output file \"" + filename + "\""); e.printStackTrace(); } } /** main method to find the resource name calling */ public static void main(String[] args) throws IOException { Find f = new Find("myDir", "<\\$", "=", "C:\\Documents and Settings\\Kieren McDonald\\Desktop\\Nick\\Java\\Test\\filetest.txt"); } }Last edited by SwissR; 07-27-2010 at 11:09 AM.
- 07-27-2010, 11:03 AM #4
You don't need visibility and return type when calling a method, but need to call with the right parameters. The main method expects a String[] so main() will not work, as a method ... void main(){... does not exist. The call with main(null) works here as it is as good as main(new String[]{}), because there is only one main method that expects an object reference.
I strongly suggest that you read some of the basic tutorials: Lesson: Classes and Objects (The Java™ Tutorials > Learning the Java Language)Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
The Ubiquitous Newbie Tips
Similar Threads
-
Calling a method
By mnki23 in forum New To JavaReplies: 21Last Post: 11-06-2009, 09:10 PM -
Calling main method
By eva in forum New To JavaReplies: 7Last Post: 11-06-2009, 01:37 PM -
calling method from main method
By bob_bee in forum New To JavaReplies: 4Last Post: 10-02-2009, 05:30 PM -
Calling a method in a different class from within a method problem
By CirKuT in forum New To JavaReplies: 29Last Post: 09-25-2008, 07:55 PM -
method calling?
By frejon26 in forum New To JavaReplies: 4Last Post: 01-25-2008, 03:38 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks