• Post Reply Bookmark Topic Watch Topic
  • New Topic

Problem with break()  RSS feed

 
Emilian Chmiel
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

What is the purpose of the break() method? And can you show us the code for it, or at least where is it declared?

Henry
 
Devaka Cooray
Marshal
Posts: 5649
765
Chrome Eclipse IDE Google App Engine IntelliJ IDE jQuery Postgres Database Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
'break' is a Java keyword, so you can't use it as a method name. Maybe you are talking about a break; statement?
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How about using return; instead?

You cannot have a method named break, because break is a Java keyword and methods cannot have names that are the same as keywords.
 
Liutauras Vilda
Sheriff
Posts: 4917
334
BSD
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Emilian Chmiel wrote:I can't use break(), so how can I fix it without using System.exit(0)?
Hi Emilian. 'break' keyword can be used either in a loop construct or switch statement. You don't have neither in your code.

Could you please explain the goal in general what are you trying to accomplish?
 
Liutauras Vilda
Sheriff
Posts: 4917
334
BSD
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And a warm welcome to the Ranch.
 
Emilian Chmiel
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK
I see it now
sorry for truble
 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome again
Avoid System.exit, which is not safe in multi‑threaded environments.
I think the most elegant way to come out of a loop would be to give it a condition including a test that the StringBuilder object is not empty.But you don't have a loop; you are recursing. You don't want an exit instruction. You want a base condition which is so simple that you can go no further down from it. I suggest you remove the line about exit, and wrap the whole of the method body including the recursive call in some condition. It can be really simple (in fact for a base case it shou‍ld be simple), like this:-
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!