Results 1 to 9 of 9
Like Tree2Likes
  • 1 Post By JosAH
  • 1 Post By Tolls

Thread: Special characters? Too same strings doesnt match...

  1. #1
    Lionlev is offline Senior Member
    Join Date
    May 2012
    Posts
    172
    Rep Power
    3

    Question Special characters? Too same strings doesnt match...

    Ok so I've go another problem. I've got a java socket multithread server and a flash client

    When client clicks on login button, it sends this string <<user123 pass123>> server receives it and sort it to strings username = user123 and userpass = pass123
    After that it checks if "user123" ini file exist in the src folder (username + ".ini") it finds it and reading it. user123.ini contains "password pass123"

    and checks if userpass == user123's password
    But the problem is that when I type the right password , it doesn't match it I don't know why...

    Here is the code:

    Java Code:
    try
    			{
    				INPUT = new Scanner(SOCK.getInputStream());
    				OUT = new PrintWriter(SOCK.getOutputStream());
    				Scanner checkUser = null;
    				boolean gotusername = false;
    				boolean gotpassword = false;
    				boolean goteverything = false;
    				boolean allowmenu = false;
    				boolean gotinmenu = false;
    				String username = "NotFound";
    				String userpassword = "NotFound";
    				while(true)
    				{
    					CheckConnection();
    					if(!INPUT.hasNext())
    					{ return;}
    					
    					if(goteverything == false)
    					{
    						username = INPUT.next();
    						userpassword = INPUT.next();
    						System.out.println("User's username: " + username +"\n" + "User's password: " + userpassword);
    						goteverything = true;
    					}
    					
    					File userFile=new File(username+".ini");
    					//File userFile=new File("lionlev.ini");
    					System.out.println("looking for " + userFile);
    					boolean exists = userFile.exists();
    					if(!exists){
    						System.out.println("Invalid username.");
    					} else {
    						try
    						{
    							checkUser = new Scanner(new File(username+".ini"));
    							//checkUser = new Scanner(new File("lionlev.ini"));
    						} catch (Exception e) {}
    						checkUser.next();
    						String cpass = checkUser.next();
    						System.out.println("Entered pass: " + userpassword);
    						System.out.println("REal pass: " + cpass);
    						if(allowmenu == false){
    							if(userpassword == cpass){
    							System.out.println("Login is alright, sending "+username+ " to the menu..");
    							OUT.println("ok1" + EOF);
    							OUT.flush();
    							allowmenu = true;
    							} else{
    								System.out.println("Wrong password");
    							}
    						} else {
    							System.out.println("allowmenu is true");
    						}
    					}
    					
    					
    					
    					String menu1 = INPUT.next();
    					while (gotinmenu == false){
    						if(menu1.indexOf("menu") > -1){
    							System.out.println("User "+username+ " is in the menu.");
    							gotinmenu = true;
    						}
    					}

    PLease help?
    Last edited by Lionlev; 06-06-2012 at 09:46 PM.

  2. #2
    Lionlev is offline Senior Member
    Join Date
    May 2012
    Posts
    172
    Rep Power
    3

    Default Re: Special characters? Too same strings doesnt match...

    Maybe I am guessing that there are some extra hidden characters? I don't know...

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Special characters? Too same strings doesnt match...

    First try calling trim() on the received String to remove leading and trailing white space. If that doesn't help, loop through each character of the received String to see for yourself exactly what you're receiving. Having said all of that, all the usual warnings about not using Strings for passwords, and for holding password Strings on disk apply.

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,353
    Blog Entries
    7
    Rep Power
    20

    Default Re: Special characters? Too same strings doesnt match...

    Quote Originally Posted by Lionlev View Post
    Java Code:
    				if(userpassword == cpass)
    That's not how you compare two Strings for equality; use the Striing.equals( ... ) method instead.

    kind regards,

    Jos
    Fubarable likes this.
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    Lionlev is offline Senior Member
    Join Date
    May 2012
    Posts
    172
    Rep Power
    3

    Default Re: Special characters? Too same strings doesnt match...

    Quote Originally Posted by JosAH View Post
    That's not how you compare two Strings for equality; use the Striing.equals( ... ) method instead.

    kind regards,

    Jos
    Thanks!

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,817
    Rep Power
    19

    Default Re: Special characters? Too same strings doesnt match...

    Java Code:
    if(goteverything == false)
    should be
    Java Code:
    if(!goteverything)
    as you did with the later 'if' statement.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  7. #7
    Lionlev is offline Senior Member
    Join Date
    May 2012
    Posts
    172
    Rep Power
    3

    Default Re: Special characters? Too same strings doesnt match...

    Quote Originally Posted by Tolls View Post
    Java Code:
    if(goteverything == false)
    should be
    Java Code:
    if(!goteverything)
    as you did with the later 'if' statement.
    But isn't that the same thing?

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,353
    Blog Entries
    7
    Rep Power
    20

    Default Re: Special characters? Too same strings doesnt match...

    Quote Originally Posted by Lionlev View Post
    But isn't that the same thing?
    Conceptually they are the same, but your version is more like:

    Java Code:
    if (a+1 > b+1) ...
    ... where a simpler:

    Java Code:
    if (a > b) ...
    ... can do the job too.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  9. #9
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,817
    Rep Power
    19

    Default Re: Special characters? Too same strings doesnt match...

    Quote Originally Posted by Lionlev View Post
    But isn't that the same thing?
    It just looks like you don't know what you're doing.
    DarrylBurke likes this.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Escaping Special Characters
    By djgovins in forum Lucene
    Replies: 6
    Last Post: 07-08-2011, 06:05 PM
  2. Special Characters on GUI
    By keo in forum AWT / Swing
    Replies: 4
    Last Post: 05-01-2011, 10:40 PM
  3. Special characters question
    By alexalex1234 in forum Advanced Java
    Replies: 1
    Last Post: 03-24-2011, 11:33 AM
  4. XML with special characters
    By Kaizah in forum XML
    Replies: 1
    Last Post: 11-06-2009, 02:26 PM
  5. special characters
    By ravian in forum New To Java
    Replies: 2
    Last Post: 11-16-2007, 01:28 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
  •