views:

75

answers:

2

This program I'm making isn't compiling right, I keep getting the error: Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: -9 at java.lang.String.charAt(String.java:687) at pro1.main(pro1.java:161)

Here's my code:

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);
  }
}
+3  A: 

Your code looks fishy in every way, and I cannot imagine anyone wanting to read 170 lines of this code.

Look at the exception: It tells you exactly what's going wrong: You pass -9 to charAt() as an index, which is - obviously - out of range, as you should only pass 0 ... (length-1) in there.

And it gives you the line number, too... so go to line number 161 and look what's in there and how it got there.

Eiko
+3  A: 

My guess would be it has something to do with this line:

z=(y-x)%26;

If x is larger than y the result of the % operation may be negative (or 0). And charAt (which is what z is given as parameter to) expects a positive value. You should try:

z=Math.abs(y-x)%26;

Edit: As a side note, this shouldn't be to hard to figure out on your own, by looking at the exception as was pointed out by others, or in the worst case using a debugger and seeing exactly what values the different variables have and why when the exception occurs.

Andrei Fierbinteanu
Another index out of bounds error waiting to happen is sbox, as it's only 26 characters long, yet it's being indexed randomly from 1 to 26. Valid indices are from 0 to 25, remember.
Gunslinger47
Yes, indeed, thanks for the input, I missed that. I guess I was only paying attention for probable negative indices. To OP: `range=generator.nextInt(26)+1;` should be `range=generator.nextInt(26);`
Andrei Fierbinteanu
It looks like he wanted the (y-x)%26 to be the reverse of (t+p)%26. Math.abs(..) wouldn't work that way. I typically use something like this for negative wrap-around: `z -= x; while (z < 0) z += 26;`
Gunslinger47