I'm learning Java in my college curriculum. I cannot express how happy I am to finally be learning Java. In an ideal school schedule, I would have learned it sooner. Due to an old academic year audit and military stuff, I'm at a disadvantage and am learning it late, and could use some help making a basic cipher.
A sample Input from a file looks like this. It looks like a run-together mess and is different from the nice, clean input files I'm used to.
3 A SAMPLE PLAINTEXT
13 SOME MORE SAMPLE TEXT
0 THIS SAMPLE TEXT IS SHIFTED BY NOTHING SO THE CIPHERTEXT WILL BE READABLE
26 THIS SAMPLE TEXT WRAPS ALL THE WAY AROUND THE ALPHABET SO WILL ALSO BE READABLE
29 A SAMPLE PLAINTEXT
At the beginning of each input is the cipher number, I call it k. That is the number to which each letter in the alphabet must be shifted. So 3 A SAMPLE PLAINTEXT means K is 3, and the output must change the phrase to "Case #1: D VDPSOH SODLQWHAW." I'm having trouble understanding what to do.
1. In C programming (bleh) inputs usually tell you how many characters you are working with so you know what to scan in from the input file. This time around the only thing that separates lines of inputs is the cipher number, then some text, then another number, then some text, and so on. I'm not sure how to properly loop this. I will pseudocode out my dilemma.
while(fin.hasNextInt()) //Is this even the right condition?
{
//initialize stuff
k = fin.nextInt;
while(//HELP)
{
//Scan in words
}
}
I'm not sure what conditions I should be using to safely and properly scan in the text.
2. There are different ways to perform the ciphering. I, thinking in C again, suggested to my teacher that the program read in the ascii values. The teacher said that each letter of the alphabet in question can be cast as an arbitary integer, and then I use the cipher to change the integers of each letter. How do I convert the inputted letters to numbers?
Posts
This includes a function called getNumericValue (inputs a char, outputs an int) which I think might be the simplest method of conversion from char to int?
However, this presents the problem of ciphering past the limit of 26. Let's say the input is:
3 ABCXYZ
ABC are easily cipher to DEF
XYZ however shouldn't turn into .,; or whatever lies beyond Z in ASCII/Unicode.
If the int values returned are nice and simple (1-26 for A-Z), then you can just use the modulus operator % after you've added your 3 to get it to turn from 29 back into 3 (in the case of Z).
On the other hand, if it's something lame (35-61 for A-Z) then you can't just mod the output so easily. An if statement (if int < 35 + k, then int = int + 35 or something similar) might do the trick.
In Java, a char (character) is just a primitive type, so you can actually treat it just like a numerical representation of the character. The only trick is to make sure you cast it correctly based on your typing. Using http://www.asciitable.com/ as reference:
System.out.println(c1); //prints "A"
System.out.println((int)c1); //prints "65"
char c2 = (char)(c1+3);
System.out.println(c2); //prints "D"
System.out.println((int)c2); //prints "68"
This combined with the advice Bethryn outlined should be enough to get you going.
Good luck!
I use BufferedReaders constantly, getting it a line at a time makes my life infintely easier when doing stuff like this.