I need HELP. I tried to separate the valid serial keys from invalid ones. I got the output correctly. but then when i tried to write it in a file, ONLY the last line is being written. the output is:
1A000000 1A000001 1A000002 1A000003 1A000004 1A000005 2B200012 3C343455 4D342423 5E324344 6F435435 7G245347
and I want to write this to a file. But ONLY 7G245347 is being written.
import java.util.*;`
import java.io.*;
public class ValidSerialKey {
public static void main(String[] args) throws IOException {
String keys = "";
File file = new File("serialkeys.txt");
try{
Scanner scan = new Scanner(file);
while (scan.hasNext()){
keys = scan.nextLine();
if ((keys.charAt(0) == '1' || keys.charAt(0) == '2' || keys.charAt(0) == '3' || keys.charAt(0) == '4' || keys.charAt(0) == '5' ||
keys.charAt(0) == '6' || keys.charAt(0) == '7' || keys.charAt(0) == '8' || keys.charAt(0) == '9' ) &&
(keys.charAt(1) == 'A' || keys.charAt(1) == 'B' || keys.charAt(1) == 'C' || keys.charAt(1) == 'D' || keys.charAt(1) == 'E' ||
keys.charAt(1) == 'F' || keys.charAt(1) == 'G' || keys.charAt(1) == 'H' || keys.charAt(1) == 'I' || keys.charAt(1) == 'J' ||
keys.charAt(1) == 'K' || keys.charAt(1) == 'L' || keys.charAt(1) == 'M' || keys.charAt(1) == 'N' || keys.charAt(1) == 'O' ||
keys.charAt(1) == 'P' || keys.charAt(1) == 'Q' || keys.charAt(1) == 'R' || keys.charAt(1) == 'S' || keys.charAt(1) == 'T' ||
keys.charAt(1) == 'U' || keys.charAt(1) == 'V' || keys.charAt(1) == 'W' || keys.charAt(1) == 'X' || keys.charAt(1) == 'Y' ||
keys.charAt(1) == 'Z' )){
System.out.println(keys);
File filein = new File("ValidKeys.txt");
try{
try
(PrintWriter pw = new PrintWriter(filein)){
pw.print(keys);
pw.close();
}
}catch (FileNotFoundException ex){
System.out.println(ex.getMessage());
}
}//end of if
}//end of while
scan.close();
}catch (FileNotFoundException exp){
System.out.println(exp.getMessage());
}
}
}