Hi all. I am working on a homework problem that is really stumping me. I had to create a 2d string array, fill it, then search through the first column.
It is a 2D array, first column is name, and 2nd, 3rd and 4th columns are three different phone numbers.
I have shortened the name column to just the first initial of the last name for brevity in my code until I can figure out what I am doing wrong.
I get the values to input from a text file no problem. I can even print the array to the screen.
The problem I am having is this: let's say that the 8 elements of the array have the first column with values such as A, B, C, D, etc.
When I try to search for an input character, and the user enters A, or any other character that is definitely present in the array, the search completes without finding the value, even though I know it is there.
Here is the code for the search method. (Please assume that I have declared the 2d array, and the text file is present and filled properly.) I also used a 1 dimensional array, populated from the first column of the 2d array, to attempt the search in another fashion, but got the exact same result this way too. I hope someone can help me. I bet it is an easy solution, and I am looking right past it.
CODE
public static void findPhones()
{
Scanner console = new Scanner(System.in);
String name;
String[] names = new String[8];
for(int x = 0; x < book.length; x++)
{
names[x] = book[x][0];
System.out.println(names[x]);
}
System.out.println("Please enter the first letter of the person's name): ");
name = console.next();
for(int x = 0; x < names.length; x++)
{
if (name == names[x])
System.out.println("Yes!");
else
System.out.println("No");
}
}
I get 8 No's every time, no matter what I enter. Why?
Shane
This post has been edited by NassauBob: 15 Mar, 2008 - 07:15 PM