0
votes

I need help with how to find and display the state information the user chose to the screen. I have five files which i have put into arrays. The five arrays contain information of a state(State Name, state capital,state nickname, State flower, state population.)

My program should allow the user of the program to enter a state name, and should display the information about that state (nickname, capital, flower, and population) to the screen.

Each input file contains the information in alphabetical order by state name. So line 1 of the state name file is Alabama, line 1 of the capital file is Montgomery (the capital of Alabama), line 1 of the flower file is the state flower for Alabama, line 1 of the nickname file is the nickname of Alabama, and line 1 of the population file is the population o``f Alabama. Line 2 of each file is info for Alaska, line 3 is for Arizona, etc.

This is my code so far.

public static void main(String[] args) throws FileNotFoundException {

   String capital, stateFlowers, stateNickName, statesNames;
   int statePopulation;

   Scanner keyboard = new Scanner (System.in);
   String userStateChoise;

   File capitals = new File("capitals.txt");
   File flores = new File("flowers.txt");
   File nickName = new File("nicknames.txt");
   File populacion = new File("population.txt");
   File state_Name = new File("stateNames.txt");

    System.out.println("Enter a State Name");

    userStateChoise = keyboard.nextLine();




    if (!capitals.exists()) {
        System.out.println("The capitals input file was not found");
        System.exit(0);
    }
     if (!flores.exists()) {
        System.out.println("The flores input file was not found");
        System.exit(0);
    }
     if (!nickName.exists()) {
        System.out.println("The nickName input file was not found");
        System.exit(0);
    }
     if (!populacion.exists()) {
        System.out.println("The populacion input file was not found");
        System.exit(0);
    }
      if (!state_Name.exists()) {
        System.out.println("The stateName input file was not found");
        System.exit(0);
    }



    Scanner inputFile = new Scanner(capitals);
    Scanner flower = new Scanner(flores);
    Scanner nickNames = new Scanner(nickName);
    Scanner populations = new Scanner(populacion);
    Scanner names = new Scanner(state_Name);       
    // ArrayList myCapitals = new ArrayList();
   //  ArrayList<String> stateNames = new ArrayList();

     while (inputFile.hasNext() && flower.hasNext() && nickNames.hasNext()
         &&   populations.hasNext() && names.hasNext())
     {
// reading all states from the input file           

//reading all cities from the input file            
        capital = inputFile.nextLine();
        stateFlowers = flower.nextLine();
        stateNickName = nickNames.nextLine();
        statePopulation = populations.nextInt();
        statesNames = names.nextLine();

 String[] stateCapitalArray = {capital};
 String[] stateflowersArray = {stateFlowers};
 String[] stateNickNameArray = {stateNickName};
 int [] statePopulationArray = {statePopulation};
 String[] stateNamesArray = {statesNames};

      // I used for loops to go through each item in the array and to display it to the        screen

  for (int index = 0; index < stateNamesArray.length; index++)
  {
      System.out.println("State name : " +stateNamesArray[index]);        
     }

   for (int index = 0; index < stateflowersArray.length; index++)
  {
      System.out.println("State flower : " + stateflowersArray[index]);        
     }
    for (int index = 0; index < stateCapitalArray.length; index++)
  {
      System.out.println("The capital : " + stateCapitalArray[index]);        
     }
  for (int index = 0; index < stateNickNameArray.length; index++)
  {
      System.out.println("State nickName : " + stateNickNameArray[index]);        
     }


   for (int index = 0; index < statePopulationArray.length; index++)
  {
      System.out.println("State pupoluation  : " + statePopulationArray[index]); 
      System.out.println("\n\n\n");
     } 

     }

     inputFile.close();
     flower.close();
     nickNames.close();
    // populations.close();
}

}

1
What is the problem with your current code. What are you trying to solve?johnsoe

1 Answers

0
votes

Besides the awkward method of collecting and storing the information and getting user input, the most glaring error I see is how you're building your array. Every time you read a new line you're reinitializing each array with one thing instead of adding each thing to each array. You'll end up with arrays of the last item from every file. The arrays should be created and initialized outside of the while loop and each item added to them as you read them.