0

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?

2
  • 2
    It will be much easier if you use a List in stead of an Array. Commented Nov 17, 2012 at 6:53
  • 3
    why don't you show some code of something you have already tried, then we help you where you're stuck. Commented Nov 17, 2012 at 7:00

5 Answers 5

2

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);
Sign up to request clarification or add additional context in comments.

5 Comments

thank you so much for your fast response! one quick question, why does Results return -1 if it's found the name?
it is used as a check in the if else if block with Results. It is just an example, feel free to modify.
oh fail, i didn't see the nameSearch method, i understand it now. thank you! this was great help!
@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.
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.
0

convert your array to String(char[] value) then use something like public String replaceAll(String regex,String replacement)

Comments

0

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

That was hard to type from my phone!
haha thank you very much! one quick question: when you convert the list back into an array, why is there a zero?
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/…
... 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.
0
String arrayvalues[] ={some set of strings};

public void methodToReplace(String st)    
{
String inputfromuser =st;

    for(int i=0;i<arrayvalues;i++)
    {
    if(inputfromuser.equals(arrayvalues[i]))
         arrayvalues[i] ="new values";

    }

}

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.