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 readCountry(String filename) throws FileNotFoundException { // Create an arraylist of the country class ArrayList countries = new ArrayList(); // 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 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 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 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 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!!"); } } }

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question

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!!");

}

}

}

Country Population
Afghanistan
Albania 2994667 11100
Area (in sg mi)
29835392 250000
Algeria 34994937 919590
Andorra 84825
181
Angola
Antigua_and_Barbuda
Argentina
Armenia 2967975 11506
13338541 481351
87884
171
41769726 1068296
Australia
21766711 2967893
Austria 8217280 32382
Azerbaijan
Bahamas 313312
Bahrain 1214705 257
8372373 33436
5382
Bangladesh
Barbados 286705
158570535
55598
166
Belarus 9577552 80154
Belgium 10431477 11787
321115
Belize
8867
Benin
Bhutan
9325032 43483
18147
Bolivia 10118683 424162
708427
Bosnia_and_Herzegovina
Botswana 2065398 231803
4622163 19741
Brazil
Brunei
203429773
3286470
401890
2228
Bulgaria 7093635 42822
Burkina_Faso
Burundi 10216190 10745
16751455 105869
Cambodia 14701717 69900
Cameroon 19711291 183567
Canada
34030589 3855081
Cape_Verde
Central_African_Republic 4950027 240534
Chad
516100
1557
10758945 495752
Chile
China
16888760 292258
1336718015
3705386
Colombia 44725543 439733
Comoros 794683
838
Congo_Democratic_Republic_of_the 71712867 905563
Congo_Republic_of_the
Costa_Rica
Cote_d'Ivoire
Croatia 4483804 21831
4243929 132046
4576562 19730
21504162 124502
Cuba
11087330 42803
Суprus
Czech_Republic
Denmark 5529888 16639
1120489 3571
10190213 30450
Djibouti 757074
Dominica 72969
8880
291
Dominican_Republic
Ecuador 15007343 109483
9956648 18815
Egypt
EL_Salvador
Equatorial_Guinea
Eritrea 5939484 46842
82079636 386660
6071774 8124
668225
10830
Estonia 1282963 17462
Ethiopia 90873739 435184
883125
Finland 5259250 130558
65312249 211208
Fiji
7054
France
Gabon
Gambia
1576665 103346
1797860 4363
Georgia 4585874 26911
Germany 81471834 137846
Ghana
24791073 92456
Greece
10760136 50942
Grenada 108419
Guatemala
133
13824463 42042
Transcribed Image Text:Country Population Afghanistan Albania 2994667 11100 Area (in sg mi) 29835392 250000 Algeria 34994937 919590 Andorra 84825 181 Angola Antigua_and_Barbuda Argentina Armenia 2967975 11506 13338541 481351 87884 171 41769726 1068296 Australia 21766711 2967893 Austria 8217280 32382 Azerbaijan Bahamas 313312 Bahrain 1214705 257 8372373 33436 5382 Bangladesh Barbados 286705 158570535 55598 166 Belarus 9577552 80154 Belgium 10431477 11787 321115 Belize 8867 Benin Bhutan 9325032 43483 18147 Bolivia 10118683 424162 708427 Bosnia_and_Herzegovina Botswana 2065398 231803 4622163 19741 Brazil Brunei 203429773 3286470 401890 2228 Bulgaria 7093635 42822 Burkina_Faso Burundi 10216190 10745 16751455 105869 Cambodia 14701717 69900 Cameroon 19711291 183567 Canada 34030589 3855081 Cape_Verde Central_African_Republic 4950027 240534 Chad 516100 1557 10758945 495752 Chile China 16888760 292258 1336718015 3705386 Colombia 44725543 439733 Comoros 794683 838 Congo_Democratic_Republic_of_the 71712867 905563 Congo_Republic_of_the Costa_Rica Cote_d'Ivoire Croatia 4483804 21831 4243929 132046 4576562 19730 21504162 124502 Cuba 11087330 42803 Суprus Czech_Republic Denmark 5529888 16639 1120489 3571 10190213 30450 Djibouti 757074 Dominica 72969 8880 291 Dominican_Republic Ecuador 15007343 109483 9956648 18815 Egypt EL_Salvador Equatorial_Guinea Eritrea 5939484 46842 82079636 386660 6071774 8124 668225 10830 Estonia 1282963 17462 Ethiopia 90873739 435184 883125 Finland 5259250 130558 65312249 211208 Fiji 7054 France Gabon Gambia 1576665 103346 1797860 4363 Georgia 4585874 26911 Germany 81471834 137846 Ghana 24791073 92456 Greece 10760136 50942 Grenada 108419 Guatemala 133 13824463 42042
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 4 steps with 7 images

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY