Computer Science Canada

Help with conversion program

Author:  Azzy [ Sat Mar 26, 2005 12:41 pm ]
Post subject:  Help with conversion program

I'm making a string conversion program in order to make one sentance to be converted into a code. But i'm having some problems and could use some help.

code:
import java.io.*;
import java.util.*;

public class codeconvert
{              
        public static void main(String args[])throws Exception
        {
                DataInput keyboard = new DataInputStream(System.in);
                String temp;
                char response;
               
                String toConvert;
                String converted;
                                               
                System.out.println("Input the sentance you want converted.");
                toConvert = keyboard.readLine();
               
                converted = toConvert;
                converted.replace("A" , "B");
                converted.replace("a" , "b");
                converted.replace("B" , "C");
                converted.replace("b" , "c");
                converted.replace("C" , "D");
                converted.replace("c" , "d");
                converted.replace("D" , "E");
                converted.replace("d" , "e");
                converted.replace("E" , "F");
                converted.replace("e" , "f");
                converted.replace("F" , "G");
                converted.replace("f" , "g");
                converted.replace("G" , "H");
                converted.replace("g" , "h");
                converted.replace("H" , "I");
                converted.replace("h" , "i");
                converted.replace("I" , "J");
                converted.replace("i" , "j");
                converted.replace("J" , "K");
                converted.replace("j" , "k");
                converted.replace("K" , "L");
                converted.replace("k" , "l");
                converted.replace("L" , "M");
                converted.replace("l" , "m");
                converted.replace("M" , "N");
                converted.replace("m" , "n");
                converted.replace("N" , "O");
                converted.replace("n" , "o");
                converted.replace("O" , "P");
                converted.replace("o" , "p");
                converted.replace("P" , "Q");
                converted.replace("p" , "q");
                converted.replace("Q" , "R");
                converted.replace("q" , "r");
                converted.replace("R" , "S");
                converted.replace("r" , "s");
                converted.replace("S" , "T");
                converted.replace("s" , "t");
                converted.replace("T" , "U");
                converted.replace("t" , "u");
                converted.replace("U" , "V");
                converted.replace("u" , "v");
                converted.replace("V" , "W");
                converted.replace("v" , "w");
                converted.replace("W" , "X");
                converted.replace("w" , "x");
                converted.replace("X" , "Y");
                converted.replace("x" , "y");
                converted.replace("Y" , "Z");
                converted.replace("y" , "z");
                converted.replace("Z" , "A");
                converted.replace("z" , "a");
                               
                System.out.println("\n\nThe sentance: ");
                System.out.println("\t\'" + toConvert + "\'");
                System.out.println("converted is: ");
                System.out.println("\t\'" + converted + "\'");
        }
}


The problem I'm having is that it would replace the letters with the converted one.

Author:  wtd [ Sat Mar 26, 2005 1:10 pm ]
Post subject: 

The replace method is not a destructive method. It doesn't change the string it's called on, but rather returns a new string with those changes made.

The String doc.

Also, there is a difference between:

code:
'a'


Abd:

code:
"a"


The first is a character, and the second is a string with only one character. Java knows the difference, and cares about it.

Your third problem is that you want to do several replaces in a row. So if you change all of the As to Bs, then your next step is to change all of the Bs to Cs, then you've just changed both As and Bs to Cs. You'll need to process the string character by character to avoid this.


: