• 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

Converting input from string to integer

 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Reading over my code versus the solution, I'm curious. In my solution, I used
int i = Integer.parseInt( args[0] ) ;
to turn the year entered on the command line into an integer.
The other way to do this is:
int year = Integer.valueOf( args[0] ).intValue() ;
Now, I don't fully understand the above line.
It looks like valueOf( args[0] ) is getting the integer value of the year string, so what does the intValue() at the end of the line do?
thanks,
-Bryan
------------------
----
Bryan Welch
bwelch@abcv.com
 
Trailboss
Posts: 23778
IntelliJ IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're right. I'll go make the change.
Integer.parseInt( args[0] ) is a better way to go. It is much cleaner and easier to read.
Integer.valueOf( args[0] ).intValue() is the way you had to do it before JDK1.2. "valueOf()" creates an Integer object based on the string passed in. Then you use intValue() to get an "int" out of that "Integer" - wacky but true.
Extra credit for showing the teacher a better way.

[This message has been edited by Paul Wheaton (edited January 14, 2000).]
 
reply
    Bookmark Topic Watch Topic
  • New Topic