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

Write a program that reads a list of concepts/strings from the LIST.txt text fil

ID: 3821322 • Letter: W

Question

Write a program that reads a list of concepts/strings from the LIST.txt text file (one concept per line), stores them into an array of strings, and then prints the following concepts (one per line):

1. All the concepts from the array

2. All the concepts from the array that contain spaces

3. All the concepts from the array that start with a consonant

4. All the concepts from the array that contain the letter A (small or large caps)

5. All the concepts from the array in alphabetical order

Alabama Alaska Arizona Arkansas California Colorado Connecticut Delaware District Of Columbia Florida Georgia Hawaii Idaho Illinois Indiana Iowa Kansas Kentucky Louisiana Maine Maryland Massachusetts Michigan Minnesota Mississippi Missouri Montana Nebraska Nevada New Hampshire New Jersey New Mexico New York North Carolina North Dakota Ohio Oklahoma Oregon Pennsylvania Rhode Island South Carolina South Dakota Tennessee Texas Utah Vermont Virginia Washington West Virginia Wisconsin Wyoming American Samoa District of Columbia Guam Northern Mariana Islands Puerto Rico United States Virgin Islands

Explanation / Answer

//Program

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.Arrays;
/*
Write a program that reads a list of concepts/strings from the LIST.txt text file (one concept per line),
stores them into an array of strings, and then prints the following concepts (one per line):
1. All the concepts from the array
2. All the concepts from the array that contain spaces
3. All the concepts from the array that start with a consonant
4. All the concepts from the array that contain the letter A (small or large caps)
5. All the concepts from the array in alphabetical order
*/
public class ListAssignment
{
   private final String inputFilePath = "C:\Users\Dell\Desktop\CheggScreenshots\LIST.txt";//The input file path
  
   public static void main(String args[])
   {
       ListAssignment listAssignment = new ListAssignment();
       listAssignment.process();
         
   }
   public void process()
   {
      
       BufferedReader br = null;
       FileReader fr = null;
       int index=0;
       //Read The file
       try
       {
           fr = new FileReader(inputFilePath);
           br = new BufferedReader(fr);
           String strCurrentLine="";
           //this while loop is to calculate the number of lines available in the LIST file
           while ((strCurrentLine = br.readLine()) != null)
           {
               index++;//incrementing the index
           }
           //Read the file again to insert in the array
           fr = new FileReader(inputFilePath);
           br = new BufferedReader(fr);
           String [] arrayConcepts=new String[index];//the index contains the number of lines available in the LIST file
           index=0;
           while ((strCurrentLine = br.readLine()) != null)//reading line by line
           {
               arrayConcepts[index]=strCurrentLine;
               index++;//incrementing the index
           }
           //Print 1.All the concepts from the array
           System.out.println("1.All the concepts from the array");
           for(int j=0;j<arrayConcepts.length;j++)
           {
               if(arrayConcepts[j]!=null)
               System.out.println(arrayConcepts[j]);
           }
           //Print 2.All the concepts from the array that contain spaces
           System.out.println("2.All the concepts from the array that contain spaces");
           for(int j=0;j<arrayConcepts.length;j++)
           {
               if(arrayConcepts[j]!=null)
               if(arrayConcepts[j].contains(" "))
               {
                   System.out.println(arrayConcepts[j]);
               }
           }
           //Print 3. All the concepts from the array that start with a consonant
           //As there are only 5 vowels it is better to check whether the first letter is a vowel
           //if not then we will consider that as a consonent
           System.out.println("3. All the concepts from the array that start with a consonant");
           for(int j=0;j<arrayConcepts.length;j++)
           {
               if(arrayConcepts[j]!=null)
               if(!arrayConcepts[j].toLowerCase().startsWith("a")&&!arrayConcepts[j].toLowerCase().startsWith("e")&&!arrayConcepts[j].toLowerCase().startsWith("i")&&!arrayConcepts[j].toLowerCase().startsWith("o")&&!arrayConcepts[j].toLowerCase().startsWith("u"))
               {
                   System.out.println(arrayConcepts[j]);
               }
           }
           //print 4. All the concepts from the array that contain the letter A (small or large caps)
           //we will convert the string into lower case
           System.out.println("4. All the concepts from the array that contain the letter A (small or large caps)");
           for(int j=0;j<arrayConcepts.length;j++)
           {
               if(arrayConcepts[j]!=null)
               if(arrayConcepts[j].toLowerCase().contains("a"))
               {
                   System.out.println(arrayConcepts[j]);
               }
           }
           //Print 5. All the concepts from the array in alphabetical order
           System.out.println("5. All the concepts from the array in alphabetical order");
           Arrays.sort(arrayConcepts);//sorting the array
           for(int j=0;j<arrayConcepts.length;j++)
           {
               if(arrayConcepts[j]!=null)
               System.out.println(arrayConcepts[j]);
           }
          
       }
       catch (IOException e)
       {
               e.printStackTrace();
       }
       finally
       {
           try {

               if (br != null)
                   br.close();

               if (fr != null)
                   fr.close();

           } catch (IOException ex) {

               ex.printStackTrace();

           }
       }      
   }

}

Output:

1.All the concepts from the array
Alabama
Alaska
Arizona
Arkansas
California
Colorado
Connecticut
Delaware
District Of Columbia
Florida
Georgia
Hawaii
Idaho
Illinois
Indiana
Iowa
Kansas
Kentucky
Louisiana
Maine
Maryland
Massachusetts
Michigan
Minnesota
Mississippi
Missouri
Montana
Nebraska
Nevada
New Hampshire
New Jersey
New Mexico
New York
North Carolina
North Dakota
Ohio
Oklahoma
Oregon
Pennsylvania
Rhode Island
South Carolina
South Dakota
Tennessee
Texas
Utah
Vermont
Virginia
Washington
West Virginia
Wisconsin
Wyoming
American Samoa
District of Columbia
Guam
Northern Mariana Islands
Puerto Rico
United States
Virgin Islands
2.All the concepts from the array that contain spaces
District Of Columbia
New Hampshire
New Jersey
New Mexico
New York
North Carolina
North Dakota
Rhode Island
South Carolina
South Dakota
West Virginia
American Samoa
District of Columbia
Northern Mariana Islands
Puerto Rico
United States
Virgin Islands
3. All the concepts from the array that start with a consonant
California
Colorado
Connecticut
Delaware
District Of Columbia
Florida
Georgia
Hawaii
Kansas
Kentucky
Louisiana
Maine
Maryland
Massachusetts
Michigan
Minnesota
Mississippi
Missouri
Montana
Nebraska
Nevada
New Hampshire
New Jersey
New Mexico
New York
North Carolina
North Dakota
Pennsylvania
Rhode Island
South Carolina
South Dakota
Tennessee
Texas
Vermont
Virginia
Washington
West Virginia
Wisconsin
Wyoming
District of Columbia
Guam
Northern Mariana Islands
Puerto Rico
Virgin Islands
4. All the concepts from the array that contain the letter A (small or large caps)
Alabama
Alaska
Arizona
Arkansas
California
Colorado
Delaware
District Of Columbia
Florida
Georgia
Hawaii
Idaho
Indiana
Iowa
Kansas
Louisiana
Maine
Maryland
Massachusetts
Michigan
Minnesota
Montana
Nebraska
Nevada
New Hampshire
North Carolina
North Dakota
Oklahoma
Pennsylvania
Rhode Island
South Carolina
South Dakota
Texas
Utah
Virginia
Washington
West Virginia
American Samoa
District of Columbia
Guam
Northern Mariana Islands
United States
Virgin Islands
5. All the concepts from the array in alphabetical order
Alabama
Alaska
American Samoa
Arizona
Arkansas
California
Colorado
Connecticut
Delaware
District Of Columbia
District of Columbia
Florida
Georgia
Guam
Hawaii
Idaho
Illinois
Indiana
Iowa
Kansas
Kentucky
Louisiana
Maine
Maryland
Massachusetts
Michigan
Minnesota
Mississippi
Missouri
Montana
Nebraska
Nevada
New Hampshire
New Jersey
New Mexico
New York
North Carolina
North Dakota
Northern Mariana Islands
Ohio
Oklahoma
Oregon
Pennsylvania
Puerto Rico
Rhode Island
South Carolina
South Dakota
Tennessee
Texas
United States
Utah
Vermont
Virgin Islands
Virginia
Washington
West Virginia
Wisconsin
Wyoming