java - StringIndexOutOfBoundsException

标签 java string

我正在制作的这个程序编译不正确,我不断收到错误: 线程“main”中的异常 java.lang.StringIndexOutOfBoundsException:字符串索引超出范围:-9 在 java.lang.String.charAt(String.java:687) 在 pro1.main(pro1.java:161)

这是我的代码:

import java.io.*;
import java.util.*;
public class pro1 {
    static String str="";
    static String str1="";
    static int range=250;
    static int length;
    static String key="";
    static String ep="";
       static String pt="";
    static char temp;
    static int t,p,h;
    static int r,x,y,z,w;
    static Random generator = new Random();
 static public String getContents(File aFile) 
   {
   StringBuilder contents = new StringBuilder();
   try {
      BufferedReader input =  new BufferedReader(new FileReader(aFile));
      try {
        String line = null; 
        while (( line = input.readLine()) != null){
          contents.append(line);
          contents.append(System.getProperty("line.separator"));
        }
      }
      finally {
        input.close();
      }
    }
    catch (IOException ex){
      ex.printStackTrace();
    }
    str1=contents.toString();
    return str1;
    }



public static void main (String args[]) throws IOException {
      File testFile = new File("/home/amritha/Desktop/sam.txt");
      System.out.println("Original file contents: " + getContents(testFile));
      System.out.println("message:"+str1);
       String sbox="abcdefghijklmnopqrstuvwxyz";
       length=str1.length()-1;
        for(int i=0;i<length;i++)
        {
           t=(int)str1.charAt(i);
             if(t==32)
              {
               int t1=32;
               temp=(char)t;
              }
            else
              {
               range=generator.nextInt(26)+1;
               temp=sbox.charAt(range);
              }
              key+=""+temp;
        }
       System.out.println("Key:"+key);
       for(int i=0;i<length;i++)
       {
         t=(int)str1.charAt(i);
          {
            if(t==32)
             {
               t=32;
               temp=(char)t;
             }
            else
             {
               t-=97;
             }
          }
        p=(int)key.charAt(i);
          {
           if(p==32)
            {
               p=32;
               temp=(char)p;
            }
           else
            {
              p-=97;
            }
          }
        if((t==32)&&(p==32))
           {
                int v=32;
                temp=(char)v;
           }
           else
           {
            r=(t+p)%26;
           temp=sbox.charAt(r);
           }
           ep+=""+temp;
       }
         System.out.println("Encrypted Text:"+ep);

   for(int i=0;i<length;i++)
       {
         y=(int)ep.charAt(i);
          {
            if(y==32)
             {
               y=32;
               temp=(char)y;
             }
            else
             {
               y-=97;
             }
          }
        x=(int)key.charAt(i);
          {
           if(x==32)
            {
               x=32;
               temp=(char)x;
            }
           else
            {
              x-=97;
            }
          }
        if((x==32)&&(y==32))
           {
                int w=32;
                temp=(char)w;
           }
           else
           {
            z=(y-x)%26;
           temp=sbox.charAt(z);
           }
           pt+=""+temp;
       }
         System.out.println("deccrypted Text:"+pt);
  }
}

最佳答案

你的代码在各个方面看起来都很可疑,我无法想象有人想要阅读这段代码的 170 行。

看看异常:它准确地告诉你出了什么问题:你将 -9 作为索引传递给 charAt() ,这显然超出了范围,因为你应该只传递 0 ... (length-1 )在那里。

它也会给你行号...所以转到第 161 行,看看里面有什么以及它是如何到达那里的。

关于java - StringIndexOutOfBoundsException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3079163/

相关文章:

java - Activity onCreate 上出现 NullPointerException

python拆分字符串但保留分隔符

c - get_string() 返回什么?

Java 8 : Converting an array of strings to have first character in uppercase and sorted order

c++ - 将字符串打印到文件,为什么需要额外的位?

c# - 在测试 1、测试 10、测试 2 中对包含数字的字符串进行排序的好方法

java - 如何在 java 中分离最后和剩余的所有单词?

java - 泛型:通配符类型的编译错误

java - 在 android 中实现 Parcelable 时出错

java - 尝试创建用户定义对象的列表,获取对象列表