Hello guys,
please could somebody help me...there is a problem with logic in my loop with pretty the same structure
in methods:
public void isMember()
and
public void deleteMember()
Compilator ignores the scanner and directly goes to else statement and puts it in the loop 10 times...
import java.util.*;
public class Membership
{
Scanner input = new Scanner(System.in);
String members[] = new String [10];
public void mainMenu()
{
int selection = 0;
System.out.println("Library Menu:\n");
System.out.println("========================");
System.out.println("1. Add members ");
System.out.println("2. Is a member? ");
System.out.println("3. Delete a member");
System.out.println("4. Count members");
System.out.println("5. Quit");
System.out.print("Please select your option now: ");
selection = input.nextInt();
switch (selection)
{
case 1:
addMembers();
break;
case 2:
isMember();
break;
case 3:
deleteMember();
break;
case 4:
countMembers();
break;
case 5:
System.out.println("Goodbye! \n");
System.exit(0);
}
}
public void addMembers()
{
System.out.println("Please type in 10 names to be added");
for (int i = 0; i < members.length; i++)
{
members[i] = input.nextLine();
// Consume the remaining newline
input.nextLine();
}
mainMenu();
}
public void isMember()
{
System.out.println("Please enter your name");
String name = input.nextLine();
for (int i = 0; i<members.length; i++)
{
if
(name == members[i])
{
System.out.println("You are a member");
}
else
{
System.out.println("You are not a member");
}
}
System.out.println("You are not a member");
mainMenu();
}
public void deleteMember()
{
System.out.println("Please enter the name to remove");
String name = input.nextLine();
for (int i = 0; i<members.length; i++)
{
if (name == members[i])
{
members[i] = null;
System.out.println("Member deleted");
}
else
{
System.out.println("Name not found");
}
}
mainMenu();
}
public void countMembers()
{
int count = 0;
for (int i = 0; i<members.length; i++)
{
if(members[i] != null)
{
count = i;
count++;
}
}
System.out.println("The number of members is: " + count);
mainMenu();
}
public static void main (String [] args)
{
new Membership().mainMenu();
}
}