• Post Reply Bookmark Topic Watch Topic
  • New Topic

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

 
Parag Patankar
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • 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


 
Sebastian Janisch
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Hi,

"anything on console is always "string",

Thanks for clearing my concept.

best regards
java-boy
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • 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.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!