我如何修复语法错误插入'}'来完成块

时间:2019-03-25 12:40:25

标签: java neo4j user-defined-functions

我的代码不断出现错误。我正在为Neo4j创建一个简单的用户定义函数。谁能帮我这个?不管我尝试什么,我都会得到

Syntax Error: Insert "}" to complete block.

当我插入}时,它给我一个错误,指出我的代码“不可访问”,当我添加一个括号使其可访问时,它使我回到第一个错误并开始循环。

这是我的代码:

public class Join {


static Cipher cipher;

@UserFunction
@Description("example.DES ,, Decryption of any input values.")

public byte[] DES( @Name("set1") List<String> strings1) {

    for (int i = 0; i < strings1.size(); i++) {
        String dot;
        dot = strings1.get(i);

        KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
        keyGenerator.init(128);
        SecretKey secretKey = keyGenerator.generateKey();
        cipher = Cipher.getInstance("AES");


        String encryptedText = encrypt(dot, secretKey);
        System.out.println("Encrypted Text After Encryption: " + encryptedText);

    }


    public static String encrypt(String dot, SecretKey secretKey)
            throws Exception {
        byte[] plainTextByte = dot.getBytes();
        cipher.init(Cipher.ENCRYPT_MODE, secretKey);
        byte[] encryptedByte = cipher.doFinal(plainTextByte);
        Base64.Encoder encoder = Base64.getEncoder();
        String encryptedText = encoder.encodeToString(encryptedByte);
        return encryptedText;
    } 

    }

4 个答案:

答案 0 :(得分:2)

在开始使用}方法之前,即for循环结束之后,您需要放置一个public static String encrypt(String dot, SecretKey secretKey)

答案 1 :(得分:0)

您缺少结束符,您的代码无法运行,如果将其添加到错误的位置,则只会导致代码中出现更多问题。我建议按照以下方式使用一些东西:

编辑:根据我的猜测,我从未使用过Cipher,如果添加右括号不起作用,则很可能是代码本身存在错误,建议您仔细检查一下并检查是否有错误。

public class Join {


static Cipher cipher;

@UserFunction
@Description("example.DES ,, Decryption of any input values.")

public byte[] DES( @Name("set1") List<String> strings1) {

    for (int i = 0; i < strings1.size(); i++) {
        String dot;
        dot = strings1.get(i);

        KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
        keyGenerator.init(128);
        SecretKey secretKey = keyGenerator.generateKey();
        cipher = Cipher.getInstance("AES");


        String encryptedText = encrypt(dot, secretKey);
        System.out.println("Encrypted Text After Encryption: " + encryptedText);

    }


    public static String encrypt(String dot, SecretKey secretKey)
            throws Exception {
        byte[] plainTextByte = dot.getBytes();
        cipher.init(Cipher.ENCRYPT_MODE, secretKey);
        byte[] encryptedByte = cipher.doFinal(plainTextByte);
        Base64.Encoder encoder = Base64.getEncoder();
        String encryptedText = encoder.encodeToString(encryptedByte);
        return encryptedText;
      } 

    }

  }

答案 2 :(得分:0)

我可以看到缺少一个}来关闭类Join。始终尝试格式化代码以查看此问题。结果代码为:

public class Join {

static Cipher cipher;

@UserFunction
@Description("example.DES ,, Decryption of any input values.")

 public byte[] DES( @Name("set1") List<String> strings1) {

    for (int i = 0; i < strings1.size(); i++) {
        String dot;
        dot = strings1.get(i);

        KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
        keyGenerator.init(128);
        SecretKey secretKey = keyGenerator.generateKey();
        cipher = Cipher.getInstance("AES");


        String encryptedText = encrypt(dot, secretKey);
        System.out.println("Encrypted Text After Encryption: " + encryptedText);

    }

}

    public static String encrypt(String dot, SecretKey secretKey) throws Exception {
        byte[] plainTextByte = dot.getBytes();
        cipher.init(Cipher.ENCRYPT_MODE, secretKey);
        byte[] encryptedByte = cipher.doFinal(plainTextByte);
        Base64.Encoder encoder = Base64.getEncoder();
        String encryptedText = encoder.encodeToString(encryptedByte);
        return encryptedText;
    } 

   }



答案 3 :(得分:0)

您必须在加密方法之前放置一个'{',以结束forloop。

 public class Join {
        static Cipher cipher;

        @UserFunction
        @Description("example.DES ,, Decryption of any input values.")

        public byte[] DES( @Name("set1") List<String> strings1) {

            for (int i = 0; i < strings1.size(); i++) {
                String dot;
                dot = strings1.get(i);

                KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
                keyGenerator.init(128);
                SecretKey secretKey = keyGenerator.generateKey();
                cipher = Cipher.getInstance("AES");


                String encryptedText = encrypt(dot, secretKey);
                System.out.println("Encrypted Text After Encryption: " + encryptedText);

            }
    }

            public static String encrypt(String dot, SecretKey secretKey)
                    throws Exception {
                byte[] plainTextByte = dot.getBytes();
                cipher.init(Cipher.ENCRYPT_MODE, secretKey);
                byte[] encryptedByte = cipher.doFinal(plainTextByte);
                Base64.Encoder encoder = Base64.getEncoder();
                String encryptedText = encoder.encodeToString(encryptedByte);
                return encryptedText;
            } 


        }