0

I'm working on a project, and I need to skip all lines that begin with '"'. I have this code:

 String line = reader.readLine();
 boolean containsChar;
 try { containsChar = line.charAt(0) != '\"'; }
 catch (Exception ex) { containsChar = true; }
 while (line != null && containsChar) {
     line = reader.readLine();
     try { containsChar = line.charAt(0) != '\"'; }
     catch (Exception ex) { containsChar = true; }
 }

I used to have a short piece of code, but that would detect any line which contains a '"' anywhere in the line:

while (line != null && !line.contains("\"")) { line = reader.readLine(); }

I had a short way of doing it, but it is possible for a line to be empty. In this case the code returns an exception:

while (line != null && line.charAt(0) != '\"') { line = reader.readLine(); }

Question: Is it possible to have code similar to the code below, where the try-catch is inside of the while test?

while (line != null && (try { line.charAt(0) != '\"'; }catch (Exception ex) { true; })) { line = reader.readLine(); }

Solution: In one of the comments Thilo gave a solution which works with a char at any index (in this example index 12):

( line.length() > 12 && line.charAt(12) == something)
Animiles
  • 97
  • 7
  • what exception is thrown? why wouldn't you be able to have a try-catch inside a loop? – Stultuske Jan 08 '18 at 10:57
  • 1
    Possible duplicate of [How to check a string starts with a substring or not in java?](https://stackoverflow.com/questions/22802234/how-to-check-a-string-starts-with-a-substring-or-not-in-java) – Bernhard Barker Jan 08 '18 at 11:14
  • Also, I suppose [What is IndexOutOfBoundsException? how can i fix it?](https://stackoverflow.com/q/40006317) (although you didn't tell us which exception you're getting) – Bernhard Barker Jan 08 '18 at 11:17
  • @Stultuske An indexOutOfBoundsException. But with any exception I want it to return True. I could do it inside of the loop, but I'm a student and I want to learn short ways to write code. And I quite regularly have a similar problem to this where a test-function can return an exception. – Animiles Jan 08 '18 at 11:38
  • your method doesn't "return" an Exception, it "throws" one. why not checking for null of line? – Stultuske Jan 08 '18 at 11:40
  • @Stultuske I am already checking if the line is null. But the line is "". In the comments of the accepted answer I found a good solution. Thilo said: ( line.length() > 12 && line.charAt(12) == something) This allows me to check any char if it exists :) – Animiles Jan 08 '18 at 11:43

1 Answers1

1

I think you want String#startsWith

while (line != null && !line.startsWith(quote))
Thilo
  • 257,207
  • 101
  • 511
  • 656
  • This indeed helps me in this case. Thank you. But what if, for instance, I want to check for a character at index 12? – Animiles Jan 08 '18 at 11:32
  • 1
    `( line.length() > 12 && line.charAt(12) == something)` – Thilo Jan 08 '18 at 11:34
  • 1
    In Java, you cannot use `try` as an expression. You'd need to move the code into a helper method. Or use Scala :-) – Thilo Jan 08 '18 at 11:36