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();
    }
    
    
}

The biggest issue, the one that you are reporting having trouble with is that in the main menu you are selecting the next integer, which leaves the rest of the line remaining. I think it would help you out a lot if you changed to reading the whole line, then converting it to an integer. This should get you started:

selection = Integer.parseInt(input.nextLine());

Compilator ignores the scanner and directly goes to else statement and puts it in the loop 10 times...

What is a compilator?

The Scanner class is tricky to use. It reads a full line including lineend into a buffer and then returns parts of what was entered as you ask for it with next methods.
Only the nextLine method returns the line end character. The nextInt method leaves the lineend in the buffer. The following nextLine reads only that line end.
To show that this is happening add a println to show the contents of what is read after you have read a line in. Be sure to add a terminating character:
System.out.println("input=" + input + "<");

AND You are using the wrong technique to test the contents of a String object. You should use the equals() method NOT the == operator.

Rune911, I have no problem with selection, I need Scanner to read integers in this case.

NormR1, I changed for equals () but the problem remains, whether it is a problem of input or comparison...

Thank you guys anyway.

Did you print out the values that are being read in to see what they are and be sure they are what you expect?

To show that this is happening add a println to show the contents of what is read after you have read a line in. Be sure to add a terminating character:
System.out.println("input=" + input + "<");

Yes, but it doesn't make sense for me:

input=java.util.Scanner[delimiters=\p{javaWhitespace}+][position=2][match valid=true][need input=false][source closed=false][skipped=false][group separator=\,][decimal separator=\.][positive prefix=][negative prefix=\Q-\E][positive suffix=][negative suffix=][NaN string=\Q?\E][infinity string=\Q?\E]<

What did you just post?
It doesn't make sense to me either. It looks like a mix of meta language and regular expression.

I meant to have you print the String that was read in not the Scanner reference variable. I assumed that the data would be read into a variable named input. I see that input is the Scanner reference. Change the variable being printed to be the String that is getting the input. For example: name.

I tried to change Scanner obj for inputDialog, it doesn't work as well:

