//Address Book Part 1
//IT 215
//George Williams

import java.util.Scanner;

class menuOperations
{
	//Initilize Arrays to store information
	String[] fNames = new String[5];
	String[] lNames = new String[5];
	String[] dAddress = new String[5];
	String[] dCity = new String[5];
	String[] dState = new String[5];
	int[] dZip = new int[5];
	int[] dDonations = new int[5];
	int[] dDonationTimes = new int[5];

	//Initilize variables for user input
	String fName = " ";
	String lName,address,city,state;
	int zip,amount,times,i,j,x;
	boolean valid = false;

	String uChoice2;
	int oldDAmount;
	int oldDTimes;

    //Initilize variables for searching
	String sFName,sLName,sAddress,sCity,sState;
	int sZip,sDRange,sDTRange;


	Scanner in = new Scanner(System.in); //For user input


    /*
    =====================================
    =Method Name: newEntry              =
    =Purpose: Allow user to input       =
    =new donater data into the program  =
    =====================================
    */

	public void newEntry()
	{
		//Setup check to make sure we do not enter too many array elements
		if (fName == " ")
		{
			int i = 0;
		}

        //Check to make sure we are under 5 (0-4)
		if (i == fNames.length)
		{
			System.out.println("Sorry, your address book is full.  You can only store 5 entires per book."); //If we have 49 elements filled display this message
			System.out.println();
		}

		else
		{

			for (;i < fNames.length; i++)
			{
				while (!valid)
				{
					System.out.println("Please enter the first name of the person:");
					fName = in.nextLine(); //Get First Name

					fNames[i] = fName; //Set current element to fName

					if (fNames[i] != fName) //Check to make sure the name was properly entered
					{
						System.out.println("There was a probelm entering the name."); //If check fails display this message
						System.out.println();
						}

						else
						{
							System.out.println("The name was entered successfully.");
							System.out.println();
							valid = true;

						}
					}
					valid = false;



		    	while (!valid)
		    	{

					System.out.println("Please enter the last name of the person:");
					lName = in.nextLine(); //Get Last Name

					lNames[i] = lName; //Set current element to lName

					if (lNames[i] != lName) //Check to make sure the name was properly entered
					{
						System.out.println("There was a probelm entering the name."); //If check fails display this message
						System.out.println();
					}

					else
					{
						System.out.println("The name was entered successfully.");
						System.out.println();
						valid = true;
					}
				}
				valid = false;




		    	while (!valid)
		    	{

					System.out.println("Please enter the address of the person:");
					address = in.nextLine(); //Get Address

					dAddress[i] = address; //Set current element to address

					if (dAddress[i] != address) //Check to make sure the address was properly entered
					{
						System.out.println("There was a probelm entering the address."); //If check fails display this message
						System.out.println();
					}

					else
					{
						System.out.println("The name was entered successfully.");
						System.out.println();
						valid = true;
					}
				}
				valid = false;




		    	while (!valid)
				{

					System.out.println("Please enter the City of the person:");
					city = in.nextLine(); //Get City

					dCity[i] = city; //Set current element to city

					if (dCity[i] != city) //Check to make sure the city was properly entered
					{
						System.out.println("There was a probelm entering the address."); //If check fails display this message
						System.out.println();
					}

					else
					{
						System.out.println("The name was entered successfully.");
						System.out.println();
						valid = true;
					}
				}
				valid = false;





		    	while (!valid)
				{

					System.out.println("Please enter the State of the person:");
					state = in.nextLine(); //Get State

					dState[i] = state; //Set current element to state

					if (dState[i] != state) //Check to make sure the state was properly entered
					{
						System.out.println("There was a probelm entering the state."); //If check fails display this message
						System.out.println();
					}

					else
					{
						System.out.println("The name was entered successfully.");
						System.out.println();
						valid = true;
					}
				}
				valid = false;




		    	while (!valid)
				{

					System.out.println("Please enter the Zip Code of the person:");
					zip = in.nextInt(); //Get Zip

					dZip[i] = zip; //Set current element to zip

					if (dZip[i] != zip) //Check to make sure the zip was properly entered
					{
						System.out.println("There was a probelm entering the zip."); //If check fails display this message
						System.out.println();
					}

					else
					{
						System.out.println("The name was entered successfully.");
						System.out.println();
						valid = true;
					}
				}
				valid = false;



		    	while (!valid)
				{

					System.out.println("Please enter the amount of the donation for the person:");
					amount = in.nextInt(); //Get Donation Amount

					dDonations[i] = amount; //Set current element to amount
					dDonationTimes[i] = 1; //Set donation times to 1, since this is the first time this person has donated.  Setting as static also prevents looping issues

					if (dDonations[i] != amount) //Check to make sure the zip was properly entered
					{
						System.out.println("There was a probelm entering the donation.\n"); //If check fails display this message
						System.out.println();
					}

					else
					{
						System.out.println("The donation was entered successfully.");
						System.out.println();
						valid = true;
					}
				}
				valid = false;
			} //End of Foor Loop

		}
    } //End newEntry



    /*
    =====================================
    =Method Name: newDonation           =
    =Purpose: Allow user enter new      =
    =donations by specific individuals  =
    =via address search                 =
    =====================================
    */

Scanner in2 = new Scanner(System.in); //For user input

