Answer:
Explanation:
The following code is written in Java. It reads every input and checks to see if it is valid. If it is it writes it to the output file.
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
class Brainly {
public static void main(String[] args) {
try {
File myObj = new File("C:/Users/Gabriel2/AppData/Roaming/JetBrains/IdeaIC2020.2/scratches/input.txt");
Scanner myReader = new Scanner(myObj);
FileWriter myWriter = new FileWriter("C:/Users/Gabriel2/AppData/Roaming/JetBrains/IdeaIC2020.2/scratches/output.txt");
while (myReader.hasNextLine()) {
String data = myReader.nextLine();
int sum = 0;
for (int x = 0; x < 5; x++ ) {
sum += data.charAt(x);
}
int remainder = (sum % 10);
System.out.println(remainder);
System.out.println(data.charAt(5));
if (remainder == Integer.parseInt(String.valueOf(data.charAt(5)))) {
System.out.println("entered");
try {
myWriter.write(data + "\n");
System.out.println("Successfully wrote to the file.");
} catch (IOException e) {
System.out.println("An error occurred.");
e.printStackTrace();
}
}
sum = 0;
}
myWriter.close();
myReader.close();
} catch (FileNotFoundException e) {
System.out.println("An error occurred.");
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}