public void   isMember()
	{
		//System.out.println("Please enter your name: ");
		//String name = input.nextLine();
		
		String name  = JOptionPane.showInputDialog(null,
      "Please enter your name:",
      "Your name", JOptionPane.QUESTION_MESSAGE);
		
		for (int i = 0; i<members.length; i++)
		{
			if 
				(name. equals(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();
	}

it doesn't work as well:

Why is that? What happens?

I mean, in both cases it doesn't show the possibility to print in...

it doesn't show the possibility to print in...

Sorry, what does that mean? Can you show the program's output and comment it to show where you want it to execute differently?

Where in your code is the decision made that you want to be made differently?
You want it to "show the possibility to print". Where is the decision made to show ...?
What controls the showing?
Add printlns to show why your logic is not working the way you want it to.

It simply doesn't show the dialog or whatever to type a value for String name.
Immediately shows 10 times "You are a member".

Please copy and paste the full console from when you execute the program.
I have no idea what input you are giving to the program to be able to test it.

--------------------Configuration: <Default>--------------------
Library Menu:

========================
1. Add members   
2. Is a member? 
3. Delete a member
4. Count members
5. Quit
Please select your option now: 1
Please type in  10 names to be added
q
w
e
r
t
y
u
i
o
p
Library Menu:

========================
1. Add members   
2. Is a member? 
3. Delete a member
4. Count members
5. Quit
Please select your option now: 2
Please enter your name: 
You are a member
You are a member
You are a member
You are a member
You are a member
You are a member
You are a member
You are a member
You are a member
You are a member
You are not a member
Library Menu:

========================
1. Add members   
2. Is a member? 
3. Delete a member
4. Count members
5. Quit
Please select your option now:

What is wrong with what you posted?

Here's a hint to make your testing go faster. Use a String for the Scanner to get its values from vs the console. This is only an example, YOU NEED to put in the correct values.

Scanner input = new Scanner("1 Name\n x\n c\n v\n b\n z\n x\n c\n v\n b\n 5\n");

When you have the logic working for this input, replace it with the System.in

Thank you very much, I give up for today.

You need to see the data that the program is working with in case it is not what you expect.

Add the printlns after ALL the variables that are read in to show their values BEFORE you use them in the program.

shows 10 times "You are a member".

It prints that message for every compare. If there are 10 compares that fail then it will print that message 10 times.
How many times do you want it printed?

What's your current code?

import java.util.*;
import javax.swing.JOptionPane;

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();
		
		//String name  = JOptionPane.showInputDialog(null,
      //"Please enter your name:",
      //"Your name", JOptionPane.QUESTION_MESSAGE);
		
		for (int i = 0; i<members.length; i++)
		{
			if 
				(name.equals(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.equals(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();
    }
    
    
}
Member Avatar for ztini

larry, the biggest issue you are facing is that you are creating multiple nested loops. Each of your methods calls "mainMenu();". You should remove these from your methods and allow the program to return naturally to the mainMenu() method. That is, after the last line is executed in the method, the program will return to the line of code that called the method.

What you are doing is creating a loop, going to a method, going to the menu, starting a new loop without completing the first, and so on. Do it enough times and you'll run out of heap space :P

import java.util.*;

Also, you should not blindly bring in entire packages. Its always best to call in the specific class you need, intead of the entire package. Like this:

import java.util.Scanner;
Scanner input = new Scanner(System.in);

And, its a good idea to declare global variables as private in almost every situation. Unless you are declaring them as final static.

public void isMember()

Methods starting with "is" should generally return a boolean. This is typically just a logic method. This is not a law, per say, but an unwritten rule.

String members[] = new String [10];

Is there a particular reason for using an Array over any of the other data types? Is this a requirement or a preference? If its a preference, you might want to consider something like an ArrayList or even a HashSet. Also, the above code will technically work, but you should really use the following syntax:

String[] members = new String[10];

Also, looking at your code, you might want to nest your menu within a do-while loop. You should always use a do-while over a while if you want the loop to run at least once (in this case you do).

So, if you used an ArrayList and a do-while, you might have something like this:

import java.util.ArrayList;
import java.util.Scanner;

public class Membership {

	private Scanner input = new Scanner(System.in);
	private ArrayList<String> members = new ArrayList<String>();
		// ArrayList allows you search, add, and delete very easily and efficiently
		// However, you may decide to use a HashSet in this case, which would
		// not allow duplicate members in your data set.

	public void mainMenu() {
		
		do { // do at least once
			System.out.println("Library Menu:\n");
			System.out.println("========================");
			System.out.println("1. Add member   ");
			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: ");
	        
			switch (input.nextInt()) { 
				// note how all of the menu and interaction is in one place
				// and the logical evaluation of your data is contained in the methods.
				// this is generally how you want to group code
				case 1:
					System.out.print("Member to Add: ");
					addMember(input.next());
					break;
				case 2: 
					System.out.print("Member to Validate: ");
					if (isMember(input.next()))
						System.out.println("You are a member.");
					else
						System.out.println("You are not a member");
					break;
				case 3:
					System.out.print("Member to Delete: ");
					if (deleteMember(input.next()))
						System.out.println("Member deleted.");
					else
						System.out.println("Member not found.");
					break;
				case 4:
					System.out.println("The number of members is " + countMembers());
					break;
				case 5:
		        		System.out.println("Goodbye! \n");
		        		System.exit(0);
			}       	
		} while (true); // keeping looping until 5 is entered
	}
	
	public void addMember(String member) {
		members.add(member);
	}
	
	public boolean isMember(String member) {
		return members.contains(member);
	}
	
	public boolean deleteMember(String member) {
		return members.remove(member);
	}
	
	public int countMembers() {
		return members.size();
	}

	public static void main (String[] args) {
		new Membership().mainMenu();
	}
}

This might be outside the scope of your assignment, but at the very least, hopefully you get inspired by something new.

2ztini:
Yes, this is a requirement to use arrays.
Nevertheless, I am inspired by your elegant code example, and how it works, thank you!

Well, now I escaped printing out 10 times by using boolian value.
Also, next() works better than nextLine() in this case.
Nevertheless, validation goes wrong (in public void validateMember()):

import java.util.Scanner;


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:
        	validateMember();
        	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 enter  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   validateMember()
	{
		System.out.println("Please enter your name: ");
		String name = input.next();
		boolean isMember;

		
		for (int i = 0; i<members.length; i++)
			
		{	System.out.print(members[i]);
			if 
				(members[i].equalsIgnoreCase(name))
			{	
				isMember = true;
				
			}
			else
			{	
				isMember = false;
			}	
		}
		
		if (isMember = true )
		{
			System.out.println("You are a member");	
		}
		else
		{
			System.out.println("You are not a member");
		}
		mainMenu();
	}
	
	public void deleteMember()
	{
		System.out.println("Please enter the name to remove");
		String name = input.next();
		boolean isMember;
		int i;
		
		for (i = 0; i<members.length; i++)
		{
			if (members[i].equalsIgnoreCase(name))
			{
				isMember = true;
			}
			else
			{
				isMember = false;
			}	
		}
		
		if (isMember = true)
		{
			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();
    }
    
    
}

when I enter whatever name to compare with already entered, it validates "ok":

--------------------Configuration: <Default>--------------------
Library Menu:

========================
1. Add members   
2. Is a member? 
3. Delete a member
4. Count members
5. Quit
Please select your option now: 1
Please enter  10 names to be added
John
Michael
Jane
Babba
Arnold
Stevie
George
Natalie
Rania
Walter
Library Menu:

========================
1. Add members   
2. Is a member? 
3. Delete a member
4. Count members
5. Quit
Please select your option now: 2
Please enter your name: 
Larry
You are a member
Library Menu:

when I enter whatever name to compare with already entered, it validates "ok":

Where does it print out "ok"?
You show output but don't say what is wrong with it.

If you tried debugging your code by adding printlns as decscribed earlier, you might see where the code is working differently than what you expect.

I've found a bug in your code and would like to show you how to find bugs.
If you add a println for the value of isMember after your search loop and before you test it, the value printed should give a hint at what is wrong?

Or would you rather someone tell you where your bugs are?

hi ..... i think it would be better if u use next() instead of nextLine(); ........ well it is working for me better that way ........... I'm kinda new so I tried to do some modifications ....... please correct me if im wrong somewhere.......

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.next();
	// Consume the remaining newline
	}
	mainMenu();
	}
	 
	public void isMember()
	{
		int flag=0;
	System.out.println("Please enter your name");
	String name = input.next();
	for (int i = 0; i<members.length; i++)
	{
	if
	(name.equalsIgnoreCase(members[i]))
	{
	  flag=0;
	}
	else
	{
	flag++;
	}
	}
	if(flag==0)
	{
		System.out.println("You are a member");
	}
	else
	{
		System.out.println("You are not a member");
	}	
		
	mainMenu();
	}
	 
	public void deleteMember()
	{
		int flag=0;
	System.out.println("Please enter the name to remove");
	String name = input.next();
	for (int i = 0; i<members.length; i++)
	{
	if (name.equalsIgnoreCase(members[i]))
	{
	members[i] = null;
	System.out.println("Member deleted");
	}
	else
	{
	flag++;
	}
	}
	if(flag==10)
	{
		System.out.println("Name Not Found");
	}
	else
	{
	System.out.println("The new List is");
	for (int i = 0; i<members.length; i++)
	{
		if(members[i]!=null)
		System.out.println(members[i]);
	}
	}
	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) {
		// TODO Auto-generated method stub
		new Membership().mainMenu();
	}

}

Thanks

please correct me if im wrong somewhere

Does the program now work the way you want it to?

The one change I suggest right now is to remove the call to mainMenu() that is in all the methods. That makes for a "recursive" call which will eventually be a problem.
You should change the main program to have a loop that calls each method as needed. Each of the methods returns to the main loop which then asks the user what to do next.
The type of loop could be a while(true) to make it loop forever until the System.exit() call.
A better way would be for the while loop to use a boolean to keep it going and have the quit case change the boolean to allow the loop to exit.
For example:

boolean whileWorking = true;  // control the loop until done working
   ...
  // Loop until user tells us to exit
  while (whileWorking) {
     ...
     case 5:
        whileWorking = false; // we're done, allow loop to exit
        break;
     } // end switch
  } // end while(whileWorking)

ok .... i actually just modified that methods add members ,,, is member and delete one.....others are larrys ...........anyway thanks ....

Thank you guys for trying to help me.

newcoder310 ,
it doesn't work correctly as well, in these 2 methods, I tested with values.
I changed the switch statement adding boolean whileWorking.
I will try to ask prof. I can't waist your time furthermore, so I will mark the topic solved.

Well, it is solved really. Thank you all again!
The only thing to work loop correctly in isMember method was to insert break after finding the flag, final version:

import java.util.*;
 
public class LibMembership {
 
 
	Scanner input = new Scanner(System.in);
	String members[] = new String [10];
 
	public void mainMenu()
	{
	int selection = 0;
	boolean whileWorking = true;
	
 while (whileWorking) {
	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);
	}
	whileWorking = false; 
		}
	}
 
 
	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.next();

	}
	mainMenu();
	}
 
	public void isMember()
	{
		int flag=0;
	System.out.println("Please enter your name");
	String name = input.next();
	for (int i = 0; i<members.length; i++)
	{
	if
	(name.equalsIgnoreCase(members[i]))
	{
	  flag=0;
	  break;
	}
	else
	{
	flag++;
	}
	}
	if(flag==0)
	{
		System.out.println("You are a member");
	}
	else
	{
		System.out.println("You are not a member");
	}	
 
	mainMenu();
	}
 
	public void deleteMember()
	{
		int flag=0;
	System.out.println("Please enter the name to remove");
	String name = input.next();
	for (int i = 0; i<members.length; i++)
	{
	if (name.equalsIgnoreCase(members[i]))
	{
	members[i] = null;
	System.out.println("Member deleted");
	}
	else
	{
	flag++;
	}
	}
	if(flag==10)
	{
		System.out.println("Name Not Found");
	}
	else
	{
	System.out.println("The new List is");
	for (int i = 0; i<members.length; i++)
	{
		if(members[i]!=null)
		System.out.println(members[i]);
	}
	}
	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 LibMembership().mainMenu();
	}
 
}

You still have the recursive calls to mainMenu().
This is NOT a good programming technique. See a previous post that has a technique for code not to do recursive calls.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.