Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
  • 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Console Input of strings using Scanner question

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am having a great deal of frustration trying to use the scanner to get user input from the eclipse console.
Here is what I am trying to do.
Print a menu for the user with options, take that (1) char input and run it into a switch statement for processing.
I have that done and it is working fine.

If the user chooses to enter strings for storage, They are instructed to enter their string and press enter to complete that string entry.
Then enter the next string and press enter, etc.
So I have a While loop for this. Get the scanner input, store it in the LinkedList, get the next scanner input, etc.
I get the scanner string and store it in a Linked list. That works fine.
The user is instructed to simply enter a blank string to end the entry procedure, like just press Enter on the new line without typing a new string.
The problem is the scanner doesn't seem to return anything for me to Test to close this procedure.
How do I TRAP the fact that the user just pressed enter so I can end my procedure?
I have tried next() and nextLine() and reset(), etc. And I am getting knowhere.

Thanks,

*Dave
 
Sheriff
Posts: 8271
592
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi David,

Maybe just post what you got so far and someone will help.
 
David A King
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Marshal
Posts: 75676
354
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Scanner??? Yesyesyesyes.
Scanner.nextChar??? Nonononononono.

At this point you start getting confused. You would like to use a Scanner#nextChar method but there is no such thing, even though there seem to be lots of Scanner#nextXXX methods. I think the idea was that a Scanner divides input into “tokens” and you can't necessarily have a char as a token. The nearest you can get to that is to use myScanner.next() to get the next token and then it is a String. Remember Strings have a method which gives you the char at a particular location. Remember that String indices are related to array indices, so they start at 0. Unless you do something silly with your Scanner, you cannot get an empty String from Scanner#next, so every token you get will have a “first” character.
 
Campbell Ritchie
Marshal
Posts: 75676
354
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

A few minutes ago, I wrote: . . . you cannot get an empty String from Scanner#next . . .

You can however get an empty String with Scanner#nextLine, so if you ever have problems with nextLine do ask. I know the solution.
 
Can't .... do .... plaid .... So I did this tiny ad instead:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic