-1

So I'm doing a project for class and this is my code. This is my first semester coding and I can't for the life of me figure out why java can't find my file. Any input or advice would be great. I don't mean to just put this all of this code out here and be like whats wrong with my code but I've been on this for a few days with no success and it's due tomorrow.

import java.util.Scanner;
import java.io.*;

public class Project3_Josh_Rivera
{
   public static void main(String[] args) throws IOException
   {

      File file = new File("boarding.txt");      
      Scanner inFile = new Scanner(file);

      String firstName, lastName, dogBreed;
      int dogWeight, days, numOfRecords;
      double subTotal, total, totalAllBills = 0, totalTax, averageBill = 0;

         numOfRecords = 1;

         //read the file
         while(inFile.hasNext())
         {
            firstName = inFile.nextLine();
            lastName = inFile.nextLine();
            dogBreed = inFile.nextLine();
            dogWeight = inFile.nextInt();
            days = inFile.nextInt();
            inFile.nextLine();
            if(inFile.hasNext())
            {
               inFile.nextLine();
            }  

         numOfRecords++;

         //call title method
         displayTitle();

         //call calculateSubtotal method
         subTotal = calculateSubtotal(dogWeight, days, dogBreed);

         //call calculateTax method
         totalTax = calculateTax(subTotal);

         //call the calculateTotal method
         total = calculateTotal(subTotal, totalTax);

         //call the calculateAllBills method and add the totals to totalAllBills
         totalAllBills = calculateAllBills(total);

         //call the calculateAverageBills mehtod
         averageBill = calculateAverageBill(totalAllBills, numOfRecords);

         //call the displayInformation method
         displayInformation(firstName,lastName,dogBreed,dogWeight,days,subTotal,totalTax,total);

         }//end while
         inFile.close();

         //output to display total of the bills and average bill cost
         System.out.printf("The total of the bills is: $%.2f", totalAllBills);
         System.out.printf("\nThe average bill cost is : $%.2f", averageBill);

   }//end main


      //method to display the title
      public static void displayTitle()
      {
         System.out.println("Madison Kennel Grooming\n\n");
      }



      //method to calculate the subtotal
      public static double calculateSubtotal(double dogWeight,int days,String dogBreed)  
      {
         //calculate subtotal
         double subtotal = .70 * dogWeight * days;
         int HighRiskFee = 20;

         //calculate subtotals with high risk fee
         if(dogBreed.equalsIgnoreCase("Pit bull"))
            subtotal += HighRiskFee;     
         else if(dogBreed.equalsIgnoreCase("Rottweiler"))
            subtotal += HighRiskFee;
         else if(dogBreed.equalsIgnoreCase("Doberman Pinscher"))
            subtotal += HighRiskFee;   

         return subtotal;

      }

      //method to calculate the tax
      public static double calculateTax(double subTotal)
      {
         double tax = 0.06;
         double totaltax;
         totaltax = subTotal * tax;
         return totaltax;
      }

      //method to calculate the total
      public static double calculateTotal(double subTotal,double totalTax)
      {
         double total = subTotal + totalTax;
         return total;
      }   

      //method to calculate all bills
      public static double calculateAllBills(double total)
      {
         double totalBills = 0;
         totalBills += total;
         return totalBills;
      }

      //method to calculate average bills   
      public static double calculateAverageBill(double totalAllBills,int numOfRecords)
      {
         double average = totalAllBills / numOfRecords;
         return average; 
      }

      //method to display information
      public static void displayInformation(String firstName,String lastName,String dogBreed,int dogWeight,int days,double subTotal,double totalTax,double total)
      {
         System.out.println("\nCustomer: " + firstName + " " + lastName);
         System.out.println("Breed: " + dogBreed);
         System.out.println("Dog's weight: " + dogWeight + " pounds.");
         System.out.println("Boarding days: " + days + " day(s).");
         System.out.printf("Subtotal for " + days + " days: $%.2f", subTotal);
         System.out.printf("\nTax amount: $%.2f", totalTax);
         System.out.printf("\nTotal due: $%.2f", total);
         System.out.println();
      }

}//end class   

This is the exception error I get:

Exception in thread "main" java.io.FileNotFoundException: boarding.txt (No such file or directory)
    at java.io.FileInputStream.open0(Native Method)
    at java.io.FileInputStream.open(FileInputStream.java:195)
    at java.io.FileInputStream.<init>(FileInputStream.java:138)
    at java.util.Scanner.<init>(Scanner.java:611)
    at Project3_Josh_Rivera.main(Project3_Josh_Rivera.java:10)

1 Answers1

-2

Looks like you are not placing the file i.e. boarding.txt at correct place. If you want to keep the code same way... then move the file to one level up from src folder into the main project folder.

Rohan Jain
  • 3
  • 1
  • 2
  • 1
    Could you be a bit clearer about this? I think I get what you're saying but at least for *me*, "main project folder" is layers *above* where source would live. – Makoto Nov 20 '17 at 23:10