简体   繁体   中英

how to fix error java.util.NoSuchElementException

the following which given belo this giving me that error how to fix it

String getNextStringStartsWith(String str) {
    
    // TODO Implement method
    Iterator<String> iterator = lines.iterator();
    while (iterator.hasNext() && !iterator.next().equals(str)) {
        iterator.next();
    }
    if(iterator.hasNext()){
        return iterator.next();
    }else{
        
        return null;
    }
}

I didn't actually understand the logic that you are trying to implement, but below could be your code, if you want to search a str in lines , and return str if found, else null :

String getNextStringStartsWith(String str) {
      // TODO Implement method
      Iterator<String> iterator = lines.iterator();
      while (iterator.hasNext()) {
          String data = iterator.next();
          if (str.equals(data)) {
             return data
          }
       }
       return null
}

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