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

Search for a Specified String in an Array

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;
}
}
Knoxx
Newbie Poster
6 posts since Jun 2009
Reputation Points: 10
Solved Threads: 0
 

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.

masijade
Industrious Poster
Moderator
4,253 posts since Feb 2006
Reputation Points: 1,471
Solved Threads: 494
 

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.

Knoxx
Newbie Poster
6 posts since Jun 2009
Reputation Points: 10
Solved Threads: 0
 

you can solve it by using StringTokenizer data type it is more easer

the_swan
Newbie Poster
21 posts since Nov 2008
Reputation Points: 7
Solved Threads: 0
 
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.

Knoxx
Newbie Poster
6 posts since Jun 2009
Reputation Points: 10
Solved Threads: 0
 

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.

masijade
Industrious Poster
Moderator
4,253 posts since Feb 2006
Reputation Points: 1,471
Solved Threads: 494
 

try .equals() instead of = for strings.

davetheant
Newbie Poster
10 posts since Jan 2011
Reputation Points: 10
Solved Threads: 1
 

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.

LeeMan4
Newbie Poster
2 posts since Oct 2010
Reputation Points: 10
Solved Threads: 0
 

you can use this code to make it more simple

decide

for (String arg : yourArray) {
	if (yourArray == yourInput) {
	decide = true;
		System.out.println("in array");
	}else{
	System.out.println("not in array");
    }
johnbucayan
Newbie Poster
1 post since Jun 2011
Reputation Points: 10
Solved Threads: 0
 
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

JamesCherrill
Posting Genius
Moderator
6,371 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You