Results 1 to 9 of 9
  1. #1
    ChrisMayhew is offline Member
    Join Date
    Jan 2009
    Posts
    32
    Rep Power
    0

    Default [SOLVED] Problem with an If Statement.

    Hey again.

    I am trying to get a while loop to work only when it gets a certain string sent back to the server.

    This is the code:
    Java Code:
    public void ServerCMDS(String NxtCmd) throws java.rmi.RemoteException
    {
        i = 1;
        if (NxtCmd == "/ShowUsers") {
           while (i <= LoggedInCount) {
                System.out.println(UsersLoggedIn[i]);
                i++;
           }
           i = 0;
        }
    }
    Now if i print out the NxtCmd to the screen it shows exactly what i just typed. I.E. /ShowUsers

    But for some reason the IF statement will not work, maybe im doing it wrong.

    If i remove the if statement the while loop will run no problem.

    Any ideas?

    Thanks,
    Chris

  2. #2
    dswastik is offline Senior Member
    Join Date
    Dec 2008
    Location
    Kolkata
    Posts
    282
    Rep Power
    6

    Default wrong approach

    for value comparison in case of String , you should use equals or equalsIgnoreCase method, so your code should go as follows


    public void ServerCMDS(String NxtCmd) throws java.rmi.RemoteException
    {
    i = 1;
    if (NxtCmd.equalsIgnoreCase("/ShowUsers")) {
    while (i <= LoggedInCount) {
    System.out.println(UsersLoggedIn[i]);
    i++;
    }
    i = 0;
    }
    }

  3. #3
    ChrisMayhew is offline Member
    Join Date
    Jan 2009
    Posts
    32
    Rep Power
    0

    Default

    Ah thanks so much, that works perfect!

  4. #4
    logicbug's Avatar
    logicbug is offline Member
    Join Date
    Jan 2009
    Location
    The Great White North, eh?
    Posts
    76
    Rep Power
    0

    Default

    yep, Id say thats it. using == for String compares addresses and not the string itself.

  5. #5
    ChrisMayhew is offline Member
    Join Date
    Jan 2009
    Posts
    32
    Rep Power
    0

    Default

    Quote Originally Posted by logicbug View Post
    yep, Id say thats it. using == for String compares addresses and not the string itself.
    When i was searching for examples when it didn't work at first everyone i found all used numbers instead of things like strings and characters. Even the book i bought was using numbers so i just assumed it was like other programming languages i have used and used == to compare them.

    :)

  6. #6
    logicbug's Avatar
    logicbug is offline Member
    Join Date
    Jan 2009
    Location
    The Great White North, eh?
    Posts
    76
    Rep Power
    0

    Default

    I did the same thing the first time I needed to compare a string in java, must have taken me 2 hours to figure out what was wrong lol it's not something you are likely to forget!

  7. #7
    ChrisMayhew is offline Member
    Join Date
    Jan 2009
    Posts
    32
    Rep Power
    0

    Default

    Just another quick question, Because at the moment im just getting it to relay messages on the servers console how do i make it so that the messages are sent to the client instead of displaying on the server?

    For example if i type /ShowUsers on my client it actually displays them on the server and not the client.

  8. #8
    Eyeinstyin is offline Member
    Join Date
    Jan 2009
    Posts
    4
    Rep Power
    0

    Default

    Firstly "==" operator compares addresses i.e reference to object;for objects but works fine with non-objects type like int,long etc i.e primary data-types.
    For String class "Equals" method is used to compare 2 strings
    System.out stands for console (on server machine)
    So you must write similar Sytem.out at client side java program.For that you will require server to send the list i.e UserLoggedIn array to client and then client will display that array using System.out

  9. #9
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Posts
    564
    Rep Power
    6

    Default

    this is one of the most common issues people have with strings. another one you (and anyone else unfamiliar with java strings) might wanna look into is null vs. empty strings.

Similar Threads

  1. problem while using return statement
    By shaluchandran in forum New To Java
    Replies: 10
    Last Post: 12-12-2008, 06:29 PM
  2. If-Else statement problem
    By MomenT in forum New To Java
    Replies: 9
    Last Post: 10-23-2008, 08:06 AM
  3. ForLoop statement problem
    By MomenT in forum New To Java
    Replies: 4
    Last Post: 10-01-2008, 01:01 PM
  4. Please help me with this Problem Statement
    By KMS in forum New To Java
    Replies: 1
    Last Post: 09-22-2008, 08:44 PM
  5. Problem with a switch statement in Java
    By baltimore in forum New To Java
    Replies: 2
    Last Post: 08-02-2007, 04:43 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •