how to handles if-else in groovy?

别等时光非礼了梦想. 提交于 2019-12-24 23:08:09

问题


Here I'm passing the skipfolders variable in input if the skipfolders is true then it prints all files from Parent path and skip the sub folders. otherwise it returns all files from all folders include sub folders as well. Here I wrote if-else conditions. When I execute this code in FileMaker it executes without any errors and displayed result.but the if-else conditions does't working here.

Problem :

If-else conditions doesn't working here.it prints all files from FTP include subfolders.skipfolders condition doesn't working. at this return allFiles.join('\n') + '\n'+ allFolderFiles.join('\n')+ '\n' prints directly and skipfolders condition doesn't working. Please help how to use if-else conditions properly in fileMaker groovy.

start()
def start(){

        boolean skipfolders = false
        def store;
        def ftpClient = new FTPClient()
        ftpClient.connect(server)
        // println(ftpClient.replyString)
        ftpClient.login(user,pass)
        ftpClient.enterLocalPassiveMode()
        FTPFile[] fileslist = ftpClient.listFiles("/")
        FTPFile[] folderfileslist = ftpClient.listFiles("/sample")

  if(skipfolders == false){

       def allFiles = []; 
       for(int i=0; i<fileslist.length; i++){  
       String file_name = fileslist[i].getName()
       String file_timestamp = fileslist[i].getTimestamp().getTime()     
       String s = '|' + file_name+ '|' + '/' +file_name+'|'  +file_timestamp
       allFiles << s       
   }  
      def allFolderFiles = [];
      for(int i=0; i<folderfileslist.length; i++){
      String folderfile_name = folderfileslist[i].getName()
      String folderfile_timestamp = folderfileslist[i].getTimestamp().getTime()
      String s1 = '|' +folderfile_name+ '|' + '/sample' +'|'+folderfile_name+'|'  +folderfile_timestamp
      allFolderFiles << s1
 }
  ftpClient.disconnect()
  return allFiles.join('\n') + '\n'+ allFolderFiles.join('\n')+ '\n'

}
else{
       def allFiles = []; 
       for(int i=0; i<fileslist.length; i++){  
       String file_name = fileslist[i].getName()
       String file_timestamp = fileslist[i].getTimestamp().getTime()     
       String s = '|' + file_name+ '|' + '/' +file_name+'|'  +file_timestamp
       allFiles << s       
   }  
   ftpClient.disconnect()
   return allFiles.enter code herejoin('\n')
  }
}

    enter code here

if anybody having idea please let me know thanks.

回答1:


If i am understanding the question correctly, you want to set the skipfolders variable as a parameter.

As it is you have declared:

boolean skipfolders = false;

So the else is never reached as mentioned by user daggett in the question comment.

If you do something like this instead:

start(true) or start(false)
def start(boolean input){
    boolean skipfolders = input;
    ...
}

Then you can reach the else statement depending on your input.




回答2:


The way you are comparing is preferred for String value, for Boolean value you could do as follows also in your code if..else opening and closing doesn't seems matching, please check that.

  boolean skipfolders = false
  if(!skipfolders){  // this directly checks the true and false conditions
     // do your stuff here on skipfolders = false
  }else {
     // do your stuff here on skipfolders = true
  }


来源:https://stackoverflow.com/questions/49848508/how-to-handles-if-else-in-groovy

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!