0

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

1
  • Consider using StringBuffer or StringBuilder. Commented Feb 4, 2014 at 23:11

1 Answer 1

2
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)
Sign up to request clarification or add additional context in comments.

8 Comments

thank you are right but still i can't get it work because of the line i need to tell java that when ever is the file not empty and that the line have the mark ":" substring it but line as refrense is wrong
can you tell me how to fix it plz i am looking around for two days
No, not if you don't provide me the exact error message.
The line is probably not really 8 characters long. You are trying to take a substring which is larger than the original string.
Let me be Sherlock today: your condition inside the while loop should probably be an if. Try: while((line = ...) != null) { if (line.indexOf(':') != -1) { ... } }.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.