The new forums will be named Coin Return (based on the most recent vote)! You can check on the status and timeline of the transition to the new forums here.
The Guiding Principles and New Rules document is now in effect.

Learning Java finally, need basic cipher help.

CantidoCantido Registered User regular
edited September 2011 in Help / Advice Forum
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?

3DS Friendcode 5413-1311-3767
Cantido on

Posts

  • SeolSeol Registered User regular
    Java's Scanner class can help you here. Check out readInt and readLine. readLine will give you a String: generally, you don't want to think on a bytes/chars level with Strings (it's not a good way to approach Java), but the problem requires it here, and there's an option to convert a String into an array of bytes in getBytes. That should hopefully be enough to see you on your way. :)

  • mightyjongyomightyjongyo Sour Crrm East Bay, CaliforniaRegistered User regular
    readLine should give you all characters up to the first \n, so that answers your problem with separating inputs. Basically loop until readLine returns some wrong value. From this string, you can get the ascii values by converting it to a character array (String class has built in functions for that). For that first value there is a parseInt or something like that you can use to get the integer value for that character (or look at an ascii table and subtract the offset).

  • BethrynBethryn Unhappiness is Mandatory Registered User regular
    Cantido wrote:
    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?
    Character class: http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Character.html

    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.

    ...and of course, as always, Kill Hitler.
  • RhapsodyRhapsody Registered User regular
    Cantido wrote:
    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?

    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:
    char c1 = 'A';
    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.

  • theSquidtheSquid Sydney, AustraliaRegistered User regular
    Just use the split() function in String - firstly using newline as a delimiter to get an array of lines, then on each line, using whitespace as a delimiter to get each word and the first number.

  • DrunkMcDrunkMc Registered User regular
    I'm personally a huge fan of BufferedReaders. So you can do:
    BufferedReader reader = new BufferedReader(new FileReader(new File("C:\cipher.txt"));
    String currLine;
    while((currLine=reader.readLine())!=null)
    {
        //Now you get every line by itself. 
        int cipherNum = Integer.valueOf(currLine.split(" "))[0]);
        //Now do what people up top told you to do.
        //Just use the modulus operator to handle when you go past 26, or use an if statement. if(ciperNum>26) cipherNum-=26;
    }
    


    Good luck!
    I use BufferedReaders constantly, getting it a line at a time makes my life infintely easier when doing stuff like this.

Sign In or Register to comment.