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;
}
}

Recommended Answers

All 9 Replies

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.

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.

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

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.

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.

try .equals() instead of = for strings.

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.

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");
    }

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

commented: Well summarized +16
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.