943,865 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 17792
  • Java RSS
Jun 21st, 2009
0

Search for a Specified String in an Array

Expand Post »
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:

Java Syntax (Toggle Plain Text)
  1. import java.util.Scanner;
  2. public class StringSearch
  3. {
  4. public static void main(String[] args)
  5. {
  6. // Initializing Array
  7. String[] nameSearch = {"Reebok", "Nike"};
  8.  
  9.  
  10. // Declaring Variables.
  11. String newName;
  12. int Results;
  13.  
  14. Scanner keyboard = new Scanner(System.in);
  15.  
  16. //Asking for input.
  17. System.out.print("\nPlease enter in a name: ");
  18. newName = keyboard.nextLine();
  19.  
  20. // Conditional statement.
  21. Results = nameSearch(nameSearch, newName);
  22.  
  23. if (Results == -1)
  24. {
  25. System.out.print("\nFound the name " + newName );
  26. }
  27. else if(Results != -1)
  28. {
  29. System.out.print("\nCannot find specified name " + newName);
  30. }
  31.  
  32. }
  33. public static int nameSearch(String[] names, String name)
  34. {
  35. for (int n = 0; n < names.length; n++)
  36. {
  37. if (names[n].equals(name))
  38. return n;
  39. }
  40.  
  41. return -1;
  42. }
  43. }
Last edited by peter_budo; Jun 8th, 2011 at 9:49 am. Reason: Added code tags to old post
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Knoxx is offline Offline
6 posts
since Jun 2009
Jun 22nd, 2009
0

Re: Search for a Specified String in an Array

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.
Moderator
Reputation Points: 1471
Solved Threads: 490
Industrious Poster
masijade is offline Offline
4,043 posts
since Feb 2006
Jun 22nd, 2009
0

Re: Search for a Specified String in an Array

Click to Expand / Collapse  Quote originally posted by masijade ...
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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Knoxx is offline Offline
6 posts
since Jun 2009
Jun 22nd, 2009
0

Re: Search for a Specified String in an Array

you can solve it by using StringTokenizer data type it is more easer
Reputation Points: 7
Solved Threads: 0
Newbie Poster
the_swan is offline Offline
21 posts
since Nov 2008
Jun 22nd, 2009
0

Re: Search for a Specified String in an Array

Click to Expand / Collapse  Quote originally posted by the_swan ...
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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Knoxx is offline Offline
6 posts
since Jun 2009
Jun 22nd, 2009
0

Re: Search for a Specified String in an Array

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.
Moderator
Reputation Points: 1471
Solved Threads: 490
Industrious Poster
masijade is offline Offline
4,043 posts
since Feb 2006
Jan 16th, 2011
0
Re: Search for a Specified String in an Array
try .equals() instead of = for strings.
Reputation Points: 10
Solved Threads: 1
Newbie Poster
davetheant is offline Offline
10 posts
since Jan 2011
Jan 16th, 2011
0
Re: Search for a Specified String in an Array
hey guys can u please tell me how to dowload a program(code) from the user problems..i always use copy n paste and sometimes it messes up the code,which ends up giving me unecesary job.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
LeeMan4 is offline Offline
2 posts
since Oct 2010
Jun 8th, 2011
1

RE: Search for a Specified String in an Array

you can use this code to make it more simple

decide
Java Syntax (Toggle Plain Text)
  1. for (String arg : yourArray) {
  2. if (yourArray == yourInput) {
  3. decide = true;
  4. System.out.println("in array");
  5. }else{
  6. System.out.println("not in array");
  7. }
Last edited by peter_budo; Jun 8th, 2011 at 9:47 am. Reason: Keep It Clear - Do wrap your programming code blocks within [code] ... [/code] tags
Reputation Points: 10
Solved Threads: 0
Newbie Poster
johnbucayan is offline Offline
1 posts
since Jun 2011
Jun 8th, 2011
1
Re: Search for a Specified String in an Array
you can use this code to make it more simple...
... unless
You want Java code
You want the brackets to match
You expect to use a loop variable somewhere within its loop
You want the test for "equal" Strings to work
You know that Strings and arrays are different things
You would like an answer within a year or two of when you post the problem

John: It's great that you want to contribute to this forum, but you will embarrass yourself less if you compile and test your code before posting it. Don't be put off, just be more careful.
J
Featured Poster
Reputation Points: 1924
Solved Threads: 951
Posting Expert
JamesCherrill is online now Online
5,788 posts
since Apr 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
This thread is currently closed and is not accepting any new replies.
Previous Thread in Java Forum Timeline: error when return object using jaxws webservice
Next Thread in Java Forum Timeline: Retrieving from ms access





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC