few problems with same code..

Discussion in 'Java' started by doha786, Apr 8, 2010.

  1. doha786

    doha786 New Member

    Joined:
    Apr 7, 2010
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    hi,
    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:
    Code:
    String target = "(";
    String[] returnValue={"String", "int", "double", "void", "char", "long", "float" };
     Map<String, File> names = new HashMap<String, File>();
     
    try{ 
    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()){ 
                    result=reader.readLine();
                                    
                       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(); }
     
  2. virxen

    virxen Active Member

    Joined:
    Nov 24, 2009
    Messages:
    387
    Likes Received:
    90
    Trophy Points:
    28
    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.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice