Welcome to Dream.In.Code
Become a Java Expert!

Join 150,423 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,098 people online right now. Registration is fast and FREE... Join Now!




Searching through arrays

 
Reply to this topicStart new topic

Searching through arrays, Need help on searching for string value in array

NassauBob
15 Mar, 2008 - 07:11 PM
Post #1

New D.I.C Head
*

Joined: 13 Mar, 2008
Posts: 6

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
User is offlineProfile CardPM
+Quote Post

Martyr2
RE: Searching Through Arrays
15 Mar, 2008 - 07:48 PM
Post #2

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,660



Thanked: 314 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
Instead of using name == names[x] try using names.equals(names[x]). The equals function is good for string comparisons and is a much better solution for comparing string values. Below is an example I whipped up for you to demonstrate...

java

public class comparestrings {
public static void main(String args[]) {
String[] names = new String[4];
names[0] = "John";
names[1] = "Mary";
names[2] = "Paul";
names[3] = "Marty";

// Get the index of Paul in our array list
int foundval = search("Paul",names);
System.out.println("Found paul at index: " + foundval);
}

// Search for a name in our array of strings
private static int search(String name, String[] nameslist) {
// Loop through the names and return the index value of paul
for (int i = 0; i < nameslist.length; i++) {

// Notice we use equals here to compare
if (name.equals(nameslist[i])) {
return i;
}
}

return -1;

}
}


So use equals for your comparison and things should work out great. Let us know if you continue to have problems. Enjoy!

"At DIC we be name hunting code ninjas!" decap.gif
User is offlineProfile CardPM
+Quote Post

NassauBob
RE: Searching Through Arrays
16 Mar, 2008 - 07:39 AM
Post #3

New D.I.C Head
*

Joined: 13 Mar, 2008
Posts: 6

THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU

Shane icon_up.gif biggrin.gif

QUOTE(Martyr2 @ 15 Mar, 2008 - 08:48 PM) *

Instead of using name == names[x] try using names.equals(names[x]). The equals function is good for string comparisons and is a much better solution for comparing string values. Below is an example I whipped up for you to demonstrate...

java

public class comparestrings {
public static void main(String args[]) {
String[] names = new String[4];
names[0] = "John";
names[1] = "Mary";
names[2] = "Paul";
names[3] = "Marty";

// Get the index of Paul in our array list
int foundval = search("Paul",names);
System.out.println("Found paul at index: " + foundval);
}

// Search for a name in our array of strings
private static int search(String name, String[] nameslist) {
// Loop through the names and return the index value of paul
for (int i = 0; i < nameslist.length; i++) {

// Notice we use equals here to compare
if (name.equals(nameslist[i])) {
return i;
}
}

return -1;

}
}


So use equals for your comparison and things should work out great. Let us know if you continue to have problems. Enjoy!

"At DIC we be name hunting code ninjas!" decap.gif


User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/9/09 08:32PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month