    public void newDonation()
    {
		boolean found = false;

		System.out.println("Please enter the address of the individual whom you want to add a new donation amount to.");
		address = in2.nextLine();

		for (int j2 = 0; j2 < dAddress.length; j2++)
		{
		    if ((address) .equals (dAddress[j2])) //Is there an address equal to the user entered address?
			{
				x = j2;
				found = true;
				break; //If so, break loop to preserve j for later in the method.
			}

			if (j2 == 4) //If j2 is 4 then the address was not found
			{
				System.out.println("The address you entered was not found.");
				System.out.println();
			}
		}
		System.out.println();

		if (found)
		{
			System.out.println("Address found!");
			System.out.println();

			System.out.println("Is this the entry your were looking for?");
			System.out.println("Entry Number " + x + " : " + fNames[x] + " " + lNames[x] + " " + dAddress[x]  + " " +  dCity[x]  + " " +  dState[x]  + " " +  dZip[x]  + " " +  dDonations[x]  + " " +  dDonationTimes[x] + " (Yes/No)");

			uChoice2 = in2.nextLine();
			System.out.println();

			if ((uChoice2) .equals ("Yes"))
			{
				System.out.println("Please enter the number of new donations:");
				times = in2.nextInt();
				System.out.println();

				System.out.println("Please enter the total amount of the dontions (round down to the nearest dollar)");
				amount = in2.nextInt();
				System.out.println();

				//Add donations
				oldDAmount = dDonations[x];
				amount += oldDAmount;
				dDonations[x] = amount;

				//Add donation times
				oldDTimes = dDonationTimes[x];
				times += oldDTimes;
				dDonationTimes[x] = times;

				System.out.println("This entry has now been updated");
				System.out.println();
			}
		}
	} //End newDonation



    /*
    =====================================
    =Method Name: search                =
    =Purpose: Allow user to search the  =
    =donater data for specific entries  =
    =====================================
    */

Scanner in3 = new Scanner(System.in); //For user input
	public void search()
	{
		System.out.println("Please enter the name you wish to search for, first or last name ONLY");
		sFName = in3.nextLine();
		System.out.println();

		System.out.println(sFName);

		for (j = 0; j < fNames.length; j++)
		{
			if ((sFName) .equals (fNames[j])) //if sFName equals array element, then print all information for entry.
			{
				System.out.println("Entry Number " + j + " : " + fNames[j] + " " + lNames[j] + " " + dAddress[j]  + " " +  dCity[j]  + " " +  dState[j]  + " " +  dZip[j]  + " " +  dDonations[j]  + " " +  dDonationTimes[j]);

			}
		}
		System.out.println();
	} //End search



    /*
    =====================================
    =Method Name: disAll                =
    =Purpose: Prints all information for=
    =all entries present in arrays      =
    =====================================
    */

    public void dispAll()
    {
		System.out.println("Current entries and information:");
		System.out.println();

		for (int i2 = 0; i2 < fNames.length; i2++)
		{
			if (!(fNames[i2]) .equals (null)) //Incase empty element.  Will cause exception, but not in this case as all elements will be filled.
			{
				System.out.println("Entry Number " + i2 + " : " + fNames[i2] + " " + lNames[i2] + " " + dAddress[i2]  + " " +  dCity[i2]  + " " +  dState[i2]  + " " +  dZip[i2]  + " " +  dDonations[i2]  + " " +  dDonationTimes[i2]);
			}
		}
		System.out.println();
	} //End dispAll
} //End menuOperations

//.equals operator workaround courtasy of Reilly, D. (2006). Top Ten Errors Java Programmers Make.  Retrieved on Jan 9, 2011 from http://www.javacoffeebreak.com/articles/toptenerrors.html




public class it215_w2_addressbook
{

	public static void main(String[] args)
	{
		//String fName,lName;
		//int dAmount,dTimes;
		int uChoice;
		boolean quit = false;

		Scanner in = new Scanner(System.in);
		menuOperations mO = new menuOperations();


		System.out.println("elcome to your Electronic Address Book, or EAB.");

		while (!quit)
		{
			System.out.println("Please choose an option from the following menu:");

			System.out.println("1) New Entry");
			System.out.println("2) New Donation");
			System.out.println("3) Search");
			System.out.println("4) Display All");
		    System.out.println("5) Quit Program");

			System.out.printf(">");

			uChoice = in.nextInt();
			System.out.println();

			switch (uChoice)
			{
				case 1:
				{
					mO.newEntry(); //call newEntry
					break;
				}

				case 2:
				{
					mO.newDonation(); //call newDonation
					break;
				}


				case 3:
				{
					mO.search(); //call search
					break;
				}

				case 4:
				{
					mO.dispAll(); //Call dispAll
					break;
				}

				case 5:
				{
					quit = true; //Quit program
					break;
				}


				default: //if no case matches
				{
					mO.newEntry(); //enter new entries
					break;
				}
			}
		}
	}
}// End Main

So, what is going on is that once the program reaches the end of the newEntry method the next time an in.nextLine or whatever the type is (nextInt for example), instead of asking for the user to input the data, it passes the line as if the user has.

This results in the first name of the entry always being blank after the first entry. I have tried several things, such as commenting off various code lines and in.next commands, but it has persisted.

Before I made some changes the code used to go back out to the main method each time one entry was made. While in the main class in.next works correctly in this case, but as soon as we re-enter the menuOptions class it also did the above, which leads me to think that my code is causing it.

I have about 2 weeks worth of experience in Java, so I can not figure out where the problem is, can anyone spot it?

Uhm, nextInt(), etc, do not "consume" the newline that follows it, so if you use "nextLine" after using something like nextInt() you will get whatever there was between the type retrieved and the next newline (usually nothing if all you expected was a single type on that line). So, if you are expected a single type on a line (and you insist on using Scanner for this) then call nextInt (or whatever) and follow it immediately with nextLine (again, assuming you are only expecting a single input on a single line, otherwise call it after retreiving the last expected type).

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.