• 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

int's and if statement help!

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
so I'm trying to write a quick program that tests out some stuff i've made for a text based game. i wanna test a simple stat system, a simple lvl up system, and a simple combat system. I'm trying to make it where the player is asked what race he/she wants to play then depending on the answer it changes the players starting stats. however after'v- doing what i thought would work and trying several different variations of what i thought would work i've come to the conclusion that I'm stuck! YAY progress! below is the code i've got typed. again I'm trying to keep it simple also I'm new to programing so i greatly appreciate your help (insert your name here.).

 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK, first point. Never use == to compare Strings. You need to use the equals() method. == checks to see if they are the same object, but you want to check if they have the same value.

Once you've done that, you might want to consider putting the choice in a loop. At the moment, if I entered "Orc" (or "Banana", or whatever) it would let me do that, but wouldn't initialise the character correctly.
 
Bartender
Posts: 563
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please UseCodeTags ( <-- click on link for instructions ) and keep line lengths to a reasonable length. 80 columns is a typically acceptable length. I've adjusted both for you this time.
 
Ranch Hand
Posts: 216
Tomcat Server Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
import statement between code ??
poor coding ...
 
Mike orr
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
first thanks for the edit greg, i will be sure to do that in the future.
second i knew there was something i was forgetting with strings and comparing them. so i should use

yep that seems to work much better!

the loop was the next thing i was gonna work on i just got side tracked. thank you guys so much for the help. take care!
 
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Nikhil Sagar wrote:import statement between code ??
poor coding ...

No, it means two classes have been put into one set of code tags. There would be no such error in the original code.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mike orr wrote:first thanks for the edit greg, i will be sure to do that in the future.
second i knew there was something i was forgetting with strings and comparing them. so i should use yep that seems to work much better!


Little tip for you: when comparing with String literals, it's quite a good idea to invert the comparison, ie:
if ("Elf".equals(player.race))
instead of
if (player.race.equals("Elf"))
because then you don't get a NullPointerException if player.race is null.
Oh, and you almost NEVER want to put a semicolon after an if statement.

BIG tip for you: Separate your GUI code from your game logic completely - or as far as you possibly can. GUI code should ONLY be concerned with display and interaction; it shouldn't be doing any calculations - and that includes stats updates. Keep all that stuff separate and simply hand back results for your GUI to display.

HIH

Winston
 
Nikhil Sagar
Ranch Hand
Posts: 216
Tomcat Server Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

Nikhil Sagar wrote:import statement between code ??
poor coding ...

No, it means two classes have been put into one set of code tags. There would be no such error in the original code.



Oh !! if it is then, I am sorry Mike...
Thanks Sherrif....
reply
    Bookmark Topic Watch Topic
  • New Topic