18.5k views
3 votes
Complete the following program that reads a sequence of integers from a file "nums.txt" and output those integers that are multiples of 10 to a file named "multipleof10.txt". The output should be in the same order as they appear in the input file, and each integer is on a separate line. The content of the file "multipleof 10.txt" will appear in the console, so that you can verifu your answer.

1 Answer

5 votes

Final answer:

To complete the program, you need to read integers from a file and output multiples of 10 to another file. The code provided accomplishes this task.

Step-by-step explanation:

To complete the program, you can use the following code:

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;

public class MultipleOfTen {
public static void main(String[] args) throws IOException {
File inputFile = new File("nums.txt");
File outputFile = new File("multipleof10.txt");
FileWriter writer = new FileWriter(outputFile);

Scanner scanner = new Scanner(inputFile);
while (scanner.hasNextInt()) {
int num = scanner.nextInt();
if (num % 10 == 0) {
writer.write(num + "\\");
}
}

writer.close();

Scanner outputScanner = new Scanner(outputFile);
while (outputScanner.hasNextLine()) {
System.out.println(outputScanner.nextLine());
}
}
}

User Chrishomer
by
8.0k points