Add the StateDataAnalyzer Java code to your project Your pro
Add the StateDataAnalyzer Java code to your project.
Your program should allow the user to choose one of these options:
Find state with maximum land mass
Find state with minimum land mass
Find state with maximum population
Find state with minimum population
Find state with maximum average income
Find state with minimum average income
You must have a void, no argument method called displayIntro() to form the introduction display (Should only appear once).
Must also have the menu (1-6 options) appear for each reprompt.
Must have a method called findMaxIndex(double[] array) that takes a double and returns the maximum value’s index.
Must have a method called findminIndex(double[] array) that takes a double and returns the minimum value’s index.
import java.io.*;
Solution
Here is the code for you:
import java.io.*;
 import java.util.*;
public class StateDataAnalyzer {
private static int NUMBER_STATES = 50;
public static String[] loadNamesFromFile(String fileName) throws Exception {
 File file = new File(fileName);
 String[] array = new String[NUMBER_STATES];
Scanner fileScan = new Scanner(file);
 for(int row = 0; row < NUMBER_STATES; row++)
 {
 array[row] = fileScan.next();
 fileScan.nextLine();
 }
 return array;
 }
public static double[] loadPercapitaFromFile(String fileName) throws Exception {
 File file = new File(fileName);
 double[] array = new double[NUMBER_STATES];
Scanner fileScan = new Scanner(file);
 for(int row = 0; row < NUMBER_STATES; row++)
 { fileScan.next();
 array[row] = fileScan.nextDouble();
 fileScan.nextLine();
 }
 return array;
 }
public static int[] loadPopulationFromFile(String fileName) throws Exception {
 File file = new File(fileName);
 int[] array = new int[NUMBER_STATES];
Scanner fileScan = new Scanner(file);
 for(int row = 0; row < NUMBER_STATES; row++)
 { fileScan.next();
 fileScan.next();
 array[row] = fileScan.nextInt();
 fileScan.nextLine();
 }
 return array;
 }
 public static double[] loadLandSizeFromFile(String fileName) throws Exception {
 File file = new File(fileName);
 double[] array = new double[NUMBER_STATES];
Scanner fileScan = new Scanner(file);
 for(int row = 0; row < NUMBER_STATES; row++)
 { fileScan.next();
 fileScan.next();
 fileScan.next();
 array[row] = fileScan.nextDouble();
 }
 return array;
 }
   //You must have a void, no argument method called displayIntro() to form the introduction display
    public static void displayIntro()
    {
    System.out.println(\"State Data Analyzer...\");
    }
    //Must also have the menu (1-6 options) appear for each reprompt.
    public static int getMenuDetails()
    {
    System.out.println(\"1. Find state with maximum land mass\");
    System.out.println(\"2. Find state with minimum land mass\");
    System.out.println(\"3. Find state with maximum population\");
    System.out.println(\"4. Find state with minimum population\");
    System.out.println(\"5. Find state with maximum average income\");
    System.out.println(\"6. Find state with minimum average income\");
    System.out.println(\"7. Exit.\");
    System.out.print(\"Enter your choice: \");
    int choice = new Scanner(System.in).nextInt();
    return choice;
    }
    //Must have a method called findMaxIndex(double[] array) that takes a double and returns the maximum value’s index.
    public static int findMaxIndex(double[] array)
    {
    int maxIndex = 0;
    for(int i = 0; i < array.length; i++)
    if(array[i] > array[maxIndex])
    maxIndex = i;
    return maxIndex;
    }
    //Must have a method called findMinIndex(double[] array) that takes a double and returns the minimum value’s index.
    public static int findMinIndex(double[] array)
    {
    int minIndex = 0;
    for(int i = 0; i < array.length; i++)
    if(array[i] < array[minIndex])
    minIndex = i;
    return minIndex;
    }
 public static void main(String[] args) throws Exception {
 String fileName = \"State_Data.csv\";
 String[] stateNames = loadNamesFromFile(fileName);
 double[] percapitaData = loadPercapitaFromFile(fileName);
 int[] populationData = loadPopulationFromFile(fileName);
 double[] landSizeData = loadLandSizeFromFile(fileName);
//This code will print the data to console to ensure you are reading
 // the data in and that the arrays are loaded.
 // This should be deleted once you have suceeded in laoding the data.
 System.err.print(\"\ State data\");
 for(int row = 0; row < NUMBER_STATES; row++)
 {
 System.out.print(\"\ \"+stateNames[row]+\" \"+percapitaData[row]+\" \"+populationData[row]+\" \"+landSizeData[row]);
 }
//display introduction (by method)
 displayIntro();
 //create program loop with menu
 while(true)
 {
 int choice = getMenuDetails();
 switch(choice)
 {
 case 1:   System.out.println(\"The state with maximum land mass is: \" + stateNames[findMaxIndex(landSizeData)]); break;
 case 2:   System.out.println(\"The state with minimum land mass is: \" + stateNames[findMinIndex(landSizeData)]); break;
 case 3:   System.out.println(\"The state with maximum population is: \" + stateNames[findMaxIndex(populationData)]); break;
 case 4:   System.out.println(\"The state with minimum population is: \" + stateNames[findMinIndex(populationData)]); break;
 case 5:   System.out.println(\"The state with maximum average income is: \" + stateNames[findMaxIndex(percapitaData)]); break;
 case 6:   System.out.println(\"The state with minimum average income is: \" + stateNames[findMinIndex(percapitaData)]); break;
 case 7:   return;
 default: System.out.println(\"Invalid menu option.\");
 }
 }
}
}



