0

I am trying to find the index of a value in a nested ArrayList, but I need to find the position of the first ArrayList. I am receiving an error when I run this code:

public int findCity(String city) {
  city = "\"" + city + "\"";
  System.out.println(cityArray.size());
  for (List<String> value : cityArray) {
    String newCity = value.get(1); 
    if (newCity == city) return cityArray.indexOf(value);
  }
  return -1;
}

The problem happens in the line after the for loop:

String newCity = value.get(1);

It's telling me that index 1 is out of bounds for length 1. Any help is greatly appreciated!

I found the problem: I was using nextLine() to read the csv file, and it was creating a new array every time there was a space. This was a huge oversight on my part, but now I can start fixing the problem.

Once again, sorry for the inconvinience.

Mason
  • 11
  • 2
  • 3
    `if (newCity == city)` -> [How do I compare strings in Java?](https://stackoverflow.com/q/513832) – Pshemo Nov 08 '21 at 16:10
  • 3
    What is `cityArray`? – Nikolas Charalambidis Nov 08 '21 at 16:10
  • 3
    If the length of your `ArrayList` is 1 then it has only one index, which is 0. – LinFelix Nov 08 '21 at 16:31
  • _"It's telling me that index 1 is out of bounds for length 1"_ - this means the List you are trying to access only has one element and you are trying to access a second element - . It doesn't exist so.. boom! It would appear that the data is not in the form you expect it to be. Consider debugging or dropping in some output commands to inspect the state. – vsfDawg Nov 08 '21 at 16:45

3 Answers3

0

You're mixing up cityArray.size() and cityArray.get(1).size(), which can be completely different.

(You are also comparing strings with ==, which is likely to cause disaster. Use .equals.)

Louis Wasserman
  • 191,574
  • 25
  • 345
  • 413
0

i think the problem is that you start counting from 1, but an ArrayList starts counting from 0. This is why the index is out of bounds.

You could try this:

String newCity = value.get(0);
Ian
  • 21
  • 4
  • 1
    The reason I am getting position 1 is that 1 is the city_ascii, which has no special characters in it. Position 0 might have special characters, and it is simpler to write without special characters. The way my array is set up is that the outer array position is the city, and the inner array position is the details (0 is city name, 1 is city_ascii, 2 is city_alt). I got all of the information from https://simplemaps.com/data/world-cities. So I don't think that is the problem, but thank you! – Mason Nov 08 '21 at 16:25
  • Ok, but what is printed out in the console in `System.out.println(cityArray.size());` ? And why do you return -1? – Ian Nov 08 '21 at 16:32
0

Below code can help you ,though I did not understand your problem.

public static void main(String[] args) {
        List<String> list1 = Arrays.asList(new String[]{"A","B"});
        List<String> list2 = Arrays.asList(new String[]{"Y","Z"});
        List<List<String>> cityArray = new ArrayList<>(2);
        cityArray.add(list1);
        cityArray.add(list2);
        System.out.println(findCity("Z", cityArray));
    }

    public static int findCity(String city, List<List<String>> cityArray) {
        System.out.println(cityArray.size());
        for (List<String> value : cityArray) {
            for(String newCity :value) { 
                if (newCity.equalsIgnoreCase(city)) {
                    return cityArray.indexOf(value);
                }
            }
        }
        return -1;
    }
Bishnu
  • 11
  • 4