-2

I have a list of words that I've turned into a string array. I ask the user to enter a character, and my code must find whether that character is in one of the words in the list of words and add that word to a new list and removed from the original list. How do I do that? Here's

String[] temps2 = newList.toArray(new String[0]); //turns list to string array.
System.out.println("\n pick a letter.");
letter_1 = input.next()charAt(0); 
for (String b : temps2){
for(char ch : b.toCharArray()){
    if letter_1==ch){
        temps2.remove(b);//don't think you can do that. 
Skye Grayson
  • 17
  • 1
  • 1
  • 1
  • Welcome to Stack Overflow! Please [take the tour](http://stackoverflow.com/tour) to see how the site works and what questions are on topic here. See also: [Why is "Can someone help me?" not an actual question?](http://meta.stackoverflow.com/q/284236) – Joe C Apr 10 '17 at 21:25
  • 1
    String has methods that you can use -- check them out by looking up the Java String API. – Hovercraft Full Of Eels Apr 10 '17 at 21:25
  • https://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html#remove(java.lang.Object) – Compass Apr 10 '17 at 21:25
  • Java arrays are not dynamic, once assigned you cannot change them so the only possible thing you could do to "remove" them from the array is to set that particular string to null and even that is not really removing, you should look into ArrayList and instead of looping you can then use the contains method and indexOf to find the item and remove it. – Ousmane D. Apr 10 '17 at 21:28
  • Welcome to Stack Overflow! Please read [How do I ask a good question?](http://stackoverflow.com/help/how-to-ask) before attempting to ask more questions. –  Apr 11 '17 at 04:31
  • Welcome to Stack Overflow! Please read [What types of questions should I avoid asking?](http://stackoverflow.com/help/dont-ask) before attempting to ask more questions. –  Apr 11 '17 at 04:31

2 Answers2

-1

If making an String array is not a necessity for you can try cloning your ArrayList called newList

ArrayList<String> newlist2 = (ArrayList<String>)newList.clone();

Then from this list you can check which Strings have the char ch and remove that. This approach should be easier for you as list has functions to remove elements.

ArrayList<String>finallist = new ArrayList<String>();
int i = 0 ;
for(String s : newlist2)
{
    for(char c : s.toCharArray())
    {
       if(ch == c)
       {
        finallist.add(newList2.remove(i));
         break;

        }
    i++

    }
}

This way finallist will have only the Strings that have the char ch

Akshat Bansal
  • 143
  • 3
  • 13
  • Gave me this error: Exception in thread "main" java.util.ConcurrentModificationException[Fifth, Sixth, Three] at java.util.AbstractList$Itr.checkForComodification(AbstractList.java:372) at java.util.AbstractList$Itr.next(AbstractList.java:343) at Game.main(Game.java:71) – Skye Grayson Apr 10 '17 at 23:36
-1

Instead of having the for loop count every char like you did

for(char ch : b.toCharArray()){...

You could do something like this:

for(int chnum ; chnum = b.length(); chnum++){
    char[] chararray = b.toCharArray();
    if(letter_1 == chararray[chnum]){
        chararray[chnum] = ... (remove it since you have it's array location)
    }
}

should work.

Justin
  • 31
  • 7