This week's book giveaway is in the Python forum.
We're giving away four copies of Python Continuous Integration and Delivery and have Moritz Lenz on-line!
See this thread for details.
Win a copy of Python Continuous Integration and Delivery this week in the Python 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Devaka Cooray
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Tim Holloway
  • Claude Moore
  • Stephan van Hulst
Bartenders:
  • Winston Gutkowski
  • Carey Brown
  • Frits Walraven

Expanding on Assignment Java-2 (EvenOrOdd) problem  RSS feed

 
Ranch Hand
Posts: 159
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have been working on the Cattle Drive tutorials mostly as a refresher for basic Java (I took a 2 year hiatus from my learning) and decided to expand on the EvenOrOdd assignment.

I am trying to ensure the entry from the user is actually an integer and not a text string or something else.  I am having issues with the isNumber part. ("name.isNumber();") I keep getting an error that says "Cannot invoke isNumber() on the primitive type int". I have tried to move it above the parseInt line but I still get the same error.  Any suggestions?

Code listed here.
 
Sheriff
Posts: 5745
148
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To answer your question, the error text is telling you exactly what is wrong.  int is a primitive, not an Object, therefore it doesn't has any methods.  What class do you think has the method isNumber()?

But there are other problems with your code.  Look at the documentation for the Scanner class, especially the nextXXX methods.  Make sure that hasNextXXX and nextXXX are paired up.

"number" is a bad name for a Scanner object.

You're getting next from the Scanner, then testing if is has another int.

You're not using the modulus (%) operator correctly.
 
John Morgan
Ranch Hand
Posts: 159
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for the feedback... I think I got it.

(even caught the scanner memory leak ;)
 
Knute Snortum
Sheriff
Posts: 5745
148
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good job!  Now see if you can loop back and ask for input after the user has entered a non-integer.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!