Answer to Question #186637 in Java | JSP | JSF for aaaaaaaaaa

Question #186637

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(); } }


1
Expert's answer
2021-04-29T03:15:14-0400
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();
        
    }

}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS