I am Having trouble with this Java code it keeps giving me the error " Error in finding file" and I do  not know how to fix it. How and where can I create an input file in my current directory for the program to work:

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question

I am Having trouble with this Java code it keeps giving me the error " Error in finding file" and I do  not know how to fix it. How and where can I create an input file in my current directory for the program to work:

/*NOTEPAD++ EDITOR*/
// import necessary package
import java.util.*;
import java.io.*;

public class Main{
   public static void main(String[] args){
       //Do write your code in try block and after try block
       //write a catch block for each suitable exceptions
       try{
           ///create an instance of File which store the ref. of input file "Students.dat"
           File infile = new File("Students.dat");
           // read file using scanner class
           Scanner scan = new Scanner(infile);
           // create instane of PrintWriter class fro writing the output file
           PrintWriter outFile = new PrintWriter("warning.dat");
           // While loop will run till last line in file
           while(scan.hasNextLine()){
               String line = scan.nextLine(); /// Now store the line from file in line variable
               //System.out.println(line); /// you can print the line on console if you want
               String [] student = line.split(" "); // Now split the line by space and store in string array
               int crHr = Integer.parseInt(student[1]); // parse the credit hour in integer and store in crHr var
               double qualityPnt = Double.parseDouble(student[2]); // parse the quality point in double and store in qualityPnt var
               double gpa = qualityPnt/crHr; // NOw compute the GPA of students and store in gpa var
               // Now check if gpa is less than 1.5 for credit hour is fewer than 30
               if(crHr < 30 && gpa < 1.5){
                   outFile.println(line);// if condition true, writeback the line in outfile "warning.dat"
               }
               // Now check if gpa is less than 1.75 for credit hour is fewer than 60
               else if((crHr > 30 && crHr < 60) && gpa < 1.75){
                   outFile.println(line);// if condition true, writeback the line in outfile "warning.dat"
               }
               // check if gpa is less than 2.0 for all other students
               else if( crHr > 60 && gpa < 2.0){
                   outFile.println(line);// if condition true, writeback the line in outfile "warning.dat"
               }
              
           }
           //close the files
           scan.close();
           outFile.close();
       }
       // this catch block occurs when input file does not exist in current working directory
       catch(FileNotFoundException e){
           System.out.println("ERROR in finding file!!"); // printing the error msg
       }
       // this catch block occurs when number are not in correct format
       catch(NumberFormatException e){
           System.out.println("ERROR in input file format!!!"); // printing the error msg
       }
       // this catch block occurs when data input does not match the method
       catch(InputMismatchException e){
           System.out.println("ERROR in input format!!!"); // printing the error msg
       }
      
   }
}

Expert Solution
steps

Step by step

Solved in 3 steps with 1 images

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY