954,536 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Having some problems with arrays

Here is my code at the moment:

import java.util.Scanner;

class menuOperations
{
	String[] fNames = new String[50];
	String[] lNames = new String[50];

	String fName = " ";
	String lName = " ";

	String sFName = " ";
	String sLName = " ";

	int dAmount,dTimes,i;

	Scanner in = new Scanner(System.in);


	public void newEntry()
	{
		if (fName == " ")
		{
			int i = 0;
		}


		if (i == 50)
		{
			System.out.println("Sorry, your address book is full.  You can only store 50 entires per book.");
		}

		else
		{
			System.out.println("Please enter the first name of the person:");
			fName = in.nextLine();

			fNames[i] = fName;

			if (fNames[i] != fName)
			{
				System.out.println("There was a probelm entering the name.");
				}

				else
				{
					System.out.println("The name was entered successfully.");
					i += 1;

			}
		}
	} //End newEntry

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

		System.out.println(sFName);

		for (int j = 0; j < fNames.length; j++)
		{
			if (fNames[j] == sFName)
			{
				System.out.println(fNames[j]);
			}
		}
	} //End search

}





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("Welcome 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) Search");
			System.out.println("3) Display All");
			System.out.println("4) Load Address Book");
			System.out.println("5) Quit Program and Save Address Book");

			System.out.printf(">");

			uChoice = in.nextInt();

			switch (uChoice)
			{
				case 2:
				{
					mO.search();
					break;
				}

				case 5:
				{
					quit = true;
					break;
				}


				default:
				{
					mO.newEntry();
					break;
				}
			}
		}
	}
}


So, now my main problem is in the search method. At the for loop at the end the code should take the entered name, which I have verified is saved correctly via the system.out line, and then search the array for matching names and print them.

When I run the program, however, it does now print out these array elements. So far, everything I have found on google states that the print command used should work.

working in:

java version "1.6.0_23"
Java(TM) SE Runtime Environment (build 1.6.0_23-b05)
Java HotSpot(TM) Client VM (build 19.0-b09, mixed mode, sharing)

Geowil
Newbie Poster
16 posts since Oct 2009
Reputation Points: 10
Solved Threads: 0
 

I don't understand your question, What are you trying to do?

Akill10
Posting Pro
575 posts since Sep 2010
Reputation Points: 115
Solved Threads: 80
 

After some additional testing, I found that Java is slightly different from C++, my first language. You can not compare strings with the == operator, doh. I switched that to .equals and bingo, it works.

@Akill10

This is an assignment for a class. We are supposed to make an address book that the user can enter entries, search for a specific entry, and be able to save the address book. I was trying to figure out a way to search for a specific name via iterating one of the name arrays and looking for any elements with that specific name.

Problem was as stated above.

Geowil
Newbie Poster
16 posts since Oct 2009
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: