Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

List of countries in the countries.exe file java Write a code to perform the fol

ID: 3830462 • Letter: L

Question

List of countries in the countries.exe file

java

Write a code to perform the following operations (file for this problem is provided for you- download it to the project root when working on eclipse - countries.txt): 1. create an Arraylist of String objects called countryList 2. Read the list of countries form countries.txt and add them to the list one at a time 3. Sort the array list alphabetically in an increasing order 4. print the list of countries 1 on each line after the sort. 5. make sure to perform exception handling when reading from the file. import java.io.*; import java.util.*; public class CountryList {public static void main(String[] args) {File file = new File("countries.txt"); ArrayList countryList = new ArrayList ();//read from the file and store the strings in an Arraylist countryList//close the scanner object//sort the countryList//display the countries stored in this sorted list 1- on each}//end of main}//end of class

Explanation / Answer

import java.io.File;

import java.io.FileNotFoundException;

import java.util.ArrayList;

import java.util.Collections;

import java.util.Scanner;

public class CountryList {

  

   public static void main(String[] args) {

      

       File file = new File("countries.txt");

      

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

      

       try {

          

           Scanner fileScanner = new Scanner(file);

          

           // reading from file one line at a time and stroing in list

           while(fileScanner.hasNextLine()){

               String country = fileScanner.nextLine();

              

               countryList.add(country);

           }

          

           fileScanner.close();

          

           // sorting

           Collections.sort(countryList);

          

           // displaying

           for(String country : countryList)

               System.out.println(country);

          

       } catch (FileNotFoundException e) {

           e.printStackTrace();

       }

   }

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote