
Getting this error when trying to run this program - java.lang.ArrayIndexOutOfBoundsException: Index 1 out of bounds for length 1
here is the program below.
import java.util.ArrayList;
import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;
public class Largest_Country
{
public static ArrayList<Country> readCountry(String filename) throws FileNotFoundException
{
// Create an arraylist of the country class
ArrayList<Country> countries = new ArrayList<Country>();
// Create a scanner object to open the input file
Scanner scan = new Scanner(new File(filename));
// Initialize a string variable to read the file
String line = "";
while(scan.hasNextLine())
{
// Read the file line by line
line = scan.nextLine();
// Split the line at spaces, and assign the values to
// a string array
String[] details = line.split(" ");
// Create object of the class Country, and pass the values
// to the object
Country country_info = new Country(details[0], Long.parseLong(details[1]), Long.parseLong(details[2]));
// Add the object to the arraylist
countries.add(country_info);
}
// Return the arraylist
return countries;
}
// Define the countryWithLargestArea() method
public static Country countryWithLargestArea(ArrayList<Country> countries)
{
// Create an object of the class, and assign the
// first value from the arraylist
Country largest_area_country = countries.get(0);
for(int x = 0; x < countries.size(); x++)
{
// Check if the value of area at index x is greater than
// the value at largest_area_country
if(countries.get(x).getCountryArea() > largest_area_country.getCountryArea())
{
// Assign the values to largest_area_country
largest_area_country = countries.get(x);
}
}
// Return the country details
return largest_area_country;
}
// Define the countryWithLargestPopulation() method
public static Country countryWithLargestPopulation(ArrayList<Country> countries)
{
// Create an object of the class, and assign the
// first value from the arraylist
Country largest_population_country = countries.get(0);
for(int x = 0; x < countries.size(); x++)
{
// Check if the value of area at index x is greater than
// the value at largest_population_country
if(countries.get(x).getCountryPopulation() > largest_population_country.getCountryPopulation())
{
// Assign the values to largest_population_country
largest_population_country = countries.get(x);
}
}
// Return the country details
return largest_population_country;
}
// Define the countryWithLargestPopulationDensity() method
public static Country countryWithLargestPopulationDensity(ArrayList<Country> countries)
{
// Create an object of the class, and assign the
// first value from the arraylist
Country largest_population_density_country = countries.get(0);
for(int x = 0; x < countries.size(); x++)
{
// Check if the value of area at index x is greater than
// the value at largest_population_density_country
if(countries.get(x).getPopulationDensity() > largest_population_density_country.getPopulationDensity())
{
// Assign the values to largest_population_density_country
largest_population_density_country = countries.get(x);
}
}
// Return the country details
return largest_population_density_country;
}
// Define the main() method
public static void main(String[] args)
{
try
{
// Create an arraylist, and call the readCountry() method
ArrayList<Country> countries = readCountry("countrydata.txt");
// Call the countryWithLargestArea(), and display the
// country having largest area
System.out.println("The country with the largest area:\n" + countryWithLargestArea(countries).printCountry());
// Call the countryWithLargestArea(), and display the
// country having largest population
System.out.println("\nThe country with the largest population:\n" + countryWithLargestPopulation(countries).printCountry());
// Call the countryWithLargestPopulationDensity(), and display the
// country having largest population density
System.out.println("\nThe country with the largest population density:\n" + countryWithLargestPopulationDensity(countries).printCountry());
}
catch(FileNotFoundException e)
{
// Display file not found exception
System.out.println("File not found!!");
}
}
}


Trending nowThis is a popular solution!
Step by stepSolved in 4 steps with 7 images

- I need help with this code !! import java.util.Arrays;import java.util.Scanner; public class MaxElement {public static void main(String[] args) { //create an object for Scanner class Scanner x = new Scanner (System.in);System.out.print ("Enter 10 integers: ");// create an arrayInteger[] arr = new Integer[10]; // Execute for loopfor (int i = 0; i < arr.length; i++) {//get the 10 integersarr[i] = x.nextInt(); } // Print the maximum numberSystem.out.print("The max number is = " + max(arr));System.out.print("\n"); } //max method public static <E extends Comparable<E>> E max(E[] arr) {E max = arr[0]; // Execute for loop for (int i = 1; i < arr.length; i++) { E element = arr[i];if (element.compareTo(max) > 0) {max = element;}}return max; }}arrow_forwardHow do I match the output? Code: import java.io.File; import java.util.ArrayList; import java.util.Scanner; public class Assignment08 { public static void main(String[] args) throws Exception { File inputData = new File("input05.txt"); Scanner input = new Scanner(inputData); int totalNumber = input.nextInt(); Friend.friends = new Friend[totalNumber]; input.nextLine(); for (int i = 0; i < totalNumber; i++) { String name = input.nextLine(); String phone = input.nextLine(); String age = input.nextLine(); int ageNum = Integer.parseInt(age); Friend.friends[i] = new Friend(name, phone, ageNum); } printContacts(Friend.friends); Friend.showTotalFriendAge(); ManageDuplicate(Friend.friends); printContacts(Friend.friends); Friend.showTotalFriendAge(); } public static void printContacts(Friend[] contacts) {…arrow_forwardNew JAVA code can only be added between lines 9 and 10, as seen in image.arrow_forward
- Write all the code within the main method in the TestTruck class below. Implement the following functionality. Construct two Truck objects: one with any make and model you choose and the second object with gas tank capacity 10. If an exception occurs, print the stack trace. Print both Truck objects that were constructed. import java.lang.IllegalArgumentException ; public class TestTruck { public static void main( String[] args ) { // write your code herearrow_forwardWrite a java program that uses an ArrayList object to store the following set of names in memory: [Steve, Tim, Lucy, Pat, Angela, Tom] Now write some more code so that the same ArrayList object is augmented with the name 'Steve' after the name 'Lucy' After the ArrayList object has been augmented with the new name, display the original and new lists on the console (as shown below), to verify that the new name is positioned correctly in the list.arrow_forwardIn Java, we can create an array of class objects. True O Falsearrow_forward
- Computer Networking: A Top-Down Approach (7th Edi...Computer EngineeringISBN:9780133594140Author:James Kurose, Keith RossPublisher:PEARSONComputer Organization and Design MIPS Edition, Fi...Computer EngineeringISBN:9780124077263Author:David A. Patterson, John L. HennessyPublisher:Elsevier ScienceNetwork+ Guide to Networks (MindTap Course List)Computer EngineeringISBN:9781337569330Author:Jill West, Tamara Dean, Jean AndrewsPublisher:Cengage Learning
- Concepts of Database ManagementComputer EngineeringISBN:9781337093422Author:Joy L. Starks, Philip J. Pratt, Mary Z. LastPublisher:Cengage LearningPrelude to ProgrammingComputer EngineeringISBN:9780133750423Author:VENIT, StewartPublisher:Pearson EducationSc Business Data Communications and Networking, T...Computer EngineeringISBN:9781119368830Author:FITZGERALDPublisher:WILEY





