-3

I am creating a short piece of code where I need to get an item from a list and compare it to a value.

Despite importing java.util.List and writing the correct method call, it still claims it 'cannot find symbol - method getItem(int)'

import java.util.*;
import java.util.List;

public static int duplicateCharacters(String input) {
    boolean number = false;

    int highest = 0;
    int secondHighest = 0;

    if(input.length()<=1){
      return -1;  
    }

    int[] newList = new int[] {0,1,2,3,4,5,6,7,8,9};

    for(int i=0; i<input.length(); i++){

        number = false;

        int currentValue = input.charAt(i);
        for(int q=0; q<newList.length; q++){
            if(newList.getItem(q) == currentValue){
                number = true;
            }
        }

        if(number == true){
            if(currentValue>highest){
                secondHighest = highest;
                highest = currentValue;
            }
        }

    }

    return secondHighest;

}

In the Java doc, it states:

getItem
public String getItem(int index)

Gets the item associated with the specified index.

Parameters:
index - the position of the item

Returns:
an item that is associated with the specified index

See Also:
getItemCount()

Thomas Fritsch
  • 9,639
  • 33
  • 37
  • 49
Link
  • 317
  • 1
  • 5
  • 17
  • 4
    [`java.util.List`](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/List.html) does not provide a method `getItem(...)`. Besides that, `newList` ist an `int[]` (`int`-array), not a `List`. – Turing85 Mar 03 '19 at 12:29
  • Not according to the Java doc... – Link Mar 03 '19 at 12:31
  • 1
    Please link the "javadoc" you are quoting. The official link i provided shows clearly that `java.util.List` does not provide a method `getItem(...)`. – Turing85 Mar 03 '19 at 12:32
  • As an aside: `if(number == true) { if(currentValue>highest) { ... } }` can be simplfied to `if (number == true && currentValue > highest) { ... }`. – Turing85 Mar 03 '19 at 12:33
  • 1
    Or `if(number) && ...` – achAmháin Mar 03 '19 at 12:36
  • 2
    The source code you are showing is lacking a class definition. It is impossible in Java to have a method declaration right after import statements. Post your real code, and the exact error message (see [mcve] and enhance)! – GhostCat Mar 03 '19 at 12:40
  • 2
    And yes: the answer is that arrays aren't list objects! Calling your variable "newList" doesn't magically change its type. – GhostCat Mar 03 '19 at 12:42
  • Compiler doesn't try to lie to you. It just informs you that declared type of variable you used doesn't provide member you are trying to use. You quoted *some* javadoc but we still don't know where did you get it from, nor why do you think it applies to `int[]` type (since such type doesn't have javadoc, it is just an array and arrays don't provide their own methods. Only methods they have access to are ones inherited from Object class which doesn't have `getItem` method). – Pshemo Mar 03 '19 at 13:36

3 Answers3

1

You can try accessing the element using the array index since newList is an array. Refer code snippet below -

public static int duplicateCharacters(String input) 
{
    boolean number = false;

    int highest = 0;
    int secondHighest = 0;

    if(input.length()<=1){
      return -1;  
    }

    int[] newList = new int[] {0,1,2,3,4,5,6,7,8,9};

    for(int i=0; i<input.length(); i++){

        number = false;

        int currentValue = input.charAt(i);
        for(int q=0; q<newList.length; q++){
            if(newList[q] == currentValue){
                number = true;
            }
        }

        if(number == true){
        if(currentValue>highest){
            secondHighest = highest;
            highest = currentValue;
        }
        }

    }

    return secondHighest;

}
Ravi
  • 192
  • 1
  • 7
1

First of all, a List isn't instantiated by the following line:

int[] newList = new int[] {0,1,2,3,4,5,6,7,8,9};

You could make it work by replacing it by the code line below:

List<Integer> newList = Arrays.asList(0, 1, 2, 3, 4, 5, 6, 7, 8, 9);

Note that I replaced int by Integer: this is because you can't use a primitive as your list type. The replacement shouldn't make a noticable difference in your code.

Next, replace this for-loop:

for(int q=0; q<newList.length; q++){
    if(newList[q] == currentValue){
        number = true;
    }
}

You can't use list[index] and list.length in a list. Instead, you use list.get(index) and list.size().

You get something like this:

for(int q = 0; q < newList.size(); q++){
    if(newList.get(q)== currentValue){
        number = true;
    }
}
Sacha
  • 819
  • 9
  • 27
0

You are trying to use the get method. List doesn't have getItem()

Raz Zelinger
  • 686
  • 9
  • 24