0
System.out.println("what is your name? ");
			
String name = kb.nextLine();
			
for(int i = 0; i < member2.size(); i++){
				
  if(member2.get(i).equals(name)){
					
     System.out.println(name + " is a member. ");
			
     break;			
}
				
else{
					
  System.out.println(name +" is not a member. ");
					
  break;
				
}

i did a system.out.println on the member2 arraylist (String arraylist) and it prints out the correct stuff. when comparing whether or not name is equal to an item in the arraylist, it always give me the else statement even if the name is in the list.

for example, member2 contains:
"john"
"william"
"xxxxx"

and then i would type in "john", and it gives me "john is not a member."

Edited by plasticfood: n/a

2
Contributors
2
Replies
3
Views
6 Years
Discussion Span
Last Post by plasticfood
0

It looks to me like your if statement is executed every time you compare, that way if the first string in the ArrayList is not the one you entered it returns ___ is not a member. Try using an intermediate variable like this:

boolean found = false;
for(String s : member2)
    if(s.equals(name))
        found = true;
        
if(found)
    System.out.println(name + " is a member. ");
else
  System.out.println(name +" is not a member. ");
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.