Search for a Specified String in an Array

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Jun 2009
Posts: 5
Reputation: Knoxx is an unknown quantity at this point 
Solved Threads: 0
Knoxx Knoxx is offline Offline
Newbie Poster

Search for a Specified String in an Array

 
0
  #1
Jun 21st, 2009
i am a new programmer trying to make a program, which askes the user for a string input and searchs for the string in an array, and prints "Found: ("string") and the position of the string in the array. this is what i have done soo far:

import java.util.Scanner;
public class StringSearch
{
public static void main(String[] args)
{
// Initializing Array
String[] nameSearch = {"Reebok", "Nike"};


// Declaring Variables.
String newName;
int Results;

Scanner keyboard = new Scanner(System.in);

//Asking for input.
System.out.print("\nPlease enter in a name: ");
newName = keyboard.nextLine();

// Conditional statement.
Results = nameSearch(nameSearch, newName);

if (Results == -1)
{
System.out.print("\nFound the name " + newName );
}
else if(Results != -1)
{
System.out.print("\nCannot find specified name " + newName);
}

}
public static int nameSearch(String[] names, String name)
{
for (int n = 0; n < names.length; n++)
{
if (names[n].equals(name))
return n;
}

return -1;
}
}
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 2,415
Reputation: masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of 
Solved Threads: 256
Moderator
masijade's Avatar
masijade masijade is offline Offline
Nearly a Posting Maven

Re: Search for a Specified String in an Array

 
0
  #2
Jun 22nd, 2009
I'm sorry, but your question is?

Is it producing any exceptions? Are there compiler messages? Or is it producing something you didn't expect, in which case, exactly how does the actual result differ from the expected result?

And, when you post code, use code tags.
Java Programmer and Sun Systems Administrator

----------------------------------------------

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 5
Reputation: Knoxx is an unknown quantity at this point 
Solved Threads: 0
Knoxx Knoxx is offline Offline
Newbie Poster

Re: Search for a Specified String in an Array

 
0
  #3
Jun 22nd, 2009
Originally Posted by masijade View Post
I'm sorry, but your question is?

Is it producing any exceptions? Are there compiler messages? Or is it producing something you didn't expect, in which case, exactly how does the actual result differ from the expected result?

And, when you post code, use code tags.

When testing the program, it always returns false even when i enter a string that matches one in the array. i want the program to identify if the input matches any of the strings present in the array, if so it prints that indeed the string is present and also prints its index. if not, it prints that the string is not found.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 21
Reputation: the_swan is an unknown quantity at this point 
Solved Threads: 0
the_swan the_swan is offline Offline
Newbie Poster

Re: Search for a Specified String in an Array

 
0
  #4
Jun 22nd, 2009
you can solve it by using StringTokenizer data type it is more easer
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 5
Reputation: Knoxx is an unknown quantity at this point 
Solved Threads: 0
Knoxx Knoxx is offline Offline
Newbie Poster

Re: Search for a Specified String in an Array

 
0
  #5
Jun 22nd, 2009
Originally Posted by the_swan View Post
you can solve it by using StringTokenizer data type it is more easer
I've never worked with it , infact have no idea how to implement it into my code.
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 2,415
Reputation: masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of 
Solved Threads: 256
Moderator
masijade's Avatar
masijade masijade is offline Offline
Nearly a Posting Maven

Re: Search for a Specified String in an Array

 
0
  #6
Jun 22nd, 2009
StringTokenizer has nothing to do with this.

Also, look at your code and answer me these four questions.

- In the nameSearch method, what will be returned when the name is not found?

- In the main method (in your if statements), what will you print when the method returns that value?

- In the nameSearch method, what possible values will be returned when the name is found (e.g. 0 or higher)?

- In the main method (in your if statements), what will you print when the method returns one of those values?

Now, like I said, look at your code and answer those questions, don't simply answer them with what you intended.
Last edited by masijade; Jun 22nd, 2009 at 4:04 am.
Java Programmer and Sun Systems Administrator

----------------------------------------------

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC