Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

shift cipher decoder

807601May 3 2008 — edited May 3 2008
I am trying to make a shift cipher decoder.
It should read in a encoded sentence, pick one word (I decided to go with the first word for now), and execute the shift process, and output all the combinations, then check against a dictionary to find all the valid words.

But for now, I am only working on the 1st case of the the algorithm, a to b, b to c etc. The other cases I plan to implement with a for loop. but that is neither here nor now. I am also ignoring the dictionary right now.

What I have is below:
import java.util.*; 
import java.lang.*;
 
                         
     
/**
 * Write a description of class ShiftCypher here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class ShiftCypher
{
    // instance variables - replace the example below with your own
    private String s;
    //private ArrayList< Character > sentence;
    //private char letters;

    /**
     * Constructor for objects of class ShiftCypher
     */
    public ShiftCypher(String input)
    { 
        s=input;
    }

    /**
     * split the string into each characters
     * 
     * @param  string s
     * @return     NA
     */
    public void splitString ()
    {
         //sentence = new ArrayList<Character>();
         String [] temp = null;
         temp=s.split(" ");
         //return s.toCharArray();
         dump(temp);
         shiftMachine(temp);
    }
    
    /**
     * dump the temp string
     */
    private void dump (String [] args) {
        System.out.println("-----------------------");
        for (int i=0; i<args.length; i++) {
            System.out.println(args);
}
System.out.println("-----------------------");
}

/**
* gets the first word for shifting
* testing method to make sure it works
*

private void getFirstWord (String [] args) {

char letters [] = args[0].toCharArray();
for (int i=0; i<letters.length; i++) {
System.out.println(letters[i]);
}

}
*
*/

private void shiftMachine (String [] args) {
int i;
char letters [] = args[0].toCharArray();

for (i=0; i<letters.length; i++){
letters[i]=letters[i]+1;
}

}

}
 
The idea is to shift the letters up by one, so a to b, b to c. I am assuming all lower cases. and all input will be chars. 
My problem is with the shiftMachine method. The line where it says 

letters=letters[i]+1

gives an error saying possible loss of precision. how do I fix that?

Thank you for your help
Davy

Edited by: Davy_Zou on 3-May-2008 6:28 AM
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on May 31 2008
Added on May 3 2008
7 comments
230 views