Why won't the program work? When I type '1' at the first question and then type in something at the second, a red text comes up: (The program is not done but it should work shouldn't it?)
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 0 at java.lang.String.charAt(String.java:658) at javaapplication2.JavaApplication2.main(JavaApplication2.java:37) Java Result: 1
Code:
package javaapplication2;
import java.util.Scanner;
/**
*
* @author John
*/
public class JavaApplication2 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int charAt = 0;
int[] karies = new int[99];
char[] ko = new char[99];
char[] firstchar = new char[99];
System.out.println("Type \"1\" for encryption and \"2\" for decryption:");
if (sc.nextInt() == 1) {
System.out.println("Enter the text you want to encrypt:");
} else {
System.out.println("Enter the text you want to decrypt:");
}
String krypt = sc.nextLine();
int longd = (sc.nextLine()).length();
while (charAt < longd) {
firstchar[charAt] = krypt.charAt(charAt);
karies[charAt] = ((int)firstchar[charAt]);
ko[charAt] = (char)(karies[charAt] - 7);
charAt++;
}
System.out.println(krypt.charAt(0));
}
}