title: Not Printing desire output using arrays and reading from files
body: <p>The program function is to read from two separate text files, One text file is called gifts(random names) and cost(total cost for each day). I have to print if the user input is 3, it will print the 3 lines with the cost of those 3 days. Additionally, I have to print the total cost of the 12 days.  </p>
  
  <p>My code</p>
  
  <pre><code>import java.io.File;
  
  import java.io.FileNotFoundException;
  
  import java.util.Scanner;
  
  import java.text.NumberFormat;
  
  public class GiftsTwelveDays{
  
      public static void main(String[] args) {
  
          String[] gifts = new String[12];
  
          double[] costGifts = new double[12];
  
          String total = new String();
  
          //Following code reads the Gifts.txt file
  
          File file = new File("gifts.txt");
  
          try 
          {
              Scanner scannerFile = new Scanner(file);
  
              while (scannerFile.hasNextLine()) 
  
              {
                  int i = scannerFile.nextInt();
  
                  String present = scannerFile.nextLine();
  
                  gifts[i - 1] = present;
  
              }
  
              scannerFile.close();
  
          }
  
          catch (FileNotFoundException e) 
  
          {
              System.out.println("File not Found.");
          }
  
  
  
          //This reads the cost and stores in array of double
  
          File file1 = new File("cost.txt");
  
          try 
          {
              Scanner scannerFile = new Scanner(file1);
  
              while (scannerFile.hasNextInt()) {
  
                  scannerFile.next();
  
                  if(scannerFile.hasNextInt())
  
                  {
  
                      int i = scannerFile.nextInt();
  
                      double cost = scannerFile.nextDouble();
  
                      costGifts[i-1] = cost;
  
                  }
  
                  else
                  {
  
                      scannerFile.nextInt();
  
                      total = scannerFile.nextLine();
  
                  }
  
              }
  
              scannerFile.close();
  
          }
  
          catch (FileNotFoundException e) 
          {
  
              System.out.println("File could not be found");;
  
          }
  
          //following gets user input and runs
  
          Scanner userInput = new Scanner(System.in);
  
          NumberFormat money = NumberFormat.getInstance(); //for format with comma
  
          money.setGroupingUsed(true);
  
          double costDay = 0;
  
          System.out.println("Enter the day:");
  
          int choice = userInput.nextInt();
  
          userInput.nextLine();
  
          if (choice &lt; 1 || choice &gt; 12)//runs if choice is invalid
  
          {
  
              System.out.println("Invalid Choice");
  
          }
  
          else
  
          {
  
              System.out.println("Your gifts for the day " + choice + " are: \n");
  
              //calculates the day cost and prints gift simultaneously
  
              for(int i = 0; i &lt; choice; i++)
  
              {
  
                  System.out.println((i + 1) + gifts[i]);
  
                  costDay = costDay + costGifts[i];
  
              }
  
              //prints the calculated cost.
  
              System.out.println("\nCost of Day: $" + money.format(costDay));
  
              System.out.println("\nTotal Cost for Twelve Days: $" + total);
  
          }
  
      }
  
  }
  </code></pre>
  
  <p>Output:</p>
  
  <pre><code>Enter the day:
  3
  Your gifts for the day 3 are: 
  
  1 Patridge in a Pear Tree
  2 Turtle Doves
  3 French Hen
  
  Cost of Day: $0
  
  Total Cost for Twelve Days: $
  </code></pre>
  
  <p>gifts.txt</p>
  
  <pre><code>1 Patridge in a Pear Tree
  2 Turtle Doves
  3 French Hen
  4 Calling Birds
  5 Gold Rings
  6 Geese-a-Laying
  7 Swans-a-Swimming
  8 Maids-a-Milking
  9 Ladies Dancing
  10 Lords-a-Leaping
  11 Piper
  12 Drummers Drumming
  </code></pre>
  
  <p>cost.txt</p>
  
  <pre><code>220.13
  595.13
  776.63
  1376.59
  2126.59
  2516.59
  15641.59
  15699.59
  23252.43
  33252.43
  36056.83
  39094.93
  </code></pre>