| | |
Search for a Specified String in an Array
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
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 |
account android api applet application array arrays automation bidirectional binary birt bluetooth chat class classes client code columns component data database designadrawingapplicationusingjavajslider draw eclipse editor error errors event exception expand fractal game givemetehcodez graphics gui guidancer homework html ide image inetaddress inheritance input integer intellij j2me java javaprojects jlabel jme jni jpanel jtextfield julia linux list loop map method methods midlethttpconnection mobile mobiledevelopmentcreatejar monitoring myaggfun netbeans newbie nullpointerexception open-source plazmic print problem program programming project property recursion ria scanner screen search server set size sms smsspam sort sourcelabs splash sql sqlite static string subclass support swing testautomation threads tree windows






