相关文章推荐
暗恋学妹的鸵鸟  ·  MySQL 8.0 ...·  1 月前    · 
暗恋学妹的鸵鸟  ·  javascript - ...·  5 月前    · 
暗恋学妹的鸵鸟  ·  Debezium MongoDB ...·  10 月前    · 
暗恋学妹的鸵鸟  ·  java.lang.IndexOutOfBo ...·  10 月前    · 
暗恋学妹的鸵鸟  ·  git tag ...·  11 月前    · 
暗恋学妹的投影仪  ·  《spring ...·  1小时前    · 
从容的排球  ·  异常 ...·  1小时前    · 
无邪的铁板烧  ·  mybatis 操作 oracle 报错 ...·  1小时前    · 
善良的骆驼  ·  ONNX未使用GPU·  1小时前    · 
好帅的海龟  ·  轻松学pytorch之使用onnx ...·  1小时前    · 
活泼的生姜  ·  21 Markdown格式 | R语言教程·  1小时前    · 

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!

E&ICT MNIT - Data Science and Machine Learning | IIT Madras - Advance Certification in Data Science and AI | E&ICT IIT Guwahati - Cloud Computing & DevOps | E&ICT IIT Guwahati - Cyber Security | E&ICT IIT Guwahati - Software Engineering & Application Development | E&ICT IIT Guwahati - Big Data Analytics | E&ICT IIT Guwahati - Full Stack Web Development | E&ICT MNIT - AI and Machine Learning | E&ICT IIT Guwahati - UI UX Design Strategy | IIT Madras - Data Analytics for Business | E&ICT IIT Roorkee - Cloud Computing & DevOps | E&ICT MNIT - Cyber Security & Ethical Hacking | E&ICT MNIT - Business Analyst & Project Management

Browse By Domains

Big Data Analytics Courses | Data Science Courses | Business Intelligence courses | Best Salesforce Courses | Cloud Computing Courses | Best Digital Marketing Courses | Programming Courses Online | Database Certification Courses | Project Management Certification Courses | Best Web Development Certification courses | Automation Trainings | UI UX Course

Popular Courses

Data Science Courses | Best Artificial Certification Intelligence Course | Machine Learning Training | Python Course | DevOps Certification Course | AWS Training and Certification | Azure Certification | AWS DevOps Training | Azure DevOps Certification Course | Cyber Security Course | Ethical Hacking Course | Big Data Hadoop Certification | SQL Course | Salesforce Admin Certification | Online Salesforce Developer Certification | Selenium Training Online | Business Analyst Course Online | Data Analyst Certification course | Best Microsoft Power BI Certification Course | Tableau Course Online | Linux certification Course | Data Analytics Certification Course | Digital Marketing Course | Online Business Analytics Course | Online Investment Banking Course | Online Supply Chain Course | Online Electric Vehicle Course

Degree Courses

M. Tech in AI & ML | M. Sc. in Data Science | MBA in Big Data Management | M. Sc. in Artificial Intelligence | MBA in Business Administration | MBA in International Marketing | Master’s in Computer Science | MBA in Finance and Accounting | Master’s in Engineering Management | Master of Science in Data Science | MSC in Data Science
 
推荐文章
善良的骆驼  ·  ONNX未使用GPU
1小时前