Question

I am trying to make an app with java where you input a sentence, and the app changes the letters to other specified letters. What I need to know is how to do a "text input" and how to change the letters. Currently, I am not getting any errors, but all I get back is "testing." It makes sense, but how do I fix it? Here is what I have so far:

public class baseCoder {

    public static void main(String[] args) {
String t1 = "testing";      
String c = "a";
String f = "b";
String h = "c";
String j = "d";
String s = "e";
String q = "f";
String r = "g";
String u = "h";
String l = "i";
String e = "j";
String w = "k";
String m = "l";
String t = "m";
String i = "n";
String p = "o";
String o = "p";
String b = "q";
String v = "r";
String x = "s";
String a = "t";
String k = "u";
String n = "v";
String y = "w";
String g = "x";
String z = "y";
String d = "s";


    System.out.println("" + t1);
}
}
Was it helpful?

Solution

First you would need to create a Map of all the letters:

Hashmap<String, String> map = new Hashmap<String, String>();
map.put("a", "c");
map.put("b", "f");
...

To get the translation of each letter you simply get the value from the map:

String translatedLetter = map.get(letter);

So now you would need to create a loop to translate the whole word one letter at a time. I would use a StringBuilder to keep track of each translated letter.

OTHER TIPS

using HashMap would right option only when you wanted to put only the specific letters replacingt the orignal one.. However, there are other procedure like using random number between 65 and 91 and replacing the corresponding value... if your requirement is just to geneate another string...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top