我不明白语句中的错误((p = scan.next())!= null)。通过使用扫描仪对象我想在搜索字符串之后打印行if(p.startsWith(“START”))。 scanner对象在main中抛出java.util.NoSuchElementException。
import java.io.File;
import java.io.IOException;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.encryption.InvalidPasswordException;
import org.apache.pdfbox.text.PDFTextStripper;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
public class ReadWriteTest {
static int i;
/**
* @param args
* @throws IOException
* @throws InvalidPasswordException
*/
public static void main(String[] args) throws InvalidPasswordException, IOException {
POIFSFileSystem fs = null;
PDDocument pdDoc = null;
String target_dir = "E:\\TEST_pdfs";
File dir = new File(target_dir);
File[] files = dir.listFiles();
{
for ( int s=0;s<files.length;s++){
if(files[s].isFile()){
pdDoc = PDDocument.load(files[s]);
//fs = new POIFSFileSystem(new FileInputStream(files[s]));
PDFTextStripper Stripper = new PDFTextStripper();
String st = Stripper.getText(pdDoc);
String linesp = System.lineSeparator();
String[] paragraphs = st.split(linesp);
for(String p: paragraphs){
Scanner scan = new Scanner(p);
while((p=scan.next())!=null) {
if(p.startsWith("START"))
do{
i++;
String nextline = scan.next();
System.out.println(nextline);
}while(i<5);
}
}
}
}}}}
Error :
Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Scanner.java:862)
at java.util.Scanner.next(Scanner.java:1371)
at ReadWriteTest.main(ReadWriteTest.java:35)