为什么我的代码找不到文件?

时间:2017-11-20 23:01:08

标签: java

所以我正在为课程做一个项目,这是我的代码。这是我的第一个学期编码,我不能为我的生活弄清楚为什么java找不到我的文件。任何意见或建议都会很棒。我并不是要把所有这些代码放在这里,就像我的代码有什么不对,但我已经在这几天没有成功了,而且明天就要到了。

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   

这是我得到的异常错误:

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 个答案:

答案 0 :(得分:-2)

看起来您没有将文件,即boarding.txt放在正确的位置。 如果你想保持代码的相同方式...然后将文件从src文件夹移动到一级,进入主项目文件夹。