Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (539)
Games in Android Showcase (132)
games submitted by our members
Games in WIP (603)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  If statements comparing a string to a string? (a = "text" doesn't work)  (Read 2022 times)
0 Members and 1 Guest are viewing this topic.
Offline Nexie

Junior Newbie





« Posted 2011-04-07 09:51:28 »

Hiya - I've been lurking the forums for a while looking for good re-introductions to Java programming (I've done a bit of C# about a year ago).

Anyway - I've been following a series of videos on TheNewBoston on Java, and I've hit a bit of a dead end.

I am trying to compare the contents of a text String to a String value in an If statement, but I can't seem to get it to acknowledge that the two are the same - Could you try and explain what I am doing wrong? The video only deals with Int comparing which is pretty simple to understand, compared to my own little attempt.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
import java.util.Scanner;

class inputtest {
   public static void main(String arg[]) {
      Scanner userinput = new Scanner(System.in);
      String ishumantest;
     
      System.out.println("Are you a human? If so type in yes");
      ishumantest = userinput.nextLine();
      // FOR DEBUGGING INPUT
      //System.out.println();
      if (ishumantest == "yes")
      {
         System.out.println("You are welcome to enter.");
      }
      else
      {
         System.out.println("Non-Humans not allowed.");
      }
   }
}

Offline Riven
« League of Dukes »

« JGO Overlord »


Medals: 840
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #1 - Posted 2011-04-07 10:10:12 »

== checks for identical references, not for object equality


1  
2  
boolean eq = new String("abc") == "abc"; // eq = false;
boolean eq = new String("abc").equals("abc"); // eq = true;


Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social
Offline Nexie

Junior Newbie





« Reply #2 - Posted 2011-04-07 10:16:29 »

Oh I see - he failed to mention that. Your suggestion works perfectly. Cheesy
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline ReBirth
« Reply #3 - Posted 2011-04-07 11:16:51 »

Because String is kinda special. new String("abc") is not same with constant "abc" itself. To take it rude, "there two of them on memory".

That's what I remember from reading about how bad String is. That's why some are using StringBuilder.

Offline cylab

JGO Ninja


Medals: 55



« Reply #4 - Posted 2011-04-07 13:45:36 »

That's why some are using StringBuilder.
Not exactly. StringBuilder provides means to concatenate Strings without creating new Strings over and over, since Strings are immutable. Has nothing todo with comparison.

As a rule of thumb: use .equals() on all types starting with a capital letter.

Mathias - I Know What [you] Did Last Summer!
Offline ToXSiK

Senior Newbie





« Reply #5 - Posted 2011-05-11 23:08:08 »

Like they said above.  I just wanted to throw in there that Object.equals(otherObj) is used when comparing OBJECTS.  == is used ONLY when comparing primitive data types (float, int, byte, etc.).  Every time you use a java object class, you will have the .equals method available (Since they all implement Comparable - Look into doing this if you want to make you're own .equals method).

ToXSiK  Cool
Offline counterp

Senior Devvie


Medals: 11



« Reply #6 - Posted 2011-05-11 23:40:02 »

Like they said above.  I just wanted to throw in there that Object.equals(otherObj) is used when comparing OBJECTS.  == is used ONLY when comparing primitive data types (float, int, byte, etc.).  Every time you use a java object class, you will have the .equals method available (Since they all implement Comparable - Look into doing this if you want to make you're own .equals method).

ToXSiK  Cool

Not true, sometimes you have to check if an object is null (and calling equals() on a null object will throw an exception), or you want to see if two objects are just the same references.
Offline ra4king

JGO Kernel


Medals: 356
Projects: 3
Exp: 5 years


I'm the King!


« Reply #7 - Posted 2011-05-12 01:28:40 »

Like they said above.  I just wanted to throw in there that Object.equals(otherObj) is used when comparing OBJECTS.  == is used ONLY when comparing primitive data types (float, int, byte, etc.).  Every time you use a java object class, you will have the .equals method available (Since they all implement Comparable - Look into doing this if you want to make you're own .equals method).

ToXSiK  Cool
Also wrong in the fact that .equals() is not in Comparable, but in Object, which is the superclass of ALL classes Wink

Offline pjt33
« Reply #8 - Posted 2011-05-12 07:14:43 »

Could you try and explain what I am doing wrong?
You're writing C# Tongue

This is one of the things that trips a lot of new Java programmers up, so the designers of C# decided to put in a special case for strings. So people who move from C# to Java are now even more likely to trip up than people who start Java with no prior knowledge of any language.
Offline ToXSiK

Senior Newbie





« Reply #9 - Posted 2011-05-13 18:18:32 »

Sorry, I meant in object.  We all make typos  Cool

Quote
Not true, sometimes you have to check if an object is null (and calling equals() on a null object will throw an exception), or you want to see if two objects are just the same references.

But by calling .equals(), whether you are checking if it is null or not or if it is just a reference, you still must use .equals for situations dealing with objects... All I am saying.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline CyanPrime
« Reply #10 - Posted 2011-05-13 18:20:43 »

I use String.compare().
Offline ra4king

JGO Kernel


Medals: 356
Projects: 3
Exp: 5 years


I'm the King!


« Reply #11 - Posted 2011-05-13 21:32:21 »

compareTo() is only useful for comparing two Strings lexicographically (by letter value).
equals() is best for comparing equality.

Offline Eli Delventhal

JGO Kernel


Medals: 42
Projects: 11
Exp: 10 years


Game Engineer


« Reply #12 - Posted 2011-05-13 22:06:44 »

I feel like nobody has said what's really important to learn here.

The reason why you can't use == for a String in Java is because a String is an Object, not a primitive. If you call == on an object, you are just comparing the memory addresses of pointers. So, if both pointers are at the same memory address, then it's true, otherwise it's false. When calling the equals() method, you are actually comparing the objects that lie at the memory you are pointing to, using a method that each object has its own implementation of. A string for example compares every single letter for equality (or maybe it does a hash of some kind, I'm not sure).

Primitives are:
int
boolean
float
double
byte
char

Everything else is an object. Just think of == as "the exact same object" and .equals() as "have the same value." Also, you don't need to use == to check for null (even though I would do it that way), because myObj.equals(null) will also be false.

See my work:
OTC Software
Offline pjt33
« Reply #13 - Posted 2011-05-13 22:14:40 »

int
boolean
float
double
byte
char
short
long
Offline ra4king

JGO Kernel


Medals: 356
Projects: 3
Exp: 5 years


I'm the King!


« Reply #14 - Posted 2011-05-13 22:15:21 »

Forgot "short" and "long" Wink

Offline ReBirth
« Reply #15 - Posted 2011-05-14 03:01:51 »

Forgot "short" and "long" Wink
you can forget short but not long, it's important for updating delta time on game loop Cheesy

Offline counterp

Senior Devvie


Medals: 11



« Reply #16 - Posted 2011-05-14 03:19:24 »

Sorry, I meant in object.  We all make typos  Cool

Quote
Not true, sometimes you have to check if an object is null (and calling equals() on a null object will throw an exception), or you want to see if two objects are just the same references.

But by calling .equals(), whether you are checking if it is null or not or if it is just a reference, you still must use .equals for situations dealing with objects... All I am saying.

That's not the problem, the problem is that you said:

Quote
== is used ONLY when comparing primitive data types

which is not true.
Offline Eli Delventhal

JGO Kernel


Medals: 42
Projects: 11
Exp: 10 years


Game Engineer


« Reply #17 - Posted 2011-05-16 17:13:18 »

Grin

Thanks, I knew I was forgetting a couple.

See my work:
OTC Software
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

rwatson462 (30 views)
2014-12-15 09:26:44

Mr.CodeIt (23 views)
2014-12-14 19:50:38

BurntPizza (50 views)
2014-12-09 22:41:13

BurntPizza (84 views)
2014-12-08 04:46:31

JscottyBieshaar (45 views)
2014-12-05 12:39:02

SHC (59 views)
2014-12-03 16:27:13

CopyableCougar4 (57 views)
2014-11-29 21:32:03

toopeicgaming1999 (123 views)
2014-11-26 15:22:04

toopeicgaming1999 (114 views)
2014-11-26 15:20:36

toopeicgaming1999 (32 views)
2014-11-26 15:20:08
Resources for WIP games
by kpars
2014-12-18 10:26:14

Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!