• 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
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

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: 80616
468
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic