简体   繁体   中英

while loop needs to continue

I have this code

static String sCurrentLine = null;

 /* keyword */
static String keyword = null;

 Scanner keywordFile = null, siteFile = null;
           try {
             keywordFile = new Scanner(new File("/home/mearts/keywords.txt"));
             siteFile = new Scanner(new FileReader(fileChooser.getSelectedFile()));
             sCurrentLine = siteFile.nextLine().trim();
             keyword = keywordFile.nextLine().trim();

               while (sCurrentLine != null){
                 while (keywordFile.hasNext() || keyword == null) {
                 System.out.println("Line--> " + keyword);
                 System.out.println("Current here >>" + sCurrentLine);
                   if (sCurrentLine.contains(keyword)) {
                       System.out.println("Found it-->> " + keyword);
                       keyword = keywordFile.nextLine();
                       System.out.println("next keyword " + keyword);
                       ///* reset search to top of site file */
                       siteFile = new Scanner(new 
                         FileReader(fileChooser.getSelectedFile()));
                         sCurrentLine = siteFile.nextLine().trim();
                   }
                   else {
                     sCurrentLine = siteFile.nextLine();
                     if (sCurrentLine == null) {
                       break;
                     }
                     if (!sCurrentLine.matches(keyword)){
                     System.out.println("The following keyword " + keyword + " does not exist in file "
                         + fileChooser.getSelectedFile());
                     }
                   }
                }  //2nd while loop
               }
           }
           catch (FileNotFoundException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
          }
           siteFile.close();
           keywordFile.close();
    }

and i have a text file called keywords which has a list of keywords in it, but my logic is off an I cannot figure out why. I think I may need to run the loop one last time but not sure how to do that My issue is that the last word in the keyword file never gets read in. so the program stops at the 2nd to last element in the text file.

I am not sure that I understand what your code should do.

If I understood correctly your code, your task is to read keywords from a file with keywords and then find all keywords in another file. Is it correct?

You should separate reading keywords from the file and search for them in the file. You should 'load' keywords in a list and then search through the file.

To load keywords in list

keywordFile = new Scanner(new File("/home/mearts/keywords.txt"));
List<String> keywordsList = new ArrayList<>();
while (keywordFile.hasNextLine()) {
    keywordsList.add(keywordFile.nextLine());
}

And to search for keywords in the file

siteFile = new Scanner((Readable) new FileReader(fileChooser.getSelectedFile()));
while (siteFile.hasNextLine()) {
    String sCurrentLine = siteFile.nextLine().trim();
    for (String keyword : keywordsList) {
        if (sCurrentLine.contains(keyword)) {
            System.out.println("Found it-->> " + keyword);
            break;
        }
    }
    System.out.println(
            "The following keyword " + keyword + " does not exist in file " + fileChooser.getSelectedFile());
}

I hope this will help :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM