Decrypt - alphabet shift

Hi guys,

As part of my learning process I have taken on the challenges athttp://www.pythonchallange.com I have been working on the level 2 challenge in which part of the challenge involves decrypting the following text

"g fmnc wms bgblr rpylqjyrc gr zw fylb. rfyrq ufyr amknsrcpq ypc dmp. bmgle gr gl zw fylb gq glcddgagclr ylb rfyr'q ufw rfgq rcvr gq qm jmle. sqgle qrpgle.kyicrpylq() gq pcamkkclbcb. lmu ynnjw ml rfc spj."

The key to the above is to simply shift all letters 2 places forward ie 'a' becomes 'c','d' becomes 'f' etc.I have solved the challenge easily by using python as well as using my bash shell.I have tried implementing the solution in java but one or two of the caharacters

come out jumbled ('a' as '{' and 'b' as '|' ) and I cannot understand why.Could someone please look over my code (below) as help me understand where I am going wrong.

import java.io.*;

publicclass decrypt

{

publicstaticvoid main(String[] args)throws Exception

{

BufferedInputStream infile =new BufferedInputStream(new FileInputStream("/home/dave/riddle2.txt"));

PrintWriter outfile =new PrintWriter(new FileOutputStream("/home/dave/riddleSoln.txt"));

while (infile.available()!=0)

{

int ch= infile.read();

int sh=ch+2;

int text = sh;

System.out.print((char)text);

outfile.print((char)text);

}

infile.close();

}

}

I have saved the text in the file riddle.txt.I also wanted to write the solution to a file (practising I/O) but have found that though 'outfile.println()' works, 'outfile.print() does not do anything.

Any assistance would be appreciated.

Thanks

[2514 byte] By [theApprenticea] at [2007-11-27 5:36:45]
# 1
grasshopper,"Close your output file you must."PrintWriter's println works anyway because it implicitly flushes the stream at the end of each line.Cheers. Keith.Message was edited by: corlettk fuddle fingers
corlettka at 2007-7-12 15:07:54 > top of Java-index,Java Essentials,New To Java...
# 2

package forums;

import java.io.*;

public class Decrptor

{

public static void main(String[] args) throws Exception {

BufferedInputStream in = new BufferedInputStream(new FileInputStream("Decryptor.in.txt"));

PrintWriter out = new PrintWriter(new FileOutputStream("Decryptor.out.txt"));

while (in.available()!=0) {

int c = in.read();

if (c >= 'A' && c <= 'Z') {

c= ((c-'A'+2) % 26)+'A';

} else if (c >= 'a' && c <= 'z') {

c= ((c-'a'+2) % 26)+'a';

}

System.out.print((char)c);

out.print((char)c);

}

System.out.println();

out.println();

in.close();

out.close();

}

}

the nasty so-and-so's have wrapped the alphabet so y+2=a, z+2=c

corlettka at 2007-7-12 15:07:54 > top of Java-index,Java Essentials,New To Java...
# 3

Ahh, grasshopper now understands!The way of the world is so simple and yet appears complex to he who understands not!

Thanks guys,I hadn't realised I hadn't closed the output file or that the alphabet had to be limited to 26 or it would wrap round to 'other' characters.Funny how one feels like an absolute

numpty when things are explained!

Thanks again!

theApprenticea at 2007-7-12 15:07:54 > top of Java-index,Java Essentials,New To Java...