Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

String Index Out of Range...

802437Oct 1 2010 — edited Oct 1 2010
For a programming assignment, I have to encrypt a message entered from the console. What is giving most trouble is writing the method with which to encrypt a message which the user would input into the console. Currently, my encryption method is written like this:
import java.lang.*;

public class EncrypterDecrypter 
{
    public static final String ORDERED = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; 
    private String key;

    public EncrypterDecrypter(String theKey) 
    { 
        key = "";
    }
 
    public String encrypt(String clearText) 
    { 
        String encryptedClearText = "";
        
        for (int i = 0; i <= clearText.length(); i++)
        {
            int length = clearText.length();
            int index = ORDERED.indexOf(length);
            encryptedClearText += key.charAt(index);
        }
        
        return encryptedClearText;
        
    }
}
I always get a String Index Out of Range -1 error message when I try to run this via my executing class. I've tried many different variations of code, yet nothing has worked. Sometimes, if I change the "index" in the "charAt" method to "length", it would return me the user-inputted string.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Oct 29 2010
Added on Oct 1 2010
8 comments
2,360 views