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.: /*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 } } }
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.:
/*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
}
}
}
Trending now
This is a popular solution!
Step by step
Solved in 2 steps with 2 images