Hi !
Featured games (91)
games approved by the League of Dukes
Games in Showcase (762)
Games in Android Showcase (229)
games submitted by our members
Games in WIP (847)
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  
  Urgh.  Why doesn't "=" work.    (Read 1884 times)
0 Members and 1 Guest are viewing this topic.
Offline Elomis

Junior Newbie

Don't run with scissors?

« Posted 2004-07-28 02:32:24 »

Ok, classic n00b question:

The following snippet of code is from a socket based text game (yeah, yeah, ANOTHER mud...) I can't make the server understand the input from the user.  I can replace the entire if statement with out.println(Command) and it will echo whatever you type to it, but if you try to perform an operation on the Command (compare it to say "q", quitting if it matches) it simply ignores the input.  What am I doing wrong?

 while((Command = in.readLine()) != null){
          if(Command == "q"){
          do something

Regards,  Huh
Offline oNyx

JGO Coder

Medals: 2

pixels! :x

« Reply #1 - Posted 2004-07-28 03:29:04 »

String bla="moo";
String foo="moo";
String bar="MoO";

if(bla.equals("moo")) //true

if(foo.equals(bla)) //also true

if(foo.equals(bar.toLowerCase())) //also true

if("moo".equals(bar)) //not true

弾幕 ☆ @mahonnaiseblog
Offline Elomis

Junior Newbie

Don't run with scissors?

« Reply #2 - Posted 2004-07-28 03:48:57 »


A straight, intelligible, non-condescending answer in a web forum which answered specifically my question.


Thankyou so much, I'm gonna stick around here.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline erikd

JGO Ninja

Medals: 16
Projects: 4
Exp: 14 years


« Reply #3 - Posted 2004-07-28 08:09:25 »

A String is an object.
The == operator checks if the objects are the same object (it basically compares the references of the objects, so comparing 2 strings that way becomes a simple integer comparison). With Strings, you may get a result you won't expect when you do:

if (stringA == stringB) {

when both stringA and stringB hold "HelloWorld"

This is because stringA and stringB may be different objects, even though the contents are the same.
Using == on Strings will work when you define the Strings literally in your source (ie, String stringA="HelloWorld; String stringB="HelloWorld"), or when you would do stringA.intern(); stringB.intern(); first (but don't start doing the latter now without knowing what it actually does).

If you want to learn more about this, check the source from the String class, and more specifically String.intern()

Offline nech_neb

Junior Devvie

Java for LIFE !!!

« Reply #4 - Posted 2004-07-28 21:11:50 »

Hope this helps:

It sure helped me back in the day....
Pages: [1]
  ignore  |  Print  

EgonOlsen (364 views)
2018-06-10 19:43:48

EgonOlsen (364 views)
2018-06-10 19:43:44

EgonOlsen (304 views)
2018-06-10 19:43:20

DesertCoockie (540 views)
2018-05-13 18:23:11

nelsongames (869 views)
2018-04-24 18:15:36

nelsongames (853 views)
2018-04-24 18:14:32

ivj94 (1304 views)
2018-03-24 14:47:39

ivj94 (425 views)
2018-03-24 14:46:31

ivj94 (1088 views)
2018-03-24 14:43:53

Solater (443 views)
2018-03-17 05:04:08
Java Gaming Resources
by philfrei
2017-12-05 19:38:37

Java Gaming Resources
by philfrei
2017-12-05 19:37:39

Java Gaming Resources
by philfrei
2017-12-05 19:36:10

Java Gaming Resources
by philfrei
2017-12-05 19:33:10

List of Learning Resources
by elect
2017-03-13 14:05:44

List of Learning Resources
by elect
2017-03-13 14:04:45

SF/X Libraries
by philfrei
2017-03-02 08:45:19

SF/X Libraries
by philfrei
2017-03-02 08:44:05 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‑
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!