Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Failure to print because of problems with variable answer

 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 74774
336
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 74774
336
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 74774
336
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, it is in the book Introduction to Java programming (10th edition) by Y. Daniel Liang
 
Marshal
Posts: 3763
536
Android Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks never even thought of checking it. It is located in chapter three.
 
Campbell Ritchie
Marshal
Posts: 74774
336
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for the info.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic