1

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();
        }
    }
}
Arvind Kumar Avinash
  • 50,121
  • 5
  • 26
  • 72

1 Answers1

0

Do it as follows:

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        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;
        do {
            System.out.print("Enter your choice [1, 2 or 3]: ");
            choice = Integer.parseInt(reader.nextLine());
            if (choice == 1) {
                System.out.print("Enter a text: ");
                String cipher = reader.nextLine();
                for (int i = 0; i < cipher.length(); 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("Enter a phrase: ");
                String phrase = reader.nextLine();
                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') {
                            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.println(phraseToCipher);
            } else if (choice == 3) {
                // ...
            } else {
                System.out.print("\n*** Thank you for Visiting ***");
                System.exit(0);
            }
        } while (choice > 0 && choice < 4);
    }
}

A sample run:

Enter your choice [1, 2 or 3]: 1
Enter a text: Hello
a   أ   H   
b   ب   e   
c   ت   l   
d   ث   l   
e   ج   o   
Enter your choice [1, 2 or 3]: 4

*** Thank you for Visiting ***

Check Scanner is skipping nextLine() after using next() or nextFoo()? for the solution to a similar problem.

Arvind Kumar Avinash
  • 50,121
  • 5
  • 26
  • 72