user1833393
user1833393

Reputation: 11

want to use startwith( ) method as !startwith( )?

I am trying to read strings from a text file that looks like this:

 # hello my name is captain
1111 $3340 4
1211 $9182 5
1211 $9192 9
if(!line.startsWith("#")) {
    System.out.println(line);
}

This prints out everything except the #:

 hello my name is captain
1111 $3340 4
1211 $9182 5
1211 $9192 9

I cant find any examples that use the ! symbol and cant tell what I'm doing wrong.

Upvotes: 1

Views: 154

Answers (1)

Sri Harsha Chilakapati
Sri Harsha Chilakapati

Reputation: 11950

Try this. The error in your code contains a space before the comments. So check trimming it.

BufferedReader reader = new BufferedReader(new FileReader(new File("path-to-file")));

String line;

while ((line = reader.readLine()) != null){
    if (!line.trim().startsWith("#")){
        System.out.println(line);
    }
}

Upvotes: 1

Related Questions