• 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

Head First Java - Dot com game, Query on Integer.parseInt

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

I am new to Java and trying to learn java from Head First book chapter 5, In this book code has written to create simple dotcom game. Basically it is doing following

1. Create grid of simple array
2. Assign 3 continous locations in a array
3. Accept guess from user
4. If location allocated = guess then give message "hit" otherwise miss and accept another guess from prompt
4. If all locations = guesses then give message "kill". otherwise loop and accept guess from user.
...etc

In one of the places it is doing

int guess = Integer.parseInt(stringGuess);

I have not understood the purpose of converting string to numbers ?

My understanding is that it is accepting "guess" from prompt and which will be always integer, so to convert from strings to integer it requres parseInt.

Pl help me in my understandings.

thanks & regards
java-boy


 
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey,

whatever comes from the console is always string. It's up to you to make the necessary conversions.

Since you work with an int array you need to coerce the string that came in to an int.
 
Parag Patankar
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

"anything on console is always "string",

Thanks for clearing my concept.

best regards
java-boy
 
Marshal
Posts: 79507
379
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Maybe worthwhile updating that example by using java.util.Scanner for your keyboard input.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic