Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
206 views
in Technique[技术] by (71.8m points)

java - read exactly a string in the next line after maching the condition

I have this simple and I am trying to find the string all and after finding it it should jump to next line and look for the integer value 30 and store it in Integer variable.

Simple:

spoon 05:09 05:39 06:11 06:41 07:11 07:41 08:11 17:11 17:41 18:11 18:41 19:11 19:41
apple 05:09 05:39 06:11 06:41 07:11 07:41 08:11 all 17:11 17:41 18:11 18:41 19:11 19:41
chair 05:11 05:41 06:14 06:44 30 07:14 07:44 08:14 17:14 17:44 18:14 18:44 19:14 19:44
table 05:13 05:43 06:17 06:47 07:17 07:47 08:17 Min 17:17 17:47 18:17 18:47 19:17 19:47

Code:

        Scanner scanner = new Scanner(file)) {
    int lineNum = 0;
    while (scanner.hasNextLine()) {
        String line = scanner.nextLine();
        lineNum++;
        if(line.contains(" all ")){
            //go to the next line and find the integer value 30 then store it.
            System.out.println("I found it on line " +lineNum);



        }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can do something like this:

Scanner scanner = new Scanner(file)) {
int lineNum = 0;
while (scanner.hasNextLine()) {
    String line = scanner.nextLine();
    lineNum++;
    if(line.contains(" all ")){
        scanner.nextLine(); // this will skip to the next line
        System.out.println("I found it on line " +lineNum);
        // look for the integer value 30 and store it in Integer variable.
    }

Basically all you have to do is once the line contains the all word then you call scanner.nextLine() which will bring you to the next line and then after that you can look for 30 and then store it. Once you're finished if there's nothing left for you to do then you can call break; to exit the while loop


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...