| | |
Search for a Specified String in an Array
![]() |
•
•
Join Date: Jun 2009
Posts: 5
Reputation:
Solved Threads: 0
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;
}
}
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;
}
}
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.
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
----------------------------------------------
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
•
•
Join Date: Jun 2009
Posts: 5
Reputation:
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.
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.
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.
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
----------------------------------------------
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
![]() |
Similar Threads
- Find string in array (VB.NET)
- string search....can i add an index? help.. (C++)
- From SQL string to array (PHP)
- Binary search string (C++)
- Search string array (C++)
- Search string? (C++)
Other Threads in the Java Forum
- Previous Thread: code executes, but file dosen't appear
- Next Thread: chat module
| Thread Tools | Search this Thread |
911 actionlistener addressbook android api append applet application array arrays automation binary blackberry block bluetooth character chat class client code component consumer csv database desktop developmenthelp eclipse error fractal ftp game givemetehcodez graphics gui html ide image integer j2me j2seprojects japplet java javaarraylist javac javaee javaprojects jni jpanel julia lego linked linux list loops mac map method methods mobile netbeans newbie number objects online oriented panel printf problem program programming project projects properties recursion replaydirector reporting researchinmotion rotatetext rsa scanner se server set singleton sms sort sql string swing test textfields threads time title tree tutorial-sample ubuntu update windows working






