d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Need El Help-o
Prev12
Add Reply New Topic New Poll
Member
Posts: 15,666
Joined: Jul 15 2007
Gold: 3,046.40
Jul 31 2012 06:08pm
Quote (SilverMice @ Jul 31 2012 04:03pm)
print out what is being compared and see if you can figure out the issue.


Strange...
Code
boolean leave=false;
 
  for(i=0; i<namesArr.size();i++){
       
   if(leave==false){
    if((usTemp==namesArr.get(i) && pwTemp==passArr.get(i))){
     
    System.out.println("Welcome!");
    leave=true;
   
    }else{
    System.out.print(usTemp + " == " + namesArr.get(i) + " && " + pwTemp + " == " + passArr.get(i) + " ");
    System.out.println("User does not exist");
    }
   }
  }


Results:
Code
asdf == can't && asdf == pan't User does not exist
asdf == asdf && asdf == asdf User does not exist
asdf == df && asdf == df User does not exist
asdf == asdf && asdf == asdf User does not exist
Member
Posts: 5,641
Joined: Apr 13 2006
Gold: 2.00
Jul 31 2012 06:12pm
you can get rid of the leave boolean.

Also the reason seems to be == I would guess in java does an address test. .Equals() should do the trick.

Code
for(i=0; i<namesArr.size();i++){
     
   if((usTemp.Equals(namesArr.get(i)) && pwTemp.Equals(passArr.get(i)))){
   
   System.out.println("Welcome!");

   }else{
   System.out.println("User does not exist");
   }

 }


This post was edited by SilverMice on Jul 31 2012 06:13pm
Member
Posts: 15,666
Joined: Jul 15 2007
Gold: 3,046.40
Jul 31 2012 06:16pm
i love you man.
Member
Posts: 11,637
Joined: Feb 2 2004
Gold: 434.84
Aug 1 2012 08:19am
Quote (SilverMice @ Jul 31 2012 07:12pm)
you can get rid of the leave boolean.

Also the reason seems to be == I would guess in java does an address test. .Equals() should do the trick.

Code
for(i=0; i<namesArr.size();i++){
     
   if((usTemp.Equals(namesArr.get(i)) && pwTemp.Equals(passArr.get(i)))){
   
   System.out.println("Welcome!");

   }else{
   System.out.println("User does not exist");
   }

 }


For objects in Java '==' does an instance equality check, i.e. it checks if the two objects are the same instance. This can be particularly nasty with Strings as sometimes String literals will pass that check due to Java's String interning. Always use Object.equals(Object o) when checking the equality unless for some reason you need to make sure the instances are the same.
Go Back To Programming & Development Topic List
Prev12
Add Reply New Topic New Poll