I am really lost on this one.
I am creating (just for my own practice and because it's kind of cool) a program to encrypt a string according to a very basic algorithm I designed. That part works fine, but of course it needs a decoder.
I decided to include an access code verification system that will cause the program not to run without a valid access code - again, just because.
But the debugging output I am getting from this program is insane. Because it's over 400 lines long, I will try to post the least amount of code I can.
First, here is the pertinent part of main() which executes the decoder if the command line arg is -d:
So all that does, minus comment and debugging prints, is create a Decoder, then run its primary method. Simple so far, yeah?
Here is the Decoder's constructor:
class Decoder { private Scanner scanner; private StringBuilder acSB, encSB; private String ls; private File inf; private String decode_this, accessCode; public Decoder() { try { //File to be encoded must be called "enc.txt" and be in the //INPUT folder. inf = new File("C:/StringEncoder/INPUT/enc.txt"); scanner = new Scanner(inf); acSB = new StringBuilder((int) inf.length()); encSB = new StringBuilder((int) inf.length()); ls = System.getProperty("line.separator"); } catch(FileNotFoundException ex) { System.err.println("ERROR:" + " File to be decoded must be called" + " \"enc.txt\" and must be placed in" + " the INPUT folder."); System.err.println("\n\n" + ex); System.exit(1); } try { //First line is access code, so transfer that //to acSB. acSB.append(scanner.nextLine() + ls); //Debugging println()'s. System.out.println("acSB toString():"); System.out.println(acSB.toString()); //Send the rest of the file into encSB. while(scanner.hasNextLine()) { encSB.append(scanner.nextLine()+ ls); } } finally { //Close scanner up. scanner.close(); } //Deconstructing StringBuilders into Strings. System.out.println("Constructor sets accessCode:"); accessCode = acSB.toString(); System.out.println(accessCode); decode_this = encSB.toString(); System.out.println("Constructor printing accessCode:"); System.out.println(accessCode); }
So basically, it looks to the input file (which obviously must be in a certain place and have a certain name), scans the first line (which must be the access code) into acSB, scans the rest (which will be the encoded message) into encSB, and closes the scanner. Then it deconstructs the StringBuilders into Strings, prints them for debugging, and it's done!
Now at NO point yet has there been anything that should call the decode() method. Here is that method:
public void decode() { //verifyAccess() is a method to make sure that the access code is //a valid one. System.out.println("decode() is executing."); if(!verifyAccess()) { //These println() statements will not display if called //from System.out. System.err.println("That stupid thing is running."); System.err.println("ERROR: Bad access code in file."); System.err.println("That stupid thing ran."); System.exit(1); } }
Of course it's incomplete, but given a false return value from verifyAccess(), it doesn't need any more code yet, because it should shut the program down.
Now, so far, everything looks peachy. But then we look at the output this insanity generates when run with the code so far:
So as you can see, the System.err.println() calls within the if() statement inside of decode() are being called in the middle of the constructor, which has no calls to decode()! PLUS the debugging statement that indicates that decode() has been called never shows up when that is going on.run:
main() is instantiating a Decoder.
acSB toString():
ZYRSQQSKCYFWNXTZWCJP
Constructor sets accessCode:
ZYRSQQSKCYFWNXTZWCJP
That stupid thing is running.
ERROR: Bad access code in file.
Constructor printing accessCode:
That stupid thing ran.
ZYRSQQSKCYFWNXTZWCJP
main() is calling dec.decode().
decode() is executing.
verifyAccess() executes.
false
Java Result: 1
BUILD SUCCESSFUL (total time: 0 seconds)
Additionally, if you put those statements inside that culprit if() statement as System.out.println()'s, they will not display. Only error output. But there is no reason that thing should be executing at all. That if() statement is the responsibility of the decode() method, and nothing in the constructor is calling decode(), so there is no way anything of that if() statement should be executing.
Obviously, I am missing something big. Usually I can figure out where the bug is with enough println()'s and following the trail of execution a few times, but this totally baffles me. Unless there is something I really don't know about scanners or standard error, or maybe file I/O, this pattern of execution seems like it should not be possible.
I thank anyone in advance for any advice.
-summit45