java - 制作加密程序,我收到语法错误,但我不知道为什么

标签 java encryption

目前我正在尝试制作一个基本程序,它接受任何字符串(例如句子或段落)的输入,并且一旦我开始工作,它就会接受每个字母并将其转换为 3 个字符的代码,我假设我应该能够做相反的事情,让它获取 3 位代码并将其转回文本,无论如何,当我尝试编译程序来测试它时,我遇到了错误。我在下面标记了这个问题。另外,一旦我让程序正常工作,我想为它制作一个图形用户界面,您可以在其中输入输入,然后在单击按钮后显示输出,但由于我才刚刚开始,如果您知道的话,这对我来说似乎很先进有什么好的教程请告诉我!

import java.util.Scanner;
import java.util.*;
class test {
private static Scanner inp;
public static void main(String[] args) {
    Map <Character, String> encryptionMappings = new HashMap<>();
    encryptionMappings.put('a',"{qaz}");
    encryptionMappings.put('b',"{wsx}");
    encryptionMappings.put('c',"{edc}");
    encryptionMappings.put('d',"{rfv}");
    encryptionMappings.put('e',"{tgb}");
    encryptionMappings.put('f',"{yhn}");
    encryptionMappings.put('g',"{ujm}");
    encryptionMappings.put('h',"{ik,}");
    encryptionMappings.put('i',"{ol>}");
    encryptionMappings.put('j',"{p;?}");
    encryptionMappings.put('k',"{[']}");
    encryptionMappings.put('l',"{qwe}");
    encryptionMappings.put('m',"{asd}");
    encryptionMappings.put('n',"{zxc}");
    encryptionMappings.put('o',"{rty}");
    encryptionMappings.put('p',"{fgh}");
    encryptionMappings.put('q',"{vbn}");
    encryptionMappings.put('r',"{yui}");
    encryptionMappings.put('s',"{hjk}");
    encryptionMappings.put('t',"{nm,}");
    encryptionMappings.put('u',"{iop}");
    encryptionMappings.put('v',"{qaw}");
    encryptionMappings.put('w',"{sxz}");
    encryptionMappings.put('x',"{red}");
    encryptionMappings.put('y',"{cvf}");
    encryptionMappings.put('z',"{ytg}");
    encryptionMappings.put('A',"{hnb}");
    encryptionMappings.put('B',"{iuj}");
    encryptionMappings.put('C',"{kml}");
    encryptionMappings.put('D',"{opl}");
    encryptionMappings.put('E',"{wom}");
    encryptionMappings.put('F',"{wsv}");
    encryptionMappings.put('G',"{ths}");
    encryptionMappings.put('H',"{imv}");
    encryptionMappings.put('I',"{ybf}");
    encryptionMappings.put('J',"{cja}");
    encryptionMappings.put('K',"{thw}");
    encryptionMappings.put('L',"{maz}");
    encryptionMappings.put('M',"{pqa}");
    encryptionMappings.put('N',"{zwl}");
    encryptionMappings.put('O',"{;ld}");
    encryptionMappings.put('P',"{'d;}");
    encryptionMappings.put('Q',"{;ny}");
    encryptionMappings.put('R',"{;ws}");
    encryptionMappings.put('S',"{c/.}");
    encryptionMappings.put('T',"{%@^}");
    encryptionMappings.put('U',"{/mc}");
    encryptionMappings.put('V',"{uka}");
    encryptionMappings.put('W',"{zby}");
    encryptionMappings.put('X',"{&hd}");
    encryptionMappings.put('Y',"{&hw}");
    encryptionMappings.put('Z',"{^#^}");
    encryptionMappings.put('0',"{$g%}");
    encryptionMappings.put('1',"{^@%}");
    encryptionMappings.put('2',"{142}");
    encryptionMappings.put('3',"{243}");
    encryptionMappings.put('4',"{089}");
    encryptionMappings.put('5',"{756}");
    encryptionMappings.put('6',"{423}");
    encryptionMappings.put('7',"{312}");
    encryptionMappings.put('8',"{145}");
    encryptionMappings.put('9',"{187}");
    encryptionMappings.put('~',"{)*(}");
    encryptionMappings.put('`',"{$#%}");
    encryptionMappings.put('!',"{!^#}");
    encryptionMappings.put('@',"{@^&}");
    encryptionMappings.put('#',"{^@&}");
    encryptionMappings.put('$',"{!?*}");
    encryptionMappings.put('%',"{^<+}");
    encryptionMappings.put('^',"{+$$}");
    encryptionMappings.put('&',"{!!*}");
    encryptionMappings.put('*',"{((%}");
    encryptionMappings.put('(',"{*&^}");
    encryptionMappings.put(')',"{$%^}");
    encryptionMappings.put('_',"{&#^}");
    encryptionMappings.put('-',"{<>?}");
    encryptionMappings.put('=',"{:'^}");
    encryptionMappings.put('{',"{%%G}");
    encryptionMappings.put('}',"{$$$}");
    encryptionMappings.put('[',"{***}");
    encryptionMappings.put(']',"{:::}");
    encryptionMappings.put(':',"{#$%}");
    encryptionMappings.put('|',"{?H*}");
    encryptionMappings.put(';',"{B&&}");
    encryptionMappings.put('"',"{@gs}");
    encryptionMappings.put('?',"{^gl}");
    encryptionMappings.put('/',"{@gn}");
    encryptionMappings.put('<',"{%TG}");
    encryptionMappings.put('>',"{5%5}");
    encryptionMappings.put(',',"{yty}");
    encryptionMappings.put('.',"{ggg}");

    inp = new Scanner(System.in);
    System.out.println("Input Password");
    int n = inp.nextInt();

    if(n!=234) {
        System.out.println("Denied Acess");
    } else { 
        System.out.print("Password Accepted"
                + "               ");
    System.out.print("Input Text to encrypt: ");

    String m = inp.next();
    String encryptMe = "He";
    StringBuilder builder = new StringBuilder();

下面的行显示了“toCharArray”的语法错误,我不知道为什么,我刚刚开始学习java,所以如果它是我错过的简单内容,我很抱歉,所有帮助都是赞赏。

    for (Character c : encryptMe.toCharArray) {
      builder.append(encryptionMappings.get(c));
    }
    String encrypted = builder.toString();
    }
}

最佳答案

您正在尝试调用对象上的方法,但缺少需要遵循 toCharArray 的空 ()。有些语言允许省略空括号,但 Java 不是其中之一。您应该使用:

for (Character c : encryptMe.toCharArray()) {
  builder.append(encryptionMappings.get(c));
}

一个好的 IDE(Eclipse、Intellij IDEA、Netbeans 等)将帮助您在学习时发现这些语法错误。

关于java - 制作加密程序,我收到语法错误,但我不知道为什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41477243/

相关文章:

java - 为什么列表不会为空?

java - 变量 "context"可能尚未初始化,java JNDI util

java - jitpack 将构建工件下载到哪里?

android - DexGuard:如何指定字符串加密

sql-server - SQL Server CLR 内存分配

java - Android - 使用 GSON 解析 JSON

java - 具有字符串限制的 WSDL 生成

java - 如何为java中的Zip文件夹提供密码保护?

java - Bouncy CaSTLe 与带有 OAEP 的 Java 默认 RSA

java - 从 android 相机即时加密视频