• Post Reply Bookmark Topic Watch Topic
  • New Topic

Using a while loop  RSS feed

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a program that I use a while loop in. I have it set to break when a certain input is typed in. How can I set it for the program to start over if that certain input is typed in? I currently have the break set for null, blank, or when # is typed. I want to change it so that when "#" is typed the program will start over. Any suggestions?

 
Saloon Keeper
Posts: 3330
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

See **** comments.
Please clean up your indentation.
No need to test for null, nextLine() shouldn't return a null.
 
Brett Banich
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the response.
 
Sheriff
Posts: 22846
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:No need to test for null, nextLine() shouldn't return a null.


And not only that:



If nextLine() did return a null, then trying to convert it to lower case would throw a NullPointerException. Likewise "message.equals(null)" would throw a NullPointerException if the message variable was null. That exception is always thrown when your code tries to call a method of a variable which contains a null reference.
 
Marshal
Posts: 56608
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The definition of the equals method is that it never returns true if either operand is null. If the left operand is null, it throws an exception, and if the right operand is null it returns false. If you need to test for nullity use
if (message == null) ...
It is often better to prevent a null getting into your method in the first place by throwing an exception if you are presented with one.
Also don't test a String for equality with "". Use this method instead.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!