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.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;...

  • 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...

  • 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 */ } }

  • What does this program print? package javaapplication210; public class JavaApplication210 { public static void main(String[]args){ System.out.printf("Result...

    What does this program print? package javaapplication210; public class JavaApplication210 { public static void main(String[]args){ System.out.printf("Result is: % d/n", mystery (-5, -9));//System.out.printf("Result is: % d/n", mystery (-4, -8));//System.out.printf("Result is: % d/n", mystery (-6, -7));//} public static int mystery(int a, int b) { if(b - 1) returns a; else return a + mystery(a, b + 1); } }

  • Project 7-3 Guessing Game import java.util.Scanner; public class GuessNumberApp {    public static void main(String[] args)...

    Project 7-3 Guessing Game import java.util.Scanner; public class GuessNumberApp {    public static void main(String[] args) { displayWelcomeMessage(); // create the Scanner object Scanner sc = new Scanner(System.in); String choice = "y"; while (choice.equalsIgnoreCase("y")) { // generate the random number and invite user to guess it int number = getRandomNumber(); displayPleaseGuessMessage(); // continue until the user guesses the number int guessNumber = 0; int counter = 1; while (guessNumber != number) { // get a valid int from user guessNumber...

  • Explain this java code, please. import java.util.Scanner; public class Program11 { public static void main(String[] args)...

    Explain this java code, please. import java.util.Scanner; public class Program11 { public static void main(String[] args) { Scanner stdIn = new Scanner(System.in); final int maxSize = 128; String[] titles = new String[maxSize]; int[] lengths = new int[maxSize]; int numDVDs = 0; String op; op = menu(stdIn); System.out.println(); while (!op.equalsIgnoreCase("q")) { if (op.equalsIgnoreCase("a")) { if (numDVDs < maxSize) numDVDs = addDVD(titles, lengths, numDVDs, stdIn); } else if (op.equalsIgnoreCase("t")) searchByTitle(titles, lengths, numDVDs, stdIn);    else if (op.equalsIgnoreCase("l")) searchByLength(titles, lengths, numDVDs, stdIn); System.out.println('\n');...

  • 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.*; public class PairFinder { public static void main(String[] args) { Scanner sc...

    import java.util.*; public class PairFinder { public static void main(String[] args) { Scanner sc = new Scanner(System.in);    // Read in the value of k int k = Integer.parseInt(sc.nextLine());    // Read in the list of numbers int[] numbers; String input = sc.nextLine(); if (input.equals("")) { numbers = new int[0]; } else { String[] numberStrings = input.split(" "); numbers = new int[numberStrings.length]; for (int i = 0; i < numberStrings.length; i++) { numbers[i] = Integer.parseInt(numberStrings[i]); } }    System.out.println(findPairs(numbers, k)); }    //method that...

  • 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...

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
ADVERTISEMENT