Java 无法在indexOf之后将字符串传递给方法

Java 无法在indexOf之后将字符串传递给方法,java,Java,我几乎完成了一个程序,它接收用户输入并加密消息,然后再显示回来。由于某些原因,我无法将字符串传递给我的加密方法并返回它。有人知道我哪里出错了吗 非常感谢所有回应的人 public static String doEncryption(String s) { char alphabet[] = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r'

我几乎完成了一个程序,它接收用户输入并加密消息,然后再显示回来。由于某些原因,我无法将字符串传递给我的加密方法并返回它。有人知道我哪里出错了吗

非常感谢所有回应的人

public static String doEncryption(String s)
{
    char alphabet[]  = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i',
        'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
        'w', 'x', 'y', 'z' };
    char key[] = { 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
        'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w',
        'x', 'y', 'z', 'a' };

    char encoded[] = new char[(s.length())];
    String encrypted="";
    int j=0;
    for(int i=0; i<s.length(); i++){
        //System.out.println(s.charAt(i));
        boolean isFound = false;
        j = 0;
        while (j < alphabet.length && !isFound){


            if (alphabet[j]==s.charAt(i)){
               encrypted=encrypted+key[j];
               isFound=true;
            }
            j++; 
        }

        if(j>=alphabet.length){
            encrypted=encrypted+s.charAt(i);
        }
       }        
    return (new String(encrypted));
}


public static void main(String args[])
{
    String match = "QUIT";
    String en = "";
    System.out.println("Welcome to the Encoding Program!");
    Scanner sc = new Scanner(System.in);
    System.out.println("Enter the text you want encoded below.  Type QUIT when finished. ");
    en = sc.nextLine();
    String trim = en.substring(0, en.indexOf("QUIT"));
    doEncryption(trim.toLowerCase());
    //String en = doEncryption(sc.nextLine().toLowerCase());
    System.out.println("The text is encoded as: " + trim.toUpperCase());
    sc.close();
}
公共静态字符串doEncryption(字符串s)
{
字符字母表[]={'a','b','c','d','e','f','g','h','i',
‘j’、‘k’、‘l’、‘m’、‘n’、‘o’、‘p’、‘q’、‘r’、‘s’、‘t’、‘u’、‘v’,
‘w’、‘x’、‘y’、‘z’};
字符键[]={b',c',d',e',f',g',h',i',j',
‘k’、‘l’、‘m’、‘n’、‘o’、‘p’、‘q’、‘r’、‘s’、‘t’、‘u’、‘v’、‘w’,
'x','y','z','a'};
字符编码[]=新字符[(s.length())];
字符串加密=”;
int j=0;
for(int i=0;i=alphabet.length){
加密=加密+s.charAt(i);
}
}        
返回(新字符串(加密));
}
公共静态void main(字符串参数[])
{
字符串匹配=“退出”;
字符串en=“”;
System.out.println(“欢迎使用编码程序!”);
扫描仪sc=新的扫描仪(System.in);
System.out.println(“在下面输入要编码的文本。完成后键入QUIT”);
en=sc.nextLine();
String trim=en.substring(0,en.indexOf(“QUIT”);
doEncryption(trim.toLowerCase());
//字符串en=doEncryption(sc.nextLine().toLowerCase());
System.out.println(“文本编码为:“+trim.toUpperCase()”);
sc.close();
}
}

您的方法返回更新后的字符串,因此在调用它时,您需要使用该返回值。更改:

doEncryption(trim.toLowerCase());
doEncryption(trim.toLowerCase());

或者重复使用
trim
,如果您愿意:

trim = doEncryption(trim.toLowerCase());
…然后使用
updatedValue
trim
显示结果。

您的方法返回更新后的字符串,因此您希望在调用它时使用该返回值。更改:

doEncryption(trim.toLowerCase());
doEncryption(trim.toLowerCase());

或者重复使用
trim
,如果您愿意:

trim = doEncryption(trim.toLowerCase());

…然后使用
updatedValue
trim
显示结果。

只是因为您从未重新分配值

改变

然后
System.out.println(“文本编码为:”+trim.toUpperCase())将显示正确的值

当心这条线

en.substring(0, en.indexOf("QUIT"));
如果字符串中没有“QUIT”,它将抛出一个

我认为您希望程序在输入“QUIT”之前一直执行,因此您需要循环直到输入为“QUIT”。您的主要方法如下所示

