Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (107)
games submitted by our members
Games in WIP (536)
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 1012 times)
0 Members and 1 Guest are viewing this topic.
Offline Elomis

Junior Newbie




Don't run with scissors?


« Posted 2004-07-28 04: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: 1


pixels! :x


« Reply #1 - Posted 2004-07-28 05: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 05:48:57 »

O_O

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

Woah.


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


Maximumisness


« Reply #3 - Posted 2004-07-28 10: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 Member




Java for LIFE !!!


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

Hope this helps:

http://java.sun.com/docs/books/tutorial/java/index.html

It sure helped me back in the day....
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.

Riven (15 views)
2014-07-29 18:09:19

Riven (10 views)
2014-07-29 18:08:52

Dwinin (10 views)
2014-07-29 10:59:34

E.R. Fleming (28 views)
2014-07-29 03:07:13

E.R. Fleming (10 views)
2014-07-29 03:06:25

pw (40 views)
2014-07-24 01:59:36

Riven (39 views)
2014-07-23 21:16:32

Riven (27 views)
2014-07-23 21:07:15

Riven (28 views)
2014-07-23 20:56:16

ctomni231 (59 views)
2014-07-18 06:55:21
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
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!