I made a project in java, where the user writes a sentence and when he presses enter, the letters he wrote are translated into symbols like: @#!& @&!. The problem is that those symbols are being printed the one under the other, that is a problem as the second project I made, translates the symbols back to letters, but It can't translate them as there is a blank between them. I searched for it on the internet and I found this article on StackOverflow: "https://stackoverflow.com/questions/37632058/how-to-convert-a-letter-to-an-another-letter-in-java" but I can't understand how it works. Is there a way to fix my own program? Help me please! The problem may be because I saved the characters that the user put in an Array, I am not sure though. The project is being run on NetBeans and the code of my project is this:
package kodikopoihsh2;
import java.util.Scanner;
/**
*
* @author user
*/
public class Kodikopoihsh2 {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
Scanner input = new Scanner(System.in);
System.out.println("write your text in order to be encrypted: ");
char[] letters = input.nextLine().toCharArray();
String a;
String b;
String c;
String d;
String e;
String f;
String g;
String h;
String i;
String j;
String k;
String l;
String m;
String n;
String o;
String p;
String q;
String r;
String s;
String t;
String u;
String v;
String w;
String x;
String y;
String z;
String aa;
String ab;
for (int counter = 0; counter < 99999; counter++){
if(letters[counter] == 'a'){
a = "!";
System.out.println(a);
}
if(letters[counter] == 'b'){
b = "@";
System.out.println(b);
}
if(letters[counter] == 'c'){
c = "#";
System.out.println(c);
}
if(letters[counter] == 'd'){
d = "$";
System.out.println(d);
}
if(letters[counter] == 'e'){
e = "%";
System.out.println(e);
}
if(letters[counter] == 'f'){
f = "^";
System.out.println(f);
}
if(letters[counter] == 'g'){
g = "&";
System.out.println(g);
}
if(letters[counter] == 'h'){
h = "*";
System.out.println(h);
}
if(letters[counter] == 'i'){
i = "(";
System.out.println(i);
}
if(letters[counter] == 'j'){
j = ")";
System.out.println(j);
}
if(letters[counter] == 'k'){
k = "-";
System.out.println(k);
}
if(letters[counter] == 'l'){
l = "_";
System.out.println(l);
}
if(letters[counter] == 'm'){
m = "=";
System.out.println(m);
}
if(letters[counter] == 'n'){
n = "+";
System.out.println(n);
}
if(letters[counter] == 'o'){
o = "`";
System.out.println(o);
}
if(letters[counter] == 'p'){
p = "~";
System.out.println(p);
}
if(letters[counter] == 'q'){
q = "1";
System.out.println(q);
}
if(letters[counter] == 'r'){
r = "2";
System.out.println(r);
}
if(letters[counter] == 's'){
s = "3";
System.out.println(s);
}
if(letters[counter] == 't'){
t = "4";
System.out.println(t);
}
if(letters[counter] == 'u'){
u = "5";
System.out.println(u);
}
if(letters[counter] == 'v'){
v = "6";
System.out.println(v);
}
if(letters[counter] == 'w'){
w = "7";
System.out.println(w);
}
if(letters[counter] == 'x'){
x = "8";
System.out.println(x);
}
if(letters[counter] == 'y'){
y = "9";
System.out.println(y);
}
if(letters[counter] == 'z'){
z = "0";
System.out.println(z);
}
if(letters[counter] == ' '){
aa = " ";
System.out.println(aa);
}
if(letters[counter] == '?'){
ab = "?";
System.out.println(ab);
}
}
}
}
I run the program, I wrote: "password encrypted" (without quotes) and here is the output with an error which I cannot understand what it means, help me please:
~
!
3
3
7
`
2
$
%
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 18
+
#
2
9
~
4
%
$