質問

i am trying to use String method inside while loop so i can read part of the file the code is this

   package AnimeAid;

import java.io.*;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

public class ReadFile {


    public void getFileInformation() throws IOException {   

        try{
        String file;
        file = "tra.srt";
        Charset charset = Charset.defaultCharset();
        Path path = Paths.get(file);
        BufferedReader reader = Files.newBufferedReader(path, charset);
        System.out.printf("Lines from %s:%n",file);
        String line;
        int num = 5;
        while((line = reader.readLine()) != null && line.indexOf(':') != -1){
            System.out.println(line.substring(0, 10));


        }
        }catch(FileNotFoundException ex){
        System.err.println(ex);
        }
    }
}

now it not give me any error message but it is not giving the right answer and i need to use return not system.out.print

       Lines from tra.srt:
BUILD SUCCESSFUL (total time: 0 seconds)

how file look like

 1
00:00:01,600 --> 00:00:04,080
<b>Mr Magnussen, please state your
full name for the record.</b>

2
00:00:04,080 --> 00:00:07,040
Charles Augustus Magnussen.

the output most be the timing 00:00:01,600 from every line how he know by looking for every : sign

役に立ちましたか?

解決

line.indexOf(':')

returns an integer. This means you have while (boolean && int) which makes no sense in a Java world. You should compare the result of the indexOf method in order to get there a boolean as well.

Eg:

while((line = reader.readLine()) != null && line.indexOf(':') != -1)
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top