Results 1 to 7 of 7
Like Tree1Likes
  • 1 Post By Wnt2bsleepin

Thread: Logic in java?

  1. #1
    Wuild is offline Member
    Join Date
    Apr 2012
    Posts
    6
    Rep Power
    0

    Default Logic in java?

    Like the topic says.
    I started developing in java a few days ago (was bored of PHP)

    So i started to make some basic apps but now im stuck at one thing...
    Let me explain

    if(string == string)
    IS TRUE
    else
    IS FALSE

    seems logical to me, but my code does not...
    Java Code:
    for (int i = 0; i < userdata.length; i++) {
    	if (userdata[i] != null) {
    		System.out.println(userdata[i].names + " => " + name);
    		if (userdata[i].names == name) {
    			System.out.println("Name already exists");
    		} else {
    			System.out.println("Does not exists");
    		}
    	}
    }
    so my problem lies in the if (userdata[i].names == name)
    each user has to register a name apon login.
    it has to check if the username is already in use.

    when printing out the System.out.println(userdata[i].names + " => " + name);
    i get for example
    test => test if test is already in use, but the
    if (userdata[i].names == name)
    says its not equal...

    Any replies would be awsome =)

  2. #2
    Wnt2bsleepin is offline Senior Member
    Join Date
    Feb 2012
    Posts
    219
    Rep Power
    3

    Default Re: Logic in java?

    When comparing strings, you have to use the equals method.

  3. #3
    Wuild is offline Member
    Join Date
    Apr 2012
    Posts
    6
    Rep Power
    0

    Default Re: Logic in java?

    what do you mean?

    Edit: nvm i think i understand ;) silly me

  4. #4
    Wuild is offline Member
    Join Date
    Apr 2012
    Posts
    6
    Rep Power
    0

    Default Re: Logic in java?

    im guessing you mean like this

    name.equals(userdata[i].names)

  5. #5
    Wnt2bsleepin is offline Senior Member
    Join Date
    Feb 2012
    Posts
    219
    Rep Power
    3

    Default Re: Logic in java?

    Pretty much. Strings are considered objects, so you need to compare the content of the String rather than the object itself.
    Fubarable likes this.

  6. #6
    Wuild is offline Member
    Join Date
    Apr 2012
    Posts
    6
    Rep Power
    0

    Default Re: Logic in java?

    Quote Originally Posted by Wnt2bsleepin View Post
    Pretty much. Strings are considered objects, so you need to compare the content of the String rather than the object itself.
    Yeah i figured it out :) thanks a bunch

  7. #7
    Wuild is offline Member
    Join Date
    Apr 2012
    Posts
    6
    Rep Power
    0

    Default Re: Logic in java?

    oh so im stuck again on what i think is simple logic...

    okey so i can check the equal names from the user handler.

    but when i login with 1 username and tries to login on another client with another username, it says that the username already exists...
    So some how the scripts sets the name before it checks it....

    Java Code:
    boolean exists = true;
    while (true) {
    	String set_name = is.readLine();
    		for (int i = 0; i < userdata.length; i++) {
    			if (userdata[i] != null) {
    				System.out.println(set_name + " => " + userdata[i].names);
    				if (set_name.equals(userdata[i].names)) {
    				os.println(set_name+" already in use");
    				exists = true;
         			        System.out.println("exists");
    				break;
    		        } else {
    				userdata[id].addUser(set_name);
    				exists = false;
    				System.out.println("does not exists");
    			}
    		}
    				}
    		if(!exists){
    		        break;
    		}
    	}
    name = userdata[id].names;

Similar Threads

  1. Logic Help
    By Barbados in forum New To Java
    Replies: 6
    Last Post: 01-22-2012, 11:24 PM
  2. Need help on logic
    By nn12 in forum New To Java
    Replies: 6
    Last Post: 03-10-2011, 11:06 AM
  3. Need help in java some implemting logic
    By satish.kinikiri in forum New To Java
    Replies: 2
    Last Post: 11-17-2010, 04:43 AM
  4. need a logic for this
    By rajivjoshi in forum New To Java
    Replies: 4
    Last Post: 06-12-2010, 02:18 PM
  5. Java + OOP + Logic Gates = HELP!
    By sev51 in forum New To Java
    Replies: 3
    Last Post: 01-26-2009, 08:42 PM

Posting Permissions

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