Go4Expert (http://www.go4expert.com/)
-   Java (http://www.go4expert.com/forums/java/)
-   -   few problems with same code.. (http://www.go4expert.com/forums/code-t21704/)

doha786 8Apr2010 13:38

few problems with same code..
i am just trying to print all the 'method' names of java files from my folder. its not showing any error but during testing i face different kinds of problem for each output with same code..

in addition, i might mention how program suppose to work...
->it reads every files line by line and find 1st parentheses ( "(" ), if have any then get the whole line before parentheses. Then locate any return value has or not in those line, if found then the next word should be a method name for that java file.

i followed this:

Access modifier--- Return value-- Method Name (parameter...)
Return Value--Method Name()

String target = "(";
String[] returnValue={"String", "int", "double", "void", "char", "long", "float" };
 Map<String, File> names = new HashMap<String, File>();
File directory = new File("C:/JavaFiles"); // where all java files are gathered
File filelist[]= directory.listFiles();

  for (File f: filelist) {
            FileReader fr = new FileReader (f);   
            BufferedReader reader = new BufferedReader (fr);

                while (reader.ready()){
                  if (result.indexOf(target)!= -1 ){
                  String line = result.substring(0, result.indexOf(target));
                      Scanner scan=new Scanner(line);           
                      for (int i=0; i<returnValue.length; i++) {
                      word=scan.findWithinHorizon(returnValue[i], 100);
                                if(word!=null) {
              methodName=scan.next()+scan.findWithinHorizon("", 0);
                                names.put(methodName, f);
                for (String found: names.keySet()){
          File file = names.get(found);
        System.out.println(found+"\t"+"FileName: "+file.getName());
                              }// end 2nd if
                            }// end 2nd for
                        }//end 1st if
                  }//end while   
        }//end 1st for
}//end try

catch(Exception e){e.printStackTrace(); }

virxen 8Apr2010 20:13

Re: few problems with same code..

String[] returnValue={"String", "int", "double", "void", "char", "long", "float" };
this is wrong approach!

a method can return nothing if the method is declared void
and also it can return a class or even an ArrayList or Vector
not only the above you use.

Also you are looking for "(" but you can put it even for mathematics equations or for type casting and for many more reasons.

All times are GMT +5.5. The time now is 19:30.