I have to do the follwing: Replace all occurrence of grade 100 in “grade.txt” wi
ID: 3760152 • Letter: I
Question
I have to do the follwing:
Replace all occurrence of grade 100 in “grade.txt” with the string “Excellent” and store them in a new text file “newgrade.txt”
Here's my current code:
package newgrades;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
/**
*
* @author Kevin
*/
public class Newgrades {
public static void main(String[] args) throws IOException{
BufferedReader inputStream = null;
PrintWriter outputStream = null;
try {
inputStream =
new BufferedReader(new FileReader("grade.txt"));
outputStream =
new PrintWriter(new FileWriter("newgrades.txt"));
String l;
while ((l = inputStream.readLine()) != null) {
outputStream.println(l);
}
} finally {
if (inputStream != null) {
inputStream.close();
}
if (outputStream != null) {
outputStream.close();
}
}
}
}
Explanation / Answer
package newgrades;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
/**
*
* @author Kevin
*/
public class Newgrades {
public static void main(String[] args) throws IOException{
BufferedReader inputStream = null;
PrintWriter outputStream = null;
try {
inputStream =
new BufferedReader(new FileReader("grade.txt"));
outputStream =
new PrintWriter(new FileWriter("newgrades.txt"));
String l;
while ((l = inputStream.readLine()) != null) {
if (Integer.parseInt(l) == 100) {
outputStream.println("Excellent");
} else {
outputStream.println(l);
}
}
} finally {
if (inputStream != null) {
inputStream.close();
}
if (outputStream != null) {
outputStream.close();
}
}
}
}