• Post Reply Bookmark Topic Watch Topic
  • New Topic

very simple question  RSS feed

 
Adam Chalkley
Ranch Hand
Posts: 518
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was bored so I just decided to make an ultra basic program to enter a name and print the name to screen while getting the program to loop while the name was not blank(null)

but for some reason this easy program actually backfired on me and when I used the while condition for some reason its pretty much looping weather the name variable is null or not,how do I fix this and why do I actually get this error :s




Thanks guys =)
 
Tim Cooke
Marshal
Posts: 4043
239
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A good reason for you to AvoidTheEqualityOperator <-- read the link and you'll understand why your condition never equates to true.
 
Tim Cooke
Marshal
Posts: 4043
239
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually, I'm posting nonsense again.

If you input nothing, does input.nextLine() return null or an empty String?
 
Adam Chalkley
Ranch Hand
Posts: 518
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tim Cooke wrote:Actually, I'm posting nonsense again.

If you input nothing, does input.nextLine() return null or an empty String?


I'm guessing a empty string,hmmm still abit confused here lol

thats what I originally thought though so to combat I wrote while(name != ""); (empty string)

but it the problem was still the same basically.
 
Tim Cooke
Marshal
Posts: 4043
239
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your System.out will tell you. Do you output "hi" (name is empty String) or "hinull" (name is null)?

If it's an empty String, then (name != null) is true, and you go round the loop again.
 
Liutauras Vilda
Sheriff
Posts: 4917
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Actually with method in.nextLine() impossible to get null pointer. So you need to check for an empty String.
 
Tim Cooke
Marshal
Posts: 4043
239
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well now, (name != "") is another issue entirely. One where the advice is to AvoidTheEqualityOperator <-- read this link
 
Adam Chalkley
Ranch Hand
Posts: 518
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tim Cooke wrote:Well now, (name != "") is another issue entirely. One where the advice is to AvoidTheEqualityOperator <-- read this link


Thats the very true I forgot strings can't use the equals operator like int,double,char and other primitive types


is there a not equals method for strings ie as in (while name!equals("")); ?
 
Liutauras Vilda
Sheriff
Posts: 4917
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
They can, but it gives you the results not the ones you expect (at least now).
When you compare objects with "==", it checks are these two references refer to the same object in memory location.
 
Tim Cooke
Marshal
Posts: 4043
239
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's because String is not a primitive type. it's a descendant of Object.

Seeing as though Liutaurus has spoiled the learning and discovery fun by giving you the answer, I shall modify it slightly and give you my top tip for handling null Strings when comparing.

Let's say in your code, name could be null. To check for a non empty String you'd need to do something like this:

However, you can simplify this by reversing the .equals(), like so:

If name is null, then the condition is false. A NullPointerException is not possible here.
 
Liutauras Vilda
Sheriff
Posts: 4917
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tim Cooke wrote:Seeing as though Liutaurus has spoiled the learning and discovery fun by giving you the answer

Well, you're right about that, my fault, sorry about that.
 
Adam Chalkley
Ranch Hand
Posts: 518
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Liutauras Vilda wrote:
Tim Cooke wrote:Seeing as though Liutaurus has spoiled the learning and discovery fun by giving you the answer

Well, you're right about that, my fault, sorry about that.


quite tricky but I'll get it ;p haha thanks guys
 
Adam Chalkley
Ranch Hand
Posts: 518
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tim Cooke wrote:That's because String is not a primitive type. it's a descendant of Object.

Seeing as though Liutaurus has spoiled the learning and discovery fun by giving you the answer, I shall modify it slightly and give you my top tip for handling null Strings when comparing.

Let's say in your code, name could be null. To check for a non empty String you'd need to do something like this:

However, you can simplify this by reversing the .equals(), like so:

If name is null, then the condition is false. A NullPointerException is not possible here.


(!"".equals(name)) why would you use the not operator? why not just use (!"".equals(name)) isn't that essentially saying you want the opposite done?
 
Tim Cooke
Marshal
Posts: 4043
239
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

This says: "do while name not equal to empty string". Which is what you want right?
 
Tim Cooke
Marshal
Posts: 4043
239
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Liutauras Vilda wrote:sorry about that.

Don't sweat it, no big deal. I've forgotten about it already
 
Adam Chalkley
Ranch Hand
Posts: 518
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tim Cooke wrote:
This says: "do while name not equal to empty string". Which is what you want right?


indeed just the order confused me haha
 
Carey Brown
Saloon Keeper
Posts: 3315
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Adam Chalkley wrote:
Tim Cooke wrote:
This says: "do while name not equal to empty string". Which is what you want right?


indeed just the order confused me haha
or while( name.length() > 0 )
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you are using a Scanner that reversal may be unnecessary. The only way you can get null from a Scanner's nextXXX methods is if you push the end‑of‑file keys (ctrl‑D on Linux/Unix/Mac or ctrl‑Z on Windows®). Otherwise it will return something, even if only the empty String; you can get the empty String if you push the enter key too soon.

If you use nextLine after nextAnythingElse, you can get an empty String returned. Look here for any explanation. You may prefer
... while(input.trim().isEmpty());
… which will catch cases where you only enter whitespace. In that case the null check might become necessary again.
 
Liutauras Vilda
Sheriff
Posts: 4917
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:while(input.trim().isEmpty());
… which will catch cases where you only enter whitespace. In that case the null check might become necessary again.

Even that it doesn't cause null pointer, just an empty String.
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you push ctrl‑Z you might have null returned from nextLine. I have never tried it, but I shall and shall report back.
 
Liutauras Vilda
Sheriff
Posts: 4917
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:If you push ctrl‑Z you might have null returned from nextLine. I have never tried it, but I shall and shall report back.

I missed that part of your post, I am sorry.
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
java Username
enter your name
    [enter]
hi
enter your name
    [enter]
hi
enter your name
[enter]
hi
enter your name[ctrl‑D]
Exception in thread "main" java.util.NoSuchElementException: No line found
at java.util.Scanner.nextLine(Scanner.java:1540)
at Username.main(Username.java:13)
So you can never get null however hard you try because you get a different Exception if the stream for input is empty
So the null check is not required and LV is correct.
 
dhrubo bhattacharjee
Greenhorn
Posts: 23
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can try :

 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!