Skip to content
Advertisement

Converting a scanner.next() to a char[]

I want to give a function a char array from scanner.next(). I only get an error message if I try to convert it like I do it now:

import java.util.Scanner;

public class Enigma {
    private static boolean finish = false;
    private static int temp = 0;
    private static int[] rotors = { 1, 2, 3, 4, 5 };
    private static int[] used = { 6, 6, 6 };
    private static int[] rotations = { 0, 0, 0 };
    private static char[] rotor1 = { 'C', 'N', 'D', 'L', 'H', 'Q', 'B', 'O', 'Y', 'G', 'R', 'T', 'U', 'E', 'M', 'V',
            'S', 'Z', 'P', 'W', 'F', 'I', 'A', 'X', 'K', 'J' };
    private static char[] rotor2 = { 'H', 'P', 'N', 'Y', 'X', 'B', 'U', 'E', 'R', 'Q', 'L', 'T', 'S', 'D', 'Z', 'W',
            'I', 'K', 'C', 'J', 'O', 'F', 'M', 'A', 'V', 'G' };
    private static char[] rotor3 = { 'J', 'I', 'H', 'Y', 'Z', 'P', 'E', 'U', 'S', 'L', 'G', 'V', 'K', 'D', 'A', 'B',
            'X', 'O', 'T', 'M', 'N', 'F', 'W', 'Q', 'R', 'C' };
    private static char[] rotor4 = { 'M', 'K', 'U', 'Z', 'D', 'B', 'G', 'R', 'Y', 'F', 'X', 'Q', 'H', 'C', 'A', 'N',
            'E', 'L', 'T', 'J', 'I', 'P', 'S', 'V', 'O', 'W' };
    private static char[] rotor5 = { 'S', 'K', 'J', 'O', 'T', 'H', 'U', 'Y', 'G', 'Z', 'F', 'L', 'I', 'X', 'P', 'V',
            'R', 'Q', 'E', 'D', 'M', 'A', 'C', 'W', 'B', 'N' };
    private static char[] normal = { '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' };
    private static Scanner scanner = new Scanner(System.in);

    public static void main(String args[]) {
        System.out.println("Bitte geben sie den ersten Rotor an");
        tr(Integer.parseInt(scanner.next()) - 1, 1);
        System.out.println("Bitte geben sie den zweiten Rotor an");
        tr(Integer.parseInt(scanner.next()) - 1, 2);
        System.out.println("Bitte geben sie den dritten Rotor an");
        tr(Integer.parseInt(scanner.next()) - 1, 3);
        System.out.println("Bitte geben sie nun die Rotation für den ersten Rotor an");
        rerot(0, Integer.parseInt(scanner.next()));
        System.out.println("Bitte geben sie nun die Rotation für den zweiten Rotor an");
        rerot(1, Integer.parseInt(scanner.next()));
        System.out.println("Bitte geben sie nun die Rotation für den dritten Rotor an");
        rerot(2, Integer.parseInt(scanner.next()));
        while (finish == false) {
            System.out.println(
                    "Bitte geben sie den Buchstaben ein, den sie Transkripieren wollen. Oder geben sie 'stop' ein um das Programm zu beenden");
            System.out.println(rotor(scanner.next().toCharArray()));
        }
    }

    private static void tr(int n, int m) {
        n = check(n);
        used[m - 1] = rotors[n];
    }

    private static int check(int n) {
        for (int i = 0; i < used.length; i++) {
            if (rotors[n] == used[i] || n < 0 || n > 4) {
                System.out.println(
                        "Dieser Rotor ist entweder nicht vorhanden oder wurde schon benutzt. Bitte geben sie einen gültigen Rotor an");
                n = check(Integer.parseInt(scanner.next()) - 1);
            }
        }
        return n;
    }

    private static void rerot(int n, int m) {
        while (m > 25) {
            m = m - 26;
        }
        rotations[n] = m;
    }

    private static char[] rotor(char[] n) {
        for (int i = 0; i < 3; i++) {
            temp = rotors[i];
            switch (temp) {
                case 1:
                    n = rot1(n, rotations[i]);
                    break;
                case 2:
                    n = rot2(n, rotations[i]);
                    break;
                case 3:
                    n = rot3(n, rotations[i]);
                    break;
                case 4:
                    n = rot4(n, rotations[i]);
                    break;
                case 5:
                    n = rot5(n, rotations[i]);
                    break;
                default:
                    break;
            }
        }
        return n;
    }

    private static char[] rot1(char[] n, int m) {
        for (int o = 0; o < normal.length; o++) {
            if (n == normal[o]) {
                o = o + m;
                if (o > 25) {
                    o = o - 26;
                }
                n = rotor1[o];
                break;
            }
        }
        return n;
    }

    private static char[] rot2(char[] n, int m) {
        for (int o = 0; o < normal.length; o++) {
            if (n == normal[o]) {
                o = o + m;
                if (o > 25) {
                    o = o - 26;
                }
                n = rotor2[o];
                break;
            }
        }
        return n;
    }

    private static char[] rot3(char[] n, int m) {
        for (int o = 0; o < normal.length; o++) {
            if (n == normal[o]) {
                o = o + m;
                if (o > 25) {
                    o = o - 26;
                }
                n = rotor3[o];
                break;
            }
        }
        return n;
    }

    private static char[] rot4(char[] n, int m) {
        for (int o = 0; o < normal.length; o++) {
            if (n == normal[o]) {
                o = o + m;
                if (o > 25) {
                    o = o - 26;
                }
                n = rotor4[o];
                break;
            }
        }
        return n;
    }

    private static char[] rot5(char[] n, int m) {
        for (int o = 0; o < normal.length; o++) {
            if (n == normal[o]) {
                o = o + m;
                if (o > 25) {
                    o = o - 26;
                }
                n = rotor5[o];
                break;
            }
        }
        return n;
    }
}

This is the errormessage I get:

    Exception in thread "main" java.lang.Error: Unresolved compilation problems: 

    Incompatible operand types char[] and char

    Type mismatch: cannot convert from char to char[]

    at Enigma.rot1(Enigma.java:94)
    at Enigma.rotor(Enigma.java:71)
    at Enigma.main(Enigma.java:39)

It also shows this error on the other ones where I want to pass the char[] over.

Need to add more text cause they say it is too much code and too less text.

Advertisement

Answer

I think the problem is you are comparing a char array n to a char normal[o] in rot1 on line 94 if (n == normal[o]) {. You might want to compare n[o] to normal[o] instead.

Similarly, you can’t assign n = rotor1[o];, you might want to assign n[o] instead. This is also the case with the functions rot2 to rot5.

Hope this was helpful.

Advertisement