• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How do I turn 2 bottles of beer into 1 bottle of beer?

 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is a code taken from the Head First Java book:


When I run this, I get 99-2 bottles of beer, yada yada, but here are the last few lines:

2 bottles of beer on the wall.
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


How would I change this code so it says "1 BOTTLE of beer on the wall"?
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, the



is not really in the right place. It needs to be before the first printed line that would contain a changed word, yes? So where would that be?
 
Jake Miller
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I feel like then it is in the right place. The first changed word would be in this line:



right?
"1 bottle of beer on the wall" ??
 
Ernest Friedman-Hill
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually, it's the (very similar) line with the period, rather than the comma, that is the one where the number changes, right? The last line of each verse. Therefore, it's right before the last line that the number needs to be computed and the word reevaluated. Note that this is already where the number is being calculated; the word needs to change at the same place.
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I feel like then it is in the right place.

And yet, your output clearly shows you it is not.

walk through the code, when beerNum is 2:


this should print:

2 bottles of beer on the wall,
2 bottles of beer.
Take one down,
pass it around.



so, beerNum is now 1.

it is, so we go into this block:

and we print "1 bottles of beer on the wall".
AHA!!!
 
Jake Miller
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
woot woot, thanks a lot Fred. Very helpful.
 
Jake Miller
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry for double post, but another question. Why would the book present the code this way, that clearly is not fully correct. I mean the program runs fine, but there is this more efficient/correct way to write the script. FYI, here is the new modified script:

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

I've just reached the same point in Chapter 1.

I fixed the problem by duplicating the if (beerNum == 1) test. If you simply move the if, then the output will not print correctly if the starting point is 1 rather than 99 (more than 1).

My feeling was that the "correct" solution was a plural testing class that decided whether to use bottle or bottles at print time.

I'm just as much a Java newbie, so I don't know if there is such a thing already or whether it would be a good idea to do it that way! Clearly the class would have a run-time overhead, but it should work.

Sorry if that's confusing.

David
 
Ranch Hand
Posts: 320
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How 'bout... instead of using a word string variable

(beerNum + " " + (beerNum==1?"bottle":"bottles") + (continued...)

?
 
Jake Miller
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Bob Ruth:

(beerNum + " " + (beerNum==1?"bottle":"bottles") + (continued...)



I am not really sure what this is saying - I have not reached anything like this yet. Can you maybe elaborate on what this does?
 
Sheriff
Posts: 1367
18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Jake,

That is a sort of one-liner if statement (called a ternary operator, since it has three parts).

You can write the following



Or you can mash it together like this:



The basic pattern is


Note that I am only using parens for readability here.

Of course, this is not the ideal example, because here you can do this:



and only change the value "if something".
[ July 18, 2007: Message edited by: Katrina Owen ]
 
Katrina Owen
Sheriff
Posts: 1367
18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Jake Miller:
Why would the book present the code this way, that clearly is not fully correct. I mean the program runs fine, but there is this more efficient/correct way to write the script.



My guess is that by having to mess around a bit to find the "bug" teaches you more than just using the program as is. (It sure teaches me more, anyway!)
 
Bob Ruth
Ranch Hand
Posts: 320
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
(beerNum==1?"bottle":"bottles")

You bet, Jake.

This is a ternary operator. It's general form is (A ? B : C)
If A is true then the entire expression evaluates to B. If A is NOT true then the entire expression evaluates to C.

So, for instance, (this is just snippets not an entire class...)


int fred = 1;

System.out.println("Fred is equal to one: " + (fred==1?"TRUE":"FALSE");
fred = 2;
System.out.println("Fred is equal to one: " + (fred==1?"TRUE":"FALSE");

in the first case the value of fred is 1 so fred==1 will be true so "TRUE" will be appended to the first string.

in the second case the value of fred is 2 os fred==1 will be false and "FALSE" will be appended.

It is just a shorthand method for selecting one of two evaluations based on a logical test.

[ July 19, 2007: Message edited by: Bob Ruth ]
[ July 19, 2007: Message edited by: Bob Ruth ]
 
Jake Miller
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Katrina-
I would agree, it is very helpful to not just have to copy something and to have to work your way through it. And in looking back at the example now, I missed the first time through a little note on the bottom saying "Our example has one little flaw in it. See if you can fix it."
Bob/Katrina-
Thanks for the help on the ternary.
 
Katrina Owen
Sheriff
Posts: 1367
18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If I remember correctly, there are a couple of other instances where they say something like "oops, that didn't do what I expected". I really enjoy finding a solution to things like that.
reply
    Bookmark Topic Watch Topic
  • New Topic