I try to write a program allow you to write your cipher in option 1, then in option 2 write your message and in this part, I want the program to replace every litter with the litter in the cipher, but the program replace just the litter number 1 (index 0 in the String), then appear this problem >>
Exception in thread "main"
rjava.util.InputMismatchException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at cipher.ciipherABC.main(ciipherABC.java:179)
This is my code in Java
package cipher;
import java.util.Scanner;
import java.util.InputMismatchException;
public class ciipherABC {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner reader = new Scanner(System.in);
char[][] litters = {
{ 'a', 'أ', ' ' }, { 'b', 'ب', ' ' }, { 'c', 'ت', ' ' }, { 'd', 'ث', ' ' }, { 'e', 'ج', ' ' },
{ 'f', 'ح', ' ' }, { 'g', 'خ', ' ' }, { 'h', 'د', ' ' }, { 'i', 'ذ', ' ' }, { 'j', 'ر', ' ' },
{ 'k', 'ز', ' ' }, { 'l', 'س', ' ' }, { 'm', 'ش', ' ' }, { 'n', 'ص', ' ' }, { 'o', 'ض', ' ' },
{ 'p', 'ط', ' ' }, { 'q', 'ظ', ' ' }, { 'r', 'ع', ' ' }, { 's', 'غ', ' ' }, { 't', 'ف', ' ' },
{ 'u', 'ق', ' ' }, { 'v', 'ك', ' ' }, { 'w', 'ل', ' ' }, { 'x', 'م', ' ' }, { 'y', 'ن', ' ' },
{ 'z', 'ه', ' ' }, { ' ', 'و', ' ' }, { ' ', 'ي', ' ' }, };
int choice = reader.nextInt();
for (int count = 1; count > 0; count++) {
if (choice == 1) {
// System.out.println(litters.length);
String cipher = reader.next();
for (int i = 0; i < 28; i++) {
litters[i][2] = cipher.charAt(i);
for (int j = 0; j < 3; j++) {
System.out.print(litters[i][j] + "\t");
}
System.out.print("\n");
}
} else if (choice == 2) {
System.out.println(litters.length);
String phrase = reader.next();
System.out.println(litters.length);
String phraseToCipher = "";
for (int i = 0; i < phrase.length(); i++) {
System.out.println("..");
if (phrase.charAt(i) != ' ') {
System.out.println(".");
if (phrase.charAt(i) == 'A' || phrase.charAt(i) == 'a')
phraseToCipher += String.valueOf(litters[0][2]);
else if (phrase.charAt(i) == 'B' || phrase.charAt(i) == 'b')
phraseToCipher += String.valueOf(litters[1][2]);
else if (phrase.charAt(i) == 'C' || phrase.charAt(i) == 'c')
phraseToCipher += String.valueOf(litters[2][2]);
else if (phrase.charAt(i) == 'D' || phrase.charAt(i) == 'd')
phraseToCipher += String.valueOf(litters[3][2]);
else if (phrase.charAt(i) == 'E' || phrase.charAt(i) == 'e')
phraseToCipher += String.valueOf(litters[4][2]);
else if (phrase.charAt(i) == 'F' || phrase.charAt(i) == 'f')
phraseToCipher += String.valueOf(litters[5][2]);
else if (phrase.charAt(i) == 'G' || phrase.charAt(i) == 'g')
phraseToCipher += String.valueOf(litters[6][2]);
else if (phrase.charAt(i) == 'H' || phrase.charAt(i) == 'h')
phraseToCipher += String.valueOf(litters[7][2]);
else if (phrase.charAt(i) == 'I' || phrase.charAt(i) == 'i') {
// System.out.println(litters[8][2]);
phraseToCipher += String.valueOf(litters[8][2]);
System.out.println(phraseToCipher);
} else if (phrase.charAt(i) == 'J' || phrase.charAt(i) == 'j')
phraseToCipher += String.valueOf(litters[9][2]);
else if (phrase.charAt(i) == 'K' || phrase.charAt(i) == 'k')
phraseToCipher += String.valueOf(litters[10][2]);
else if (phrase.charAt(i) == 'L' || phrase.charAt(i) == 'l')
phraseToCipher += String.valueOf(litters[11][2]);
else if (phrase.charAt(i) == 'M' || phrase.charAt(i) == 'm')
phraseToCipher += String.valueOf(litters[12][2]);
else if (phrase.charAt(i) == 'N' || phrase.charAt(i) == 'n')
phraseToCipher += String.valueOf(litters[13][2]);
else if (phrase.charAt(i) == 'O' || phrase.charAt(i) == 'o')
phraseToCipher += String.valueOf(litters[14][2]);
else if (phrase.charAt(i) == 'P' || phrase.charAt(i) == 'p')
phraseToCipher += String.valueOf(litters[15][2]);
else if (phrase.charAt(i) == 'Q' || phrase.charAt(i) == 'q')
phraseToCipher += String.valueOf(litters[16][2]);
else if (phrase.charAt(i) == 'R' || phrase.charAt(i) == 'r')
phraseToCipher += String.valueOf(litters[17][2]);
else if (phrase.charAt(i) == 'S' || phrase.charAt(i) == 's')
phraseToCipher += String.valueOf(litters[18][2]);
else if (phrase.charAt(i) == 'T' || phrase.charAt(i) == 't')
phraseToCipher += String.valueOf(litters[19][2]);
else if (phrase.charAt(i) == 'U' || phrase.charAt(i) == 'u')
phraseToCipher += String.valueOf(litters[20][2]);
else if (phrase.charAt(i) == 'V' || phrase.charAt(i) == 'v')
phraseToCipher += String.valueOf(litters[21][2]);
else if (phrase.charAt(i) == 'W' || phrase.charAt(i) == 'w')
phraseToCipher += String.valueOf(litters[22][2]);
else if (phrase.charAt(i) == 'X' || phrase.charAt(i) == 'x')
phraseToCipher += String.valueOf(litters[23][2]);
else if (phrase.charAt(i) == 'Y' || phrase.charAt(i) == 'y')
phraseToCipher += String.valueOf(litters[24][2]);
else if (phrase.charAt(i) == 'Z' || phrase.charAt(i) == 'z')
phraseToCipher += String.valueOf(litters[25][2]);
else
phraseToCipher += String.valueOf(litters[26][2]);
} else
phraseToCipher += "$";
}
System.out.print(phraseToCipher);
} else if (choice == 3) {
} else {
System.out.print("\n*** Thank you for Visiting ***");
System.exit(0);
}
choice = reader.nextInt();
}
}
}