• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Bear Bibeault
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • salvin francis
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Frits Walraven
Bartenders:
  • Jj Roberts
  • Carey Brown
  • Scott Selikoff

Trying to understand the concept of collecting multiple users inputs

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, my name is Jon. Hope everyone is doing well during these times. Still learning Java, but it feels like every concept takes a very long time to sink in.

If it's no trouble, I wanted to try to understand getting inputs from different users to print them all afterward. It's for an assignment, and just curious if I could get sent to the right path. Not trying to get it solved, I'm just not sure how to go about it or the thought process.

I only understand how to take, store and print for 1 user, but lost on how to add 2 more.

Apologies for the trouble and hopefully this makes sense.

 
Bartender
Posts: 7804
70
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
Welcome to the Code Ranch Jon.

We need some more info to go on. Is your program interface web based, text based, or GUI based? What are you currently using to save the user responses: a text file, a database, or something else?
 
JonP Lee
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ah, sorry Carey. I'm just using NetBeans if that's what you mean. Not sure if this clears it up.

       
 
Master Rancher
Posts: 4194
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Do you know how to define and use your own classes?  Your program could define a class to hold all the input from a user and then save each instance of that class with a user's data in a list.
After getting the input from all the users, the program could go through the list and print out the data for each user.
 
Carey Brown
Bartender
Posts: 7804
70
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
If you are going to accept spaces in the name then for the name you'll have to call nextLine() instead. Unfortunately when you do that you will run into one of the infamous Scanner gotchas, the left over new-line from nextInt() which would cause name to be empty. Here's how to fix that problem.
       
 
JonP Lee
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Norm Radder wrote:Do you know how to define and use your own classes?  Your program could define a class to hold all the input from a user and then save each instance of that class with a user's data in a list.
After getting the input from all the users, the program could go through the list and print out the data for each user.



Sorry, do you mean creating a new class? Would I create an array to store the data?
 
JonP Lee
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ah.. Thanks Carey. I completely overlooked that part about the nextLine.

Carey Brown wrote:If you are going to accept spaces in the name then for the name you'll have to call nextLine() instead. Unfortunately when you do that you will run into one of the infamous Scanner gotchas, the left over new-line from nextInt() which would cause name to be empty. Here's how to fix that problem.
       

 
Carey Brown
Bartender
Posts: 7804
70
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
More likely that you'd use a List instead of an array because a List can keep track of its own size and expand and contract as needed.
 
JonP Lee
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:More likely that you'd use a List instead of an array because a List can keep track of its own size and expand and contract as needed.



Thank you very much Carey. Hope my post wasn't too vague.
 
Carey Brown
Bartender
Posts: 7804
70
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
Jon, when replying to a post don't use Quote unless you are trying to point out something specific in the message that you're replying to. Using Quote all the time just fills the thread with redundant information. Thanks.

[Edit:]To reply to the last post, look below the last post, just to the left, and you should see a "Post Reply" button.
 
Marshal
Posts: 72088
312
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

The reason for that problem is that the nextLine() method isn't well named and so many of the book authors believe its name reflects its functionality; this API link tells you what it actually does. Please tell us what your book says it does.
What happens after nextInt()‑<line‑terminator> is that the Scanner reads the remainder of the line, which in this case contains no useful information, and after that all your inputs are out of sequence, just like what Carey said. It is only a matter of time before you can't match an input, and suffer an exception. I used to call nextLine() twice, but have since hit on a better solution: create a utility class. You can now write KeyboardInputs.nextLine() and even use Scanner's ability to read ahead. The following code will happily handle input like this:-

123 JonP Lee

...and this is an example of how you can write a nextLine() method:-Pre-Java11 code used trim() instead of strip(). The message would read something like, “Please write name‑<enter>: ” Remember that unlike many Scanner methods, nextLine() is dependent on a line end sequence being used and its input depends on the location of that line end.
 
Carey Brown
Bartender
Posts: 7804
70
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
Campbell, I'm uncomfortable with your approach to handling extraneous new-lines in nextLine(). What if an empty String is actually a valid input? In Jon's case your approach may be acceptable but I'd hate to adopt it as the universal answer.
 
Campbell Ritchie
Marshal
Posts: 72088
312
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Good point. My utility class misses out certain functionality. For example it doesn't provide any technique for changing the delimiter on the Scanner. It never occurred to me to accept an empty line as a valid input. As you imply, that would require a different approach.
 
Slideshow boring ... losing consciousness ... just gonna take a quick nap on this tiny ad ...
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic