The following program is supposed to read two numbers from a file named Ex20Input.txt, and write the sum of the numbers to a file named Ex20Output.dat. However, it fails to do so. Rewrite the program so that it performs correctly. (You may assume that both numbers are on the same line.) import java.util.*; public class Ch3Ex20 { public static void main(String[] args) { Scanner inFile ¼ new Scanner(new FileReader("Ex20Input.txt")); int num1, num2; num1 = inFile.nextInt(); num2 = inFile.nextInt(); outFile.println("Sum = " + (num1 + num2)); outFile.close(); } }
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.PrintWriter;
import java.util.Scanner;
public class ChEx20 {
public static void main(String[] args) throws FileNotFoundException{
Scanner inFile=new Scanner(new FileReader("Ex20Input.txt"));
PrintWriter outFile=new PrintWriter("Ex20Output.dat");
int num1,num2;
num1=inFile.nextInt();
num2=inFile.nextInt();
outFile.println("Sum="+(num1+num2));
inFile.close();
outFile.close();
}
}
Comments
Leave a comment