0

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

1 Answers1

2

You want to keep the PrintWriter opened to write other things during the next iterations of the loop
So don't create a new one at each iteration.

As a side note, you don't need to close explicitly the PrintWriter instance when you use try with resources.

You should replace this logic :

loop     
    try
       (PrintWriter pw = new PrintWriter(uniqueFile)){
           pw.print(keys);             
    }//end of inner try

end loop

by a logic where you include the whole logic in the try with resources statement :

try(PrintWriter pw = new PrintWriter(uniqueFile)){
    loop  
        pw.print(keys);                       
    end loop
}
catch (IOException e){ 
   ... // exception handling
}
davidxxx
  • 125,838
  • 23
  • 214
  • 215
  • 1
    This will work only once, you closed the `PrintWriter` in the loop. This should be done outside of the loop. – AxelH Jun 16 '17 at 08:20
  • Right. Thank you :) I am thinking about your comment and a better way to address the problem. I updated. Finally `try with resources` is suitable. What do you think of this version ? – davidxxx Jun 16 '17 at 08:28
  • Depends if OP want to break the loop on the exception. I would keep the try-with-resource like you did, but catch some exception inside it (`PrintWriter.append` don't specified any, so in your case, is there a risk ? not sure). But we could prevent a loop breaking based on the input of the user (Scanner) (But I am thinking to much here ;) ) – AxelH Jun 16 '17 at 08:36
  • "Depends if OP want to break the loop on the exception." In fact, only the `PrintWriter()` constructor declares throwing a `FileNotFoundException`. So, we will get the exception soon. If `FileNotFoundException` is throws, looping makes no really sense. – davidxxx Jun 16 '17 at 08:50
  • 1
    "PrintWriter.append don't specified any, so in your case, is there a risk ? not sure" Indeed `PrintWriter` methods don't throw I/O exceptions, but some of its constructors do. For example in the `write(String)` method , you can see : `catch (IOException x) { trouble = true; }` – davidxxx Jun 16 '17 at 08:51
  • Oh yeah, I remember that boolean logic now (so do we rethrow it if needed ?), so indeed it is safe to use like this (unless user input something bad in the original code). So definitly, I would use your actual code ;) – AxelH Jun 16 '17 at 08:54