Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

String method startsWith() not working as expected  RSS feed

 
Fred Hamilton
Ranch Hand
Posts: 684
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Greetings. I am trying to write a program that reads from standard input and echoes to the monitor only those lines that begin with "//". It is a prototype for a program which will print out the comments from a java source file. The problem is that the program is echoing everything I type and not just those lines beginning with "//". Here is my code. I must be missing something obvious, but I just can't see it.

 
Stefan Evans
Bartender
Posts: 1836
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It still amazes me that one extra semi colon in the wrong place can affect so much...

 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Fred Hamilton wrote:



Your error is nothing to do with the startsWith() method. There is a stray semi-colon after your if-statement that means that the System.out.println() is not inside the if-statement.
 
Fred Hamilton
Ranch Hand
Posts: 684
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Heavy Sigh I should make a rule for myself. Whenever I am about to post to a message board concerning a debugging problem, make sure I check for the extra semicolon first; I am aware of what can happen when this mistake occurs, not sure why I missed it.

Thanks for the quick reply.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!