• Post Reply Bookmark Topic Watch Topic
  • New Topic

Failure to print because of problems with variable answer  RSS feed

 
Lovro Bajc
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I didnt want to pen new topic so I was hoping if you can help me here I have been going though book and the exercises they have, where I came accross a problem with boolean. Code in the book is written exactly like this:



however the true/fals result can not be printed due to the "answer" variable...Is it me or the book wrong?

thanks
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You ought to have started a new thread. That is a completely new question. Fortunately I can create a new thread.
You ought also to have given more details of what happens. What you wrote suggested you were not getting the answer printed, whereas I got a compile time error when I tried your code. You have not declared the variable answer anywhere. It would have worked if you had declared answer.

There are a few minor formatting errors in your code, but one serious logic error. You are closing a Scanner pointing to System.in. Other people have made the same mistake and had problems, for example in this thread.
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You wrote number when you actually should have written answer.
 
Lovro Bajc
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the info and help. It was Type-o in a book I am reading

 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome

If it is an error in the book, did you find it in the book's errata webpage?
 
Lovro Bajc
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, it is in the book Introduction to Java programming (10th edition) by Y. Daniel Liang
 
Ron McLeod
Bartender
Posts: 1603
232
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Errata for the book can be found here.

Page 77, line 14 in Listing 3.1, change number to answer.
 
Lovro Bajc
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks never even thought of checking it. It is located in chapter three.
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for the info.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!