Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 字到符号转换器_Java_String - Fatal编程技术网

Java 字到符号转换器

Java 字到符号转换器,java,string,Java,String,我想建立一个系统,当写一个字母(例如:a)时,它会将其转换为一个符号(例如:ԅ) 我该怎么做呢?我知道这看起来很简单,但我对编程非常、非常陌生,我所能做的就是一个简单的日历或计算器 我试着一个人做,但没有成功。我输入了一个完整的单词,但它只打印第一个字母,而不是包含4+的单词。例如,我键入“光环”,但它只打印“ԅ” 非常感谢大家!!!我终于成功了使用string=string。替换(“a”,“ԅ”) 其中string是给定的单词(在你的例子中是aura)。我会使用一个for循环,每次读取一个字母

我想建立一个系统,当写一个字母(例如:a)时,它会将其转换为一个符号(例如:ԅ

我该怎么做呢?我知道这看起来很简单,但我对编程非常、非常陌生,我所能做的就是一个简单的日历或计算器

我试着一个人做,但没有成功。我输入了一个完整的单词,但它只打印第一个字母,而不是包含4+的单词。例如,我键入“光环”,但它只打印“ԅ


非常感谢大家!!!我终于成功了

使用
string=string。替换(“a”,“ԅ”)

其中string是给定的单词(在你的例子中是aura)。

我会使用一个for循环,每次读取一个字母提供的字符串,然后生成一个新字符串“翻译”每个字母

下面是一些快速代码:

Scanner i = new Scanner(System.in);
System.out.print("Enter string:");
String start = i.next();
int len = start.length;
String end = "";

for (int a = 0, a<len, a++) {
    if (start.Charat(a) == "a") {
        end = end.append(ԅ)
    } etc...
}
Scanner i=新的扫描仪(System.in);
System.out.print(“输入字符串:”);
字符串start=i.next();
int len=起始长度;
字符串结束=”;

对于(int a=0,a而言,最好的方法是使用翻译表对Translator类进行编码:

编辑:在同意AnthonyRaymond的建议后,我选择了地图选项,而不是
char[]
选项(我的第一个想法)

public class Translator
{
    private Map<Character,Character> table=createTable();

    public String translate(String input)
    {...}
}
公共类转换器
{
私有映射表=createTable();
公共字符串转换(字符串输入)
{...}
}
要构造表,必须仅设置显式映射(以便将表减小到最小大小):

private Map createTable()
{
Map table=newhashmap();
//设置显式映射:
表1.put('a'、'ԅ');
表1.put('b',…);
返回表;
}
翻译规则必须首先索引表格,如果找不到字符,则选择输入字符(以便保留空白、换行和其他不可翻译字符):

公共字符串翻译(字符串输入)
{
StringBuilder stb=新的StringBuilder(input.length());

对于(inti=0;iHi@EpicJavaNoob,欢迎访问StackOverflow

您可以通过对字符串使用
Map
replace()
函数来解决问题


您必须在程序中创建此类

public class MyReplacer {
    private static final Map<Character, Character> map = new HashMap<Character, Character>();
    static { // the static block is used to instantiate the Map once for all
        map.put('a' , '\u2026');
        map.put('b', '+');
    }

    public static String encode(String stringToEncode) {
        // for each entry in the hashmap, we replace the character by his corresponding key
        for (Map.Entry<Character, Character> entry : map.entrySet()) {
            stringToEncode = stringToEncode.replace(entry.getKey(), entry.getValue());
        }
        return stringToEncode;
    }
}


映射用于将键与值关联




我同意这不是你能找到的简单解决方案,但这是你学习编码的方式,看看别人写的代码,试着理解它是如何工作的,以及它为什么这样写。

在这种情况下,你可以使用
HashMap

如果你不知道那是什么,它基本上是一个“字典”。你可以在你的例子中这样做
HashMap

a -> ԅ
b -> !
c -> @
d -> #
注意:以上不是真正的代码,只是一种视觉表现!我不知道你的符号是什么意思以及如何键入它们,所以我使用了普通符号

看到了吗?这真是一本字典。当你找“a”时,你会得到“ԅ”,当你找“b”时,你会得到“!”等等。你现在明白了吗

好的,现在让我们看看代码

HashMap<Character, Character> characterMap = new HashMap<> (); //Create a new HashMap
characterMap.put ('a', 'ԅ'); // Put an entry in the "dictionary"
characterMap.put ('b', '!');
//and so on... I am lazy
这基本上是说,

Go to find every entry in the dictionary. 
For each entry,
    Store it in the "entry" variable
    Replace all the occurrences of entry's key by entry's value

这就是为什么要创建String.replace。我对java lol很陌生。我只是尽力提供帮助。不要使用
replaceAll
(这使用regex,在本例中不使用)。使用
replace
@MCMastery修复,谢谢;)我打赌
table['a']='ԅ'
会起作用,但向初学者展示这一点并不是最好的主意。这是一种来自地狱的实践^^@AnthonyRaymond也许你是对的,但这是我通常编写字符映射的方式:它既高效又简单……我想是的。你有什么建议吗?用
映射来代替
字符[]
?是的,我更愿意利用对象和基本类型。加上65536索引看起来像一个大数组^^^是的,你是对的:它更像是一个映射,而且尺寸更小。(顺便说一下:65K是
字符
索引的总寻址空间)。谢谢。你可能已经复制了我第一个答案的一段代码…我修复了3个命名不正确的方法调用…哎呀!我修复了。我编辑了它是为了提到你。@AnthonyRaymond非常感谢大家!!!为什么失败了?包括你的源代码和一些示例输入,以便我们可以看到发生了什么。
a -> ԅ
b -> !
c -> @
d -> #
HashMap<Character, Character> characterMap = new HashMap<> (); //Create a new HashMap
characterMap.put ('a', 'ԅ'); // Put an entry in the "dictionary"
characterMap.put ('b', '!');
//and so on... I am lazy
for (Map.Entry<Character, Character> entry : map.entrySet()) {
    input = input.replace(entry.getKey(), entry.getValue());
}
Go to find every entry in the dictionary. 
For each entry,
    Store it in the "entry" variable
    Replace all the occurrences of entry's key by entry's value