d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Need El Help-o
12Next
Add Reply New Topic New Poll
Member
Posts: 15,666
Joined: Jul 15 2007
Gold: 3,046.40
Jul 31 2012 05:02pm
The following code will not check the user input versus an existing arraylist...
It is prompting the "User does not exist"...


Code

//prompt user for input

String usCheck=JOptionPane.showInputDialog("Enter username");
String pwCheck=JOptionPane.showInputDialog("Input password");



//assign arraylist to string array
//assign input to string
//I know it's repetitive I just tried it this way hoping it'll somehow work

String[] names = namesArr.toArray(new String[namesArr.size()]);
String[] passwords = namesArr.toArray(new String[passArr.size()]);
String usTemp = usCheck;
String pwTemp = pwCheck;



//FOR LOOP to check if INPUT==element in the array

  for(i=0; i<names.length;i++){
   
   if((usTemp==names[i] && pwTemp==passwords[i])){
    System.out.println("Welcome!");
   }else{
    System.out.println("User does not exist");
   }
  }
Member
Posts: 15,666
Joined: Jul 15 2007
Gold: 3,046.40
Jul 31 2012 05:07pm
Side note:

I have the rest of the logic working with print statements to verify and catch blah blah blahs.

get user input
save to file
read the file
print the contents of the file
prompt user for name/pass
check name/pass <--- This is the above code
Member
Posts: 5,641
Joined: Apr 13 2006
Gold: 2.00
Jul 31 2012 05:21pm
What is happening likely is that you are not using the last name/password combo in the list, so even if you hit the correct name/pass you get passed up and fail because the next username/pass is not correct. I bet if you use the last username/password combo in that set you get a "Welcome!".

Make a function that returns boolean after looping through names/passwords and returning true immediately when the correct username/pass gets found.

This post was edited by SilverMice on Jul 31 2012 05:35pm
Member
Posts: 15,666
Joined: Jul 15 2007
Gold: 3,046.40
Jul 31 2012 05:26pm
Quote (SilverMice @ Jul 31 2012 03:21pm)
What is happening likely is that you are not using the last name/password combo in the list, so even if you hit the correct name/pass you get passed up and fail because the next username/pass is not correct. I bet if you use that set you get a "Welcome!".

Make a function that returns boolean after looping through names/passwords and returning true immediately when the correct username/pass gets found.


hmm

This post was edited by Calamitymic on Jul 31 2012 05:29pm
Member
Posts: 15,666
Joined: Jul 15 2007
Gold: 3,046.40
Jul 31 2012 05:38pm
Here's the new code, it still seems to just print "User does not exist" repeatedly for the amount of elements in the array.

Is there restrictions with the type of data?

b/c the names[i] array is populated from an arraylist using .toarray method and the .showInputDialog is assigned to a string.


Code
boolean leave=false;
 
  for(i=0; i<names.length;i++){
   if(leave==false){
   
    if((usTemp==names[i] && pwTemp==passwords[i])){
     
    System.out.println("Welcome!");
    leave=true;
   
    }else{
     
    System.out.println("User does not exist");
    }
   }
  }
Member
Posts: 5,641
Joined: Apr 13 2006
Gold: 2.00
Jul 31 2012 05:43pm
Quote (Calamitymic @ Jul 31 2012 05:38pm)
Here's the new code, it still seems to just print "User does not exist" repeatedly for the amount of elements in the array.

Is there restrictions with the type of data?

b/c the names[i] array is populated from an arraylist using .toarray method and the .showInputDialog is assigned to a string.


Code
boolean leave=false;
 
  for(i=0; i<names.length;i++){
   if(leave==false){
   
    if((usTemp==names[i] && pwTemp==passwords[i])){
     
    System.out.println("Welcome!");
    leave=true;
   
    }else{
     
    System.out.println("User does not exist");
    }
   }
  }


haha I should read more thoroughly... I gave the way wrong answer.

what is the namesArr's type?
Member
Posts: 15,666
Joined: Jul 15 2007
Gold: 3,046.40
Jul 31 2012 05:45pm
I'm using a scanner to read the file and to write it onto an ArrayList<String>
Member
Posts: 5,641
Joined: Apr 13 2006
Gold: 2.00
Jul 31 2012 05:49pm
Is there any reason you have not to just do the following:

Code
//prompt user for input

String usCheck=JOptionPane.showInputDialog("Enter username");
String pwCheck=JOptionPane.showInputDialog("Input password");



//assign arraylist to string array
//assign input to string
//I know it's repetitive I just tried it this way hoping it'll somehow work

String usTemp = usCheck;
String pwTemp = pwCheck;



//FOR LOOP to check if INPUT==element in the array

 for(i=0; i<names.length;i++){
 
  if((usTemp==namesArr[i] && pwTemp==passArr[i])){
   System.out.println("Welcome!");
  }else{
   System.out.println("User does not exist");
  }
 }




If you don't like that idea. you can always change your passArray line ot actually use the passArray. At the moment you are passing namesArray to both names and passwords.
Member
Posts: 15,666
Joined: Jul 15 2007
Gold: 3,046.40
Jul 31 2012 05:56pm
I didn't realize i was passing names to both. strange.

but i fixed it and ran it again with same results.

same results passing it through namesArr

Code
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.println("User does not exist");
    }
   }
  }
Member
Posts: 5,641
Joined: Apr 13 2006
Gold: 2.00
Jul 31 2012 06:03pm
print out what is being compared and see if you can figure out the issue.

Code
System.out.println("usTemp= " + usTemp + " nameArr[" + i + "]= " namesArr.get(i));


something like above only with both sets of name/pass.

This post was edited by SilverMice on Jul 31 2012 06:05pm
Go Back To Programming & Development Topic List
12Next
Add Reply New Topic New Poll