• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

String x is declared as a null string without any user input

 
Greenhorn
Posts: 2
Netbeans IDE Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi! I'm writing a code that'll make a binary search tree in console and I seem to be running into a problem.


The second loop or so, for loop will repeat for some reason. After some debugging, I saw that it was because that x is being registered as a null string and immediately going into the switch - case code without asking for user input, but what I don't know is how?

Here is my code down below:




If you have any ideas as to why this is happening, please let me know. Thanks!  
 
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Print out x to see what is being read in:

I don't think nextLine will return null.

Does this happen the first time round the loop, or only on later iterations?
 
Marshal
Posts: 76452
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
DT is correct; x (what a poor name for a String) isn't null. Don't go saying, “null String,” because there is no such thing, unless it is "null". You have found misleading information about what Scanner#nextLine() does. Have a look at our FAQ, whih I thinkk will help you.
 
Sven Whittenoff
Greenhorn
Posts: 2
Netbeans IDE Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Dave Tolls wrote:Print out x to see what is being read in:

I don't think nextLine will return null.



Campbell Ritchie wrote:DT is correct; x (what a poor name for a String) isn't null. Don't go saying, “null String,” because there is no such thing, unless it is "null".



Oh yeah, I keep mixing up Empty Strings with null stuffs. My bad xD.

The question still stands though. It's frustrating me to no end.

Dave Tolls wrote: Does this happen the first time round the loop, or only on later iterations?



Only in later iterations. Here's a short image of what happens in the console:
https://www.imgur.com/J6aF0OM

As you can see, after the 2nd iteration, once the code loops, it goes to "Not a valid response." without any user input.

When I debugged it, I tried to use x.isEmpty() in an if statement, and it satisfied the condition. Kinda annoying me tbh >.<
 
Campbell Ritchie
Marshal
Posts: 76452
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please tell us what your book says nextLine() does, what you have been taught nextLine() does, and what the documentation says nextLine() does. Let's see whether they are different.
 
Saloon Keeper
Posts: 9564
79
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I believe you are running into a classic Scanner gotcha. "nextLine()" reads all characters up to a new-line. IF a new-line is already pending in the queue, then nextLine() will consume that and return the String. If a pending new-line is all that is in the queue then the String that is immediately returned is an empty String. So, where does a pending new-line come from? "nextInt()" for one, consumes all digits until it encounters a non-digit. If you entered in a number followed by Enter then the non-digit is a new-line. So, now a new-line is sitting in the queue waiting to be read and along comes nextLine() which says, ah, ha, I found a new-line so therefore I have a whole line. When, in fact you haven't entered a line at all. So to fix this you have to flush out the new-line after calling nextInt() and before calling the "real" nextLine().
 
Campbell Ritchie
Marshal
Posts: 76452
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I prefer not to create a Scanner directly, but to have a utility class called KeyboardInputs (or similar) and use that. I recently edited the FAQ I linked to yesterday to include suggestions about utility classes.

And I see I put the wrong link in . I have somehow gone back to my post about the Beatles, where I suggested calling nextLine() twice. This is the FAQ I thought I had quoted.
 
a wee bit from the empire
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic