Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about Collectives
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about Teams
Like the title says, my problem is that when I compile I get Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 1, Size: 1. Here is the part where the problem is coming from.
Edit.
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)
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've been searching online for a solution, so I'm pretty sure the problem is that the array is too small, but i'm still not sure how to fix it. I'm trying to get the upper part of code to take just the numbers out of the array and put them in a 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'm 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;
I've been stuck on this for awhile so any help would be greatly appreciated!
Edit.
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)
–
Looping until i < flightList.size()
But getPrice(list.get(i)
is using index from above but on different list
for (int i=0; i<flightList.size(); i++) {
list = directFlights(flightList, city1, city2);
Price.add(getPrice(list.get(i)));
Inside Method , directFlights()
List is created using below condition, meaning size of list will always be less than or equal to size of flightList
.
But above getPrice(list.get(i)
is using indexing from parent list (flightList).
if (city1a.equals(city1) && city2a.equals(city2)) {
list.add(flightList.get(i));
–
Your for loop is with respect to flightList, not list.
In every iteration you're 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)));
list = directFlights(flightList, city1, city2);
for (int i=0; i<list.size(); i++) {
Price.add(getPrice(list.get(i)));
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.