String match = "QUIT";
String en = "";
System.out.println("Welcome to the Encoding Program!");
Scanner sc = new Scanner(System.in);
System.out.println("Enter the text you want encoded below.  Type QUIT when finished. ");
do
{
    en = sc.nextLine();

    if(!en.toUpperCase().equals(match))
    {
        en = doEncryption(en.toLowerCase());

        System.out.println("The text is encoded as: " + en.toUpperCase());
    }
} while(!match.equals(en.toUpperCase()));
sc.close();

当您想退出时,只需输入quit。

,因为您从未重新分配值

改变

然后
System.out.println(“文本编码为:”+trim.toUpperCase())将显示正确的值

当心这条线

en.substring(0, en.indexOf("QUIT"));
如果字符串中没有“QUIT”,它将抛出一个

我认为您希望程序在输入“QUIT”之前一直执行,因此您需要循环直到输入为“QUIT”。您的主要方法如下所示

String match = "QUIT";
String en = "";
System.out.println("Welcome to the Encoding Program!");
Scanner sc = new Scanner(System.in);
System.out.println("Enter the text you want encoded below.  Type QUIT when finished. ");
do
{
    en = sc.nextLine();

    if(!en.toUpperCase().equals(match))
    {
        en = doEncryption(en.toLowerCase());

        System.out.println("The text is encoded as: " + en.toUpperCase());
    }
} while(!match.equals(en.toUpperCase()));
sc.close();


当您想退出时,只需输入quit。

出于某种原因。
?你说你不能是什么意思?你有错误吗?什么都没发生吗?请更具体一点,您必须将结果分配给其他变量。
出于某种原因
?你说你不能是什么意思?你有错误吗?什么都没发生吗?请更具体一点,你必须把结果分配给其他变量。啊,这样做了,谢谢!现在一切都正常了,我正在运行测试用例,我的第一个测试用例出现了一个错误!如果我输入say“ABCDEFGHIJKLMNOPQRSTUVWXYZ”,我希望encrpytion创建“BCDEFGH…”,我会得到一个错误:“线程“main”java.lang.StringIndexOutOfBoundsException:字符串索引超出范围:-1在java.lang.String.substring(String.java:1954)在cipher2.main(cipher2.java:46)处这是什么原因?谢谢!@still2blue看到我的答案了,我阻止你这么做。这是因为你输入了一个不包含“QUIT”的字符串“所以返回索引-1不是
子字符串的有效边界。啊,这样做了,谢谢!现在一切都正常了,我正在运行测试用例,我的第一个测试用例出现了一个错误!如果我输入say“ABCDEFGHIJKLMNOPQRSTUVWXYZ”,我希望encrpytion创建“BCDEFGH…”,我会得到一个错误:“线程“main”java.lang.StringIndexOutOfBoundsException:字符串索引超出范围:-1在java.lang.String.substring(String.java:1954)在cipher2.main(cipher2.java:46)处这是什么原因?谢谢!@still2blue看到我的答案了,我阻止你这么做。这是因为你输入了一个不包含“QUIT”的字符串“所以返回索引-1不是
子字符串的有效边界。那么我用什么来删除它呢?”?我做了您建议的更改,但由于子字符串行的原因仍然出现错误。我发现了,我忘记了退出,但是如果我键入“ABCDEFGHIJKLMNOPQRSTUVWXYZQUIT”,它无法识别退出并对其进行加密,我如何更改它?验证
if(string.contains(“QUIT”))
除了一件事:欢迎使用编码程序外,一切正常!在下面输入要编码的文本。完成后键入QUIT。ABCDEFGHIJKLMNOPQRSTUVWXYZ退出退出文本编码为:bcdefghijklmnopqrstuvwxyz RVJU为什么在加密消息的末尾添加一个额外的Z?明白了!这是我循环中的一个条件,那么我用什么来删除退出呢?我做了您建议的更改,但由于子字符串行的原因仍然出现错误。我发现了,我忘记了退出,但是如果我键入“ABCDEFGHIJKLMNOPQRSTUVWXYZQUIT”,它无法识别退出并对其进行加密,我如何更改它?验证
if(string.contains(“QUIT”))
除了一件事:欢迎使用编码程序外,一切正常!在下面输入要编码的文本。完成后键入QUIT。退出退出退出文本编码为:bcdefghjjjklmnopqrstuvwxyz RVJU为什么在e处添加额外的Z