Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Java by (7k points)

When I compile my code I am getting Exception in thread “main” like this:

Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 1, Size: 1   

at java.util.ArrayList.rangeCheck(ArrayList.java:653)   

at java.util.ArrayList.get(ArrayList.java:429)  

at pa6.FlightFinder.bestDirectPrice(FlightFinder.java:117)  

at pa6.FlightFinder.main(FlightFinder.java:14)

This is my code:
public static ArrayList<String> bestDirectPrice(ArrayList<String> flightList,String city1, String city2) {

ArrayList<String> list = new ArrayList<String>();

ArrayList<String> Price = new ArrayList<String>();

for (int i=0; i<flightList.size(); i++) {

    list = directFlights(flightList, city1, city2);

    Price.add(getPrice(list.get(i)));

}

return Price;

I have been searching online for a solution, so I am pretty sure the problem is that the array id too small, but I am still not sure how to fix it. I am trying to get the upper part of the code to take just the numbers out of the array and put them in the new one.

ArrayList<String> test = new ArrayList<String>(); //Array with list of flights

test.add("Orlando#DesMoines#194.88");

test.add("Portland#Orlando#287.74");

test.add("Buffalo#Boston#299.52"); 

test.add("Buffalo#Portland#264.80"); 

test.add("Chicago#Buffalo#223.56");

System.out.println(bestDirectPrice(test,"Buffalo","Orlando"));

And here are the other methods I am calling

public static String getPrice(String price) { //Takes flight description, which is a string, as a parameter and returns price of flight

String[] sArray = price.split("#", -1);

String newPrice = "";

for (int i = 0; i<1; i++)       

    newPrice = sArray[2];

return newPrice;

}

public static ArrayList<String> directFlights(ArrayList<String> flightList, String city1, String city2) { // Method to create array list containing every direct flight from city1 to city 2

ArrayList<String> list = new ArrayList<String>();

for (int i=0; i<flightList.size(); i++){

    String city2a = getDestinationCity(flightList.get(i));

    String city1a = getOriginationCity(flightList.get(i));

    if (city1a.equals(city1) && city2a.equals(city2)) {

        list.add(flightList.get(i));

    }

}

return list;

}

Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 1, Size: 1   

at java.util.ArrayList.rangeCheck(ArrayList.java:653)   

at java.util.ArrayList.get(ArrayList.java:429)  

at pa6.FlightFinder.bestDirectPrice(FlightFinder.java:117)  

at pa6.FlightFinder.main(FlightFinder.java:14)

I have been stuck on this for a while so any help would be greatly appreciated!

1 Answer

0 votes
by (13.1k points)

The problem in your code is your for loop is with respect to flightList, not list. In every iteration you are reassigning a new value to list rather than adding a new element

Try changing

for (int i=0; i<flightList.size(); i++) {

    list = directFlights(flightList, city1, city2);

    Price.add(getPrice(list.get(i)));

}

To  

list = directFlights(flightList, city1, city2);

for (int i=0; i<list.size(); i++) {

    Price.add(getPrice(list.get(i)));

}

Want to learn Java? Check out the core Java certification from Intellipaat.

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
asked Mar 13, 2021 in Java by Jake (7k points)
0 votes
1 answer

Browse Categories

...