Task - File Handling - Students to be Warned #31
Replies: 27 comments
-
|
`package File_Handling; public class Warning { } |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
|
`package Week4; // **************************************************************************** import java.io.*; public class Warning { public static void main(String[] args) { } |
Beta Was this translation helpful? Give feedback.
-
|
import java.util.Scanner; import javax.lang.model.type.NullType; import java.io.*; class Warning { } |
Beta Was this translation helpful? Give feedback.
-
|
`import java.io.*; } |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
|
// **************************************************************************** import javax.naming.NameAlreadyBoundException; import java.io.*; public class Warning { } |
Beta Was this translation helpful? Give feedback.
-
|
import java.util.; public class studentfile { } |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
|
`import java.util.; public class Warning { } |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
|
import java.util.Scanner; import javax.lang.model.type.NullType; import java.io.*; public class Warning } |
Beta Was this translation helpful? Give feedback.
-
import java.util.Scanner;
import java.io.*;
public class Warning {
public static void main(String[] args) {
int creditHrs;
double qualityPts;
double gpa; // grade point (quality point) average
String line, name, inputName = "week4/Tuesday/students.dat";
String outputName = "week4/Tuesday/warning.dat";
try {
// Set up scanner to input file
Scanner scanner = new Scanner(new FileReader(inputName));
// Set up the output file stream
FileWriter fwrite = new FileWriter(outputName, true);
// Print a header to the output file - "Students on Academic Warning"
fwrite.write("Students on Academic Warning\n");
// Process the input file, one token at a time
while (scanner.hasNextLine()) {
// Get the credit hours and quality points and
// determine if the student is on warning. If so,
// write the student data to the output file.
line = scanner.nextLine();
String[] eachTokenOfLine = line.split(" ");
name = eachTokenOfLine[0];
creditHrs = Integer.parseInt(eachTokenOfLine[1]);
qualityPts = Double.parseDouble(eachTokenOfLine[2]);
gpa = qualityPts / (double) creditHrs;
if ((gpa < 1.5 && creditHrs < 30) || (gpa < 1.75 && creditHrs < 60) || (gpa < 2)) {
fwrite.write(line + "\n");
}
}
// Close output file
fwrite.close();
scanner.close();
} catch (FileNotFoundException exception) {
System.out.println("The file " + inputName + " was not found.");
} catch (IOException exception) {
System.out.println(exception);
} catch (NumberFormatException e) {
System.out.println("Format error in input file: " + e);
}
}
} |
Beta Was this translation helpful? Give feedback.
-
import java.util.Scanner;
import java.io.*;
public class FileHandling2 {
public static boolean hasWarning(double gpa, int creditHrs) {
return ((creditHrs < 30 && gpa < 1.5) || (creditHrs < 60 && gpa < 1.75) || (gpa < 2.0));
}
public static void main(String[] args) {
int creditHrs;
double qualityPts;
double gpa;
String line, inputName = "Students.dat";
String[] info;
String outputName = "warning.dat";
try (Scanner scanner = new Scanner(new File(inputName))) {
try (FileWriter writer = new FileWriter(outputName)) {
while (scanner.hasNextLine()) {
line = scanner.nextLine();
info = line.split(" ");
creditHrs = Integer.parseInt(info[1].trim());
qualityPts = Double.parseDouble(info[2].trim());
gpa = qualityPts / creditHrs;
if (hasWarning(gpa, creditHrs))
writer.write(line + "\n");
}
}
System.out.println("File written ");
} catch (FileNotFoundException exception) {
System.out.println("The file " + inputName + " was not found.");
} catch (IOException exception) {
System.out.println(exception);
} catch (NumberFormatException e) {
System.out.println("Format error in input file: " + e);
}
}
} |
Beta Was this translation helpful? Give feedback.
-
import java.util.Scanner;
import java.io.*;
public class Warning {
public static boolean hasWarning(double gpa, int creditHrs) {
return ((gpa < 1.5) && (creditHrs < 30) || (gpa < 1.75) && (creditHrs < 60) || (gpa < 2.0));
}
public static void main(String[] args) {
int creditHrs;
double qualityPts;
double gpa;
String line, name, inputName = "students.dat";
String outputName = "warning.dat";
try {
FileReader file = new FileReader(inputName);
Scanner scanner = new Scanner(file);
FileWriter fileWriter = new FileWriter(outputName, true);
fileWriter.write("Students on Academic Warning\n");
while (scanner.hasNext()) {
line = scanner.nextLine();
String[] data = line.split(" ");
name = data[0];
creditHrs = Integer.parseInt(data[1]);
qualityPts = Double.parseDouble(data[2]);
gpa = qualityPts / creditHrs;
if (hasWarning(gpa, creditHrs)) {
fileWriter.write(line + "\n");
}
}
fileWriter.close();
scanner.close();
} catch (FileNotFoundException exception) {
System.out.println("The file " + inputName + " was not found.");
} catch (IOException exception) {
System.out.println(exception);
} catch (NumberFormatException e) {
System.out.println("Format error in input file: " + e);
}
}
} |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Reading from and Writing to Text Files
Write a program that will read in a file of student academic credit data and create a list of students on academic warning. The list of students on warning will be written to a file. Each line of the input file will contain the student name (a single String with no spaces), the number of semester hours earned (an integer), the total quality points earned (a double). The following shows part of a typical data file:
The program should compute the GPA (grade point or quality point average) for each student (the total quality points divided by the number of semester hours) then write the student information to the output file if that student should be put on academic warning. A student will be on warning if he/she has a GPA less than 1.5 for students with fewer than 30 semester hours credit, 1.75 for students with fewer than 60 semester hours credit, and 2.0 for all other students. The file Warning.java contains a skeleton of the program. Do the following:
-- A FileNotFoundException if the input file does not exist
-- A NumberFormatException if it can’t parse an int or double when it tries to – this indicates an error in the input file format
-- An IOException if something else goes wrong with the input or output stream
Add a catch for each of these situations, and in each case give as specific a message as you can. The program will terminate if any of these exceptions are thrown, but at least you can supply the user with useful information.
students.dat
Expected Output
warning.dat
Beta Was this translation helpful? Give feedback.
All reactions