【问题标题】:StringIndexOutOfBoundsException in Java [closed]Java中的StringIndexOutOfBoundsException [关闭]
【发布时间】:2012-12-10 23:54:59
【问题描述】:

我正在使用 Eclipse。我需要制作一个程序来加密和解密用户输入的单词,但是当我选择加密时,我在线程“main”java.lang.StringIndexOutOfBoundsException中出现异常:字符串索引超出范围:0 在 java.lang.String.charAt(未知来源) 在 Encryption.encrypt(Encryption.java:43) 在 Encryption.main(Encryption.java:118)

这是我的代码:

import java.util.Scanner;

public class Encryption 
{
    public static String message = "";
    public static boolean hasMessage = false;
    public static boolean encrypted = false;
    static char a = 0; 
    static char b;
    static int w;
    static int x;
    static int y;
    static int z;



    public static void display()
    {
        System.out.println("Message: " + message + "\n");
    }

    public static void encrypt(String word)
    {
        if(!hasMessage)
        {
            System.out.println("No message");
            // Tell the user there is no message
        }
        else if(encrypted)
        {
            System.out.println("Message is already encrypted");
            // Tell the user the message is already encrypted
        }

        else
        {
            message = "";
            // Reset the message to blank


            for (message.length();;)
            {
                for (message.charAt(a);; a++) 
                {
                    int w = (int) a * 2;
                    int x = (int) w + 2;  
                    char y = (char) x; 
        }
            }

            //get char from each letter (increase char each time),  cast as int


        }
        System.out.println(message);
        encrypted = true;

        // Using the parameter word, modify message
        // to contain a new value following a predictable pattern
        // Hint:  alter each character's ASCII value by casting
        //        to an integer and using math operators

        // Display the new message
        // Set encrypted to true


    }

    public static void decrypt(String word)
    {
        if(!hasMessage)
        {
            System.out.println("No message");
            // Tell the user there is no message
        }
        else if(!encrypted)
        {
            System.out.println("Message not encrypted");
            // Tell the user the message is not encrypted

        }
        else
        {
            System.out.println(message);
            // Like encrypt, but in reverse
        }

    }

    public static void main(String[] args) 
    {  
        Scanner sc = new Scanner(System.in);
        int menuChoice = 0;

        while(menuChoice != 4)
        {
            System.out.println( "[1] Enter Word\n" + 
                    "[2] Encrypt\n" + 
                    "[3] Decrypt\n" + 
                    "[4] Quit\n");

            menuChoice = sc.nextInt();

            if(menuChoice == 1)
            {
                System.out.println("Input message");
                message = sc.next();
                // prompt user to input message
                // assign next word to the class-level variable message
                hasMessage = true;
                encrypted = false;
                // set hasMessage to true
                // set encrypted to false

            }
            else if(menuChoice == 2)
            {
                encrypt(message);
            }
            else if(menuChoice == 3)
            {
                decrypt(message);
            }
        }
    }
}

【问题讨论】:

  • 我建议你从一个非常基础的教程开始,一个很好的教程是 Oracle 的The Java Tutorials。我还建议从提供解决方案的练习开始。
  • 我同意 ignis。你还不了解循环。

标签: java eclipse exception encryption


【解决方案1】:

你还不太了解 for 循环。您没有包含条件表达式。 for(initialize; condition; increment) 是模式。您有 for (message.charAt(a);; a++) 缺少条件。缺少条件默认为 true,这意味着循环将永远继续。所以,a 不断增加直到charAt(a) 炸弹。

for(int i=0; i<10; i++)

例如,表示从 0 开始,在循环结束时将 i 加 1,当 i

【讨论】:

  • 啊,谢谢你的帮助^^(我几个月前才开始学习Java)
【解决方案2】:

您正在将消息重置为空白,然后尝试对其进行迭代

        message = "";
        // Reset the message to blank


        for (message.length();;)
        {

【讨论】:

    【解决方案3】:

    您正在访问空字符串的第一个字符。因此例外。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-08-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多