2

I need to have user input the name they would like to have removed then find the index of in the array that, that name is held. Then I need to remove the name along with the price and rating. I may only use parallel arrays. I'm not sure if they other part is running successfully because I am trying to use .remove() and I get the error:

cannot find symbol

symbol: method remove(int)

location: variable array1 of type String[]

code

public static void removeGames(Scanner keyboard, String[] array1,            
        double[] array2, double[] array3, int currentLength)
{
    String removeInput;

    System.out.println("Enter the name of the game you would like to remove"
            + " from the list: ");
    removeInput = keyboard.next();

    for(int i = 0; i < array1.length; i++)
    {
        if(removeInput.equalsIgnoreCase(array1[i]))
        {
            array1.remove(i);
            array2.remove(i);
            array3.remove(i);
        }
    }
}
JasonMArcher
  • 14,195
  • 22
  • 56
  • 52
DarthKiro
  • 31
  • 3

4 Answers4

4

A few things.

  1. Arrays don't have a remove() method. If you want to perform that operation on an Array data structure, you want to use an ArrayList.
  2. Parallel arrays can be confusing to work with. Instead, put all the information into its own object:

    class Game {
        String name;
        double price, rating;
    }
    

Then you can write:

    ArrayList<Game> games = new ArrayList<Game>();
La-comadreja
  • 5,627
  • 11
  • 36
  • 64
3

There is no remove method for Array. You can use the Arraylist.remove() method.

Jeremy B.
  • 9,168
  • 3
  • 45
  • 57
Anirban Nag 'tintinmj'
  • 5,572
  • 6
  • 39
  • 59
3

The reason you are getting this error is because array objects in Java don't have a .remove() method. If you really want a dynamic collection that you can remove objects from, you should use an ArrayList.

Just replace the arrays in your method signature with ArrayLists, then in your body replace array1[i] with array1.get(i) like so:

public static void removeGames(Scanner keyboard, ArrayList<String> array1,            
        ArrayList<Double> array2, ArrayList<Double> array3, int currentLength) {
    String removeInput;

    System.out.println("Enter the name of the game you would like to remove"
            + " from the list: ");
    removeInput = keyboard.next();

    for(int i = 0; i < array1.length; i++) {
        if(removeInput.equalsIgnoreCase(array1.get(i)) {
            array1.remove(i);
            array2.remove(i);
            array3.remove(i);
        }
    }
}

Just make sure to import java.util.ArrayList.

La-comadreja
  • 5,627
  • 11
  • 36
  • 64
James Dunn
  • 8,064
  • 13
  • 53
  • 87
  • Also, La-comadreja gives good advice about using a Game object, and just having one ArrayList of games. I recommend you do that as well. I didn't incorporate that in my answer because I wanted to focus on just saying that ArrayLists are better suited for this job than arrays. – James Dunn Feb 07 '14 at 20:17
0

If you really need to use array you should write your own method that removes the needed element. As java has quite an impressive collection of containers in the java.util package I would suggest using one from there. As you need to access elements at a given index I would suggest to use ArrayList. If you know the index and just want to remove the element from there use LinkedList.

I would advise also coding against the List interface, hence your code will look like this:

public static void removeGames(Scanner keyboard, List<String> array1,            
    List<Double> array2, List<Double> array3) {
    String removeInput;

    System.out.println("Enter the name of the game you would like to remove"
        + " from the list: ");
    removeInput = keyboard.next();

    int index = array1.indexOf(removeInput);
    array1.remove(index);
    array2.remove(index);
    array3.remove(index);
}
Olimpiu POP
  • 5,001
  • 4
  • 34
  • 49