Question

Cant figure out how to fix error Code- import java.io.File; import java.io.IOException; import java.util.*; public class Program8 {    public static void main(String[] args)throws IOException{       ...

Cant figure out how to fix error

Code-

import java.io.File;
import java.io.IOException;
import java.util.*;

public class Program8 {
   public static void main(String[] args)throws IOException{
       File prg8 = new File("program8.txt");
       Scanner reader = new Scanner(prg8);
       String cName = "";
       int cID = 0;
       double bill = 0.0;
       String email = "";
       double nExempt = 0.0;
       String tExempt = "";
       int x = 0;
       int j = 1;

       while(reader.hasNextInt()) {
           x = reader.nextInt();}
       Customers c1 [] = new Customers [x];
       for (int i = 0; i < x; i++) {
           cName = reader.next();
           cID = reader.nextInt();
           bill = reader.nextDouble();
           email = reader.next();  
           if (reader.hasNextDouble()) {
               nExempt = reader.nextDouble();
               c1 [i] = new Customers(cName, cID, bill, email, nExempt);          
           }
           else if (reader.hasNext()) {
               tExempt = reader.next();
               c1 [i] = new Customers(cName, cID, bill, email, tExempt);      
           }  
       }  
       reader.close();
       Arrays.sort(c1);
       for (int i = 0; i < x ; i++) {

           if (i % 45 == 0) {
               System.out.println("\n");
               printHeader(j);
               j++;
           }

           System.out.println(c1[i]);
       }  

   }
   public static void printHeader(int j) {

       System.out.printf("%84s %32s ", "Office Supplies Inc Customer Report" , "Page: " + j + " \n");
       System.out.printf("%87s", "=================================== \n\n\n");
       System.out.printf("%-20s %5s %27s %30s %16s %13s","Customer Name" ,"ID", "Email Address", "Balance" ,"Tax Type" , "Tax Amount" + "\n");
       System.out.printf("%-20s %5s %27s %30s %16s %14s","=============" ,"==", "=============", "=======" ,"========" , "========== " + "\n");
       System.out.println();
   }
}

class Customers implements Comparable<Customers> {
   private String cName;
   private int cID;
   private double bill;
   private String email;
   private double nExempt;
   private String tExempt;

   public Customers() {
       cName = "";
       cID = 0;
       bill = 0.0;
       email = "";
       nExempt = 0.0;
       tExempt = "";
   }

   public Customers (String name, int cID, double bill, String email, double nExempt) {
       this.cName = name;
       this.cID = cID;
       this.bill = bill;
       this.email = email;
       this.nExempt = nExempt;
   }
   public Customers (String name, int cID, double bill, String email, String tExempt) {
       this.cName = name;
       this.cID = cID;
       this.bill = bill;
       this.email = email;
       this.tExempt = tExempt;
   }

   public String getCName () {
       return cName;
   }
   public String getEmail () {
       return email;
   }
   public String getTExempt () {
       return tExempt;
   }
   public int getCID() {
       return cID;
   }
   public double getBill() {
       return bill;
   }
   public double getnExempt() {
       return nExempt;
   }
   public void setCName (String cName) {
       this.cName = cName;
   }
   public void setEmail (String email) {
       this.email = email;
   }
   public void setTExempt (String tExempt) {
       this.tExempt = tExempt;
   }
   public void setCID (int cID) {
       this.cID = cID;
   }
   public void setnExempt (double nExempt) {
       this.nExempt = nExempt;
   }


   @Override
   public int compareTo(Customers it) {
       if (this.getCID()== it.getCID()) {
           return 0;
       }
       else if (this.getCID() < it.getCID()) {
           return -1;
       }
       else
           return 1;
   }
   @Override
   public String toString() {
       String temp = "";
       if (nExempt != 0.0) {
           temp = String.format("%-20s %5d\t\t %-30s %,13.2f %15s %12.2f", cName, cID, email, bill, "tax liable", (nExempt * bill) );
       }
       else {
           temp = String.format("%-20s %5d\t\t %-30s %,13.2f %15s ", cName, cID, email, bill, tExempt );
       }
       return temp;
   }
}

Error-

Exception in thread "main" java.io.FileNotFoundException: program6.txt (No such file or directory)

at java.base/java.io.FileInputStream.open0(Native Method)

at java.base/java.io.FileInputStream.open(FileInputStream.java:220)

at java.base/java.io.FileInputStream.<init>(FileInputStream.java:158)

at java.base/java.util.Scanner.<init>(Scanner.java:639)

at Program6.main(Program6.java:8)

0 0
Add a comment Improve this question Transcribed image text
Answer #1

This error is happening because of the line File prg8 = new File("program8.txt");

The reason for this error is that the java compiler is unable to find this file while running the code and hence, throwing the error.

Following are the steps to get rid of this error:

1. Make sure that the file program8.txt is created in the same directory as your project workspace.

2. In order to be safer, provide the full absolute path of the file while opening it.

For example, File prg8 = new File("/Usrs/project/program8.txt");

Add a comment
Know the answer?
Add Answer to:
Cant figure out how to fix error Code- import java.io.File; import java.io.IOException; import java.util.*; public class Program8 {    public static void main(String[] args)throws IOException{       ...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • import java.util.Scanner; public class StudentClient {       public static void main(String[] args)    {   ...

    import java.util.Scanner; public class StudentClient {       public static void main(String[] args)    {        Student s1 = new Student();         Student s2 = new Student("Smith", "123-45-6789", 3.2);         Student s3 = new Student("Jones", "987-65-4321", 3.7);         System.out.println("The name of student #1 is ");         System.out.println("The social security number of student #1 is " + s1.toString());         System.out.println("Student #2 is " + s2);         System.out.println("the name of student #3 is " + s3.getName());         System.out.println("The social security number...

  • import java.util.Random; import java.util.ArrayList; /** * */ public class hw5_task8 { public static void main(String[] args)...

    import java.util.Random; import java.util.ArrayList; /** * */ public class hw5_task8 { public static void main(String[] args) { int[] grades = randomIntArr(10); printIntArray("grades", grades); ArrayList<Integer> indexesF_AL = selectIndexes_1(grades); System.out.println(" indexesF_AL: " + indexesF_AL); int[] indexesF_Arr = selectIndexes_2(grades); printIntArray("indexesF_Arr",indexesF_Arr); } public static int[] randomIntArr(int N){ int[] res = new int[N]; Random r = new Random(0); for(int i = 0; i < res.length; i++){ res[i] = r.nextInt(101); // r.nextInt(101) returns an in in range [0, 100] } return res; } public static void...

  • import java.util.Arrays; public class lab {    public static void main(String args[])    {    int...

    import java.util.Arrays; public class lab {    public static void main(String args[])    {    int arr[] = {10, 7, 8, 9, 1, 5,6,7};    int arr2[] = {9, 8, 7, 6, 5, 4, 3, 2, 1};    int arr3[] = {1, 3, 5, 3, 2, 6, 20};    quicksort(arr,0,arr.length-1);    quicksort(arr2,0,arr2.length-1);    quicksort(arr3,0,arr3.length-1);    System.out.println(Arrays.toString(arr));    System.out.println(Arrays.toString(arr2));    System.out.println(Arrays.toString(arr3));       }    private static int partition(int[] items,int low, int high)    {    int i=0;    int j=0;...

  • For the below code, what will be printed? public class mathchar { public static void main(String[]...

    For the below code, what will be printed? public class mathchar { public static void main(String[] args) { int i = 81, j = 3, k = 6; char w = 'f'; System.out.printf("%.2f\n", Math.sqrt(i)); System.out.printf("%.2f\n", Math.pow(j,k)); System.out.printf("%c\n", Character.toUpperCase(w)); System.out.printf("%c\n", Character.toLowerCase(w)); System.out.printf("%d\n", (int)(Math.random() * 21 + 6)); /* just tell range of possible values */ } }

  • Fix this program package chapter8_Test; import java.util.Scanner; public class Chapter8 { public static void main(String[] args)...

    Fix this program package chapter8_Test; import java.util.Scanner; public class Chapter8 { public static void main(String[] args) { int[] matrix = {{1,2},{3,4},{5,6},{7,8}}; int columnChoice; int columnTotal = 0; double columnAverage = 0; Scanner input = new Scanner(System.in); System.out.print("Which column would you like to average (1 or 2)? "); columnChoice = input.nextInt(); for(int row = 0;row < matrix.length;++row) { columnTotal += matrix[row][columnChoice]; } columnAverage = columnTotal / (float) matrix.length; System.out.printf("\nThe average of column %d is %.2f\n", columnAverage, columnAverage); } } This program...

  • import java.util.Scanner; public class TriangleMaker {    public static void main(String[] args) {        //...

    import java.util.Scanner; public class TriangleMaker {    public static void main(String[] args) {        // TODO Auto-generated method stub        System.out.println("Welcome to the Triangle Maker! Enter the size of the triangle.");        Scanner keyboard = new Scanner(System.in);    int size = keyboard.nextInt();    for (int i = 1; i <= size; i++)    {    for (int j = 0; j < i; j++)    {    System.out.print("*");    }    System.out.println();    }    for (int...

  • import java.util.Scanner; public class MPGMain {    public static void main(String[] args)    {       ...

    import java.util.Scanner; public class MPGMain {    public static void main(String[] args)    {        Scanner input = new Scanner(System.in);               Mileage mileage = new Mileage();               System.out.println("Enter your miles: ");        mileage.setMiles(input.nextDouble());               System.out.println("Enter your gallons: ");        mileage.setGallons(input.nextDouble());               System.out.printf("MPG : %.2f",mileage.getMPG());           } } public class Mileage {    private double miles;    private double gallons;    public double getMiles()...

  • Draw a flowchart for this program public class InsertionSort {     public static void main(String a[]) {...

    Draw a flowchart for this program public class InsertionSort {     public static void main(String a[]) {         int[] array = {7, 1, 3, 2, 42, 76, 9};         insertionSort(array);     }     public static int[] insertionSort(int[] input) {         int temp;         for (int i = 1; i < input.length; i++) {             for (int j = i; j > 0; j--) {                 if (input[j] < input[j - 1]) {                     temp = input[j];                     input[j] = input[j - 1];                     input[j - 1] = temp;                 }             }             display(input, i);...

  • How to arrange the result neatly? public class LabTest { public static void main(String[] args) {...

    How to arrange the result neatly? public class LabTest { public static void main(String[] args) { String[] state= {"Johor", "Kedah","Kelantan","Melaka","Negeri Sembilan","Pahang","Perak","Perlis","Pulau Pinang","Sabah","Sarawak","Selangor","Terengganu","Wilayah Persekutuan Labuan","Wilayah Persekutuan Kuala Lumpur"}; int [] rainfall= {1133,1312,1699,1220,1450,1596,1350,1189,1347,1987,1999,1125,1789,1980,1374}; int temp; String tempN; for (int i=0;i<rainfall.length;i++){ for (int j=0;j<rainfall.length;j++){ if (rainfall[i]>rainfall[j]){ temp=rainfall[i]; rainfall[i]=rainfall[j]; rainfall[j]=temp;    tempN=state[i]; state[i]=state[j]; state[j]=tempN; } } } for (int i=0;i<rainfall.length;i++){ System.out.print(state[i] + "\t"); } System.out.println(""); for (int i=0;i<rainfall.length;i++){ System.out.print(rainfall[i] + "\t");    } } } Sabah Kelantan Pahang Perak Pulau Pinang Kedah Sarawak Wilayah...

  • Priority Queue Demo import java.util.*; public class PriorityQueueDemo {    public static void main(String[] args) {...

    Priority Queue Demo import java.util.*; public class PriorityQueueDemo {    public static void main(String[] args) {        //TODO 1: Create a priority queue of Strings and assign it to variable queue1               //TODO 2: Add Oklahoma, Indiana, Georgia, Texas to queue1                      System.out.println("Priority queue using Comparable:");        //TODO 3: remove from queue1 all the strings one by one        // with the "smaller" strings (higher priority) ahead of "bigger"...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT