I have an array filled with a hundred strings. I would like to be able to use an input given by the user, pass it through a method parameter, search for that exact string, and remove it. How would I go about doing this? Furthermore, how would I do the same thing but add it instead?
-
2It will be much easier if you use a List in stead of an Array.jahroy– jahroy2012-11-17 06:53:16 +00:00Commented Nov 17, 2012 at 6:53
-
3why don't you show some code of something you have already tried, then we help you where you're stuck.coder– coder2012-11-17 07:00:01 +00:00Commented Nov 17, 2012 at 7:00
Add a comment
|
5 Answers
Something like this ??
import java.util.Scanner;
public class StringSearch
{
public static void main(String[] args)
{
String[] nameArray = {"Stack", "Overflow"};
String newName;
int index;
Scanner keyboard = new Scanner(System.in);
System.out.print("\nPlease enter in a name: ");
newName = keyboard.nextLine();
index = nameSearch(nameArray, newName);
if (index != -1) {
System.out.print("\nFound the name " + newName );
}
else {
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;
}
}
If you want to implement remove you can delete something from an array like this
List<String> list = new ArrayList<String>(Arrays.asList(array));
list.remove("thing to remove");
array = list.toArray(array);
5 Comments
Jay
thank you so much for your fast response! one quick question, why does Results return -1 if it's found the name?
Yves_T
it is used as a check in the if else if block with Results. It is just an example, feel free to modify.
Jay
oh fail, i didn't see the nameSearch method, i understand it now. thank you! this was great help!
Surender Thakran
@Yves_T : Not to be critical here but you botched your
if statement there. Results is -1 if the string is not found, not the other way around.jahroy
There's no reason to use list.removeAll(). You can just do this in stead:
list.remove("thing to remove"). I'm going to edit the answer.It would be much easier to use a List in stead of an array.
You can convert an array into a List like this:
String[] stringArray = populateArray();
List<String> stringList = Arrays.asList(stringArray);
You can convert a List to an array like this:
String[] stringArray = stringList.toArray(new String[0]);
4 Comments
jahroy
That was hard to type from my phone!
Jay
haha thank you very much! one quick question: when you convert the list back into an array, why is there a zero?
jahroy
It doesn't have to be a zero. All that matters is that you provide an array of the correct type. You could also use this:
stringList.toArray(new String[stringList.size()]); I'm not sure if there's any difference. You can read the documentation to find out: docs.oracle.com/javase/6/docs/api/java/util/…jahroy
... and the solution I'd suggest is to use a List in stead of an array if possible. There's no reason to use an array or to convert between the two if it's not necessary.
The thing you can do is:
1) Whatever the element you have in array, just convert that array into List
2) Once you get the list, the all things are easier for you.
3) Just use contains methos to find if this list contains the specified element.
4) And accordingly use Add/Remove method of List.