Win a copy of Node.js Design Patterns: Design and implement production-grade Node.js applications using proven patterns and techniques this week in the Server-Side JavaScript and NodeJS forum!
  • 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
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Editing beerbottle java to include user input

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


just starting to work on this 99 beer bottle task. Whilst not the prettiest it runs and generally outputs what I want. How would I consider changing it to include a user input when the user can input what number to start at, instead of 99 after if I imported the scanner util? Thanks

 
Marshal
Posts: 74084
332
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

Have you found the documentation for Scanner? There is an example just like what you want near the beginning of that documentation.
 
Dave Murph
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Welcome to the Ranch

Have you found the documentation for Scanner? There is an example just like what you want near the beginning of that documentation.



Thanks, I did some tweaks and by adding in scanner snippet and made some progress

It will ask for input as below but then still start off at 99

***********
How many bottles are on the wall:
5
Beer Song

-----------
99 bottles of beer on the wall
99 bottles of beer
Take one down.
etc
etc

********

 
Campbell Ritchie
Marshal
Posts: 74084
332
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are reading the input correctly, but because you applied it to a different variable, the number used in the song is still 99.
 
Dave Murph
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:You are reading the input correctly, but because you applied it to a different variable, the number used in the song is still 99.



Thanks for the reply - still gone a bit code blind and not seeing the errors of my ways, I will keep tweaking until its straight in my head!
 
Campbell Ritchie
Marshal
Posts: 74084
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, don't tweak the code. Read it out. Copy it onto a sheet of paper. Count the variables with your fingers. Or any combination of the preceding, until you work out what I meant. Then you will be able to correct it is a few seconds, and you will have learnt lots about reading code.
 
Dave Murph
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:No, don't tweak the code. Read it out. Copy it onto a sheet of paper. Count the variables with your fingers. Or any combination of the preceding, until you work out what I meant. Then you will be able to correct it is a few seconds, and you will have learnt lots about reading code.



Thanks ! With a bit of patience I managed to get it working as below - still a bit messy and still not sure why I have an extra line of " x bottles of beer on the wall" in the output that appears after the linebreak

How many bottles are on the wall:
12
Beer Song

-----------
12 bottles of beer on the wall
12 bottles of beer
Take one down.
Pass it around.

11 bottles of beer on the wall
11 bottles of beer on the wall
11 bottles of beer
Take one down.
Pass it around.

10 bottles of beer on the wall
10 bottles of beer on the wall
10 bottles of beer
Take one down.
Pass it around.



 
Campbell Ritchie
Marshal
Posts: 74084
332
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You appear to be changing “bottlesȁ to “bottle” one line too late; go through the loop and work out where the best place for that change would be. Otherwise I can't see anything wrong with the output.
Don't use \n; use the printf() method and %n for line end.
 
Marshal
Posts: 16598
278
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To simplify and eliminate duplication, you could define a method and use a MessageFormat object:

Usage would be something like this:

See the following:
https://docs.oracle.com/javase/tutorial/i18n/format/choiceFormat.html
https://docs.oracle.com/javase/8/docs/api/java/text/ChoiceFormat.html
https://docs.oracle.com/javase/8/docs/api/java/text/MessageFormat.html
 
Dave Murph
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:You appear to be changing “bottlesȁ to “bottle” one line too late; go through the loop and work out where the best place for that change would be. Otherwise I can't see anything wrong with the output.
Don't use \n; use the printf() method and %n for line end.



Thanks - it seems I had the new line separator in the wrong spot - format now is much better , thanks all for the assistance.

2 bottles of beer on the wall
2 bottles of beer
Take one down.
Pass it around.
1 bottles of beer on the wall.

1 bottle of beer on the wall
1 bottle of beer
Take one down.
Pass it around.
No more bottles of beer on the wall
 
Campbell Ritchie
Marshal
Posts: 74084
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Dave Murph wrote:. . . Thanks . . ..

That's a pleasure
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic