Java-Gaming.org Java4K winners: [ by our judges | by the community ]         
Featured games (67)
games approved by the League of Dukes
Games in Showcase (∞)
games submitted by our members



News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  Print  
  Help with String variables  (Read 770 times)
0 Members and 1 Guest are viewing this topic.
Offline Drakkanic

JGO n00b
*

Posts: 3



« on: 2006-01-19 20:25:00 »

Hello, I'm a high-school student taking beginning Java, and I'm currently working on my final project (a simple RPG). It's going pretty well, except for one part: is there a way to check if a String variable does not equal something? I know this example won't work, because you can't use ==  with Strings to compare two variables (usually), but is there another way to do this? Any suggestions would be greatly appreciated, thanks.

public static void genderE(String gender, int picNum)
   {
      
      while (gender != "male" || gender != "female")
         {
            System.out.println("You have entered an invalid gender. Please try again:");
            gender = In.getString();
         }
      
      if (gender.equalsIgnoreCase("male"))
         picNum += 0;
      else if (gender.equalsIgnoreCase("female"))
         picNum += 100;
      else
         System.out.println("You have entered an invalid gender. Please try again:");
   }
Offline swpalmer

JGO Kernel
*****

Posts: 3438
Medals: 4


Where's the Kaboom?


« Reply #1 on: 2006-01-19 20:27:36 »

Not?

1  
 ! 

Offline Drakkanic

JGO n00b
*

Posts: 3



« Reply #2 on: 2006-01-19 20:30:02 »

It still doesn't work, because I get errors just for that, no matter what I enter, even if it's right.
Games published by our own members! Go get 'em!
Offline swpalmer

JGO Kernel
*****

Posts: 3438
Medals: 4


Where's the Kaboom?


« Reply #3 on: 2006-01-19 20:33:11 »

You don't get errors for stuff that is right.  If you told us what you tried and what the error message is we might be able to help you.

1  
2  
3  
if( !"male".equals("female") ) {
    System.out.println("Always true!");
}

Offline Drakkanic

JGO n00b
*

Posts: 3



« Reply #4 on: 2006-01-19 20:49:18 »

Sorry, I didn't understand what you meant with the single ! (extremely newbie-ish, and forgetful). That works, thanks.
Offline bauerr

Jr. Member
**

Posts: 92


Java!!!!!!!!!!! !!!


« Reply #5 on: 2006-01-20 02:02:03 »

The ! is a negation of a boolean expression. For example

  boolean male;

  ...

  if(!male){
    System.out.println("it's a woman);
  }else{
    System.out.println("it's a man);
  }
Offline hvor2

Sr. Member
**

Posts: 278


Beyond mind, there is an awareness...


« Reply #6 on: 2006-01-20 03:50:34 »

... and so mystring.equals("Something") returns boolean and you can negate it with prefix ! .
!mystring.equals("Something") --- that means mystring is NOT equals to string "Something", as guys already said.

Pages: [1]
  Print  
 
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.16 | SMF © 2011, Simple Machines Valid XHTML 1.0! Valid CSS!
Page created in 0.086 seconds with 20 queries.