1

So I am trying to populate a 2D array from two strings, as seen below.

However, when I go to compile my code, I get a

"java: incompatible types: char[] cannot be converted to char"

error. What am I doing wrong?

public static void main(String[] args) {
    String alphabet = ("ABCDEFGHIJKLMNOPQRSTUVWXYZ");
    String iAlphabet = ("ZYXWVUTSRQPONMLKJIHGFEDCBA");
    char alphabetArray [][] = {{alphabet.toCharArray()},{iAlphabet.toCharArray()}};
    System.out.print(alphabetArray[4][4]);
}

}

(New to Java, and am just bashing my head against a wall on this one)

8
  • What are you trying to get in your 2D array? Commented Mar 13, 2020 at 12:05
  • 2
    You mean {alphabet.toCharArray(),iAlphabet.toCharArray()};? You don't need parentheses around the Strings you know, maybe you'll want to read this as well: docs.oracle.com/javase/tutorial so you don't need to bash your head against a wall so much. Commented Mar 13, 2020 at 12:05
  • what do you want as output? Commented Mar 13, 2020 at 12:06
  • I'm trying to do a substitution cipher. So when this works, I'll write a "for" loop that will replace the characters of a string with the characters of the 'row' below. Commented Mar 13, 2020 at 12:17
  • 1
    char[] is basically a string (it is in C). The error is that you are trying to assign a "string" to a char. Just remove a set of brackets like so {alphabet.toCharArray(),iAlphabet.toCharArray()} Commented Mar 13, 2020 at 12:47

5 Answers 5

2

I guess you want to be able to translate the character from one string to the other one at the same position:

public static void main(String[] args) {
   String alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
   String iAlphabet = "ZYXWVUTSRQPONMLKJIHGFEDCBA";
   char alphabetArray [][] = {alphabet.toCharArray(),iAlphabet.toCharArray()};

   System.out.print("3rd character: " + alphabetArray[0][2] + " -> " + alphabetArray[1][2]);
}

This prints:

3rd character: C -> X

An example of ussage as translate would be:

public static void main(String[] args) {
   String alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
   String iAlphabet = "ZYXWVUTSRQPONMLKJIHGFEDCBA";
   char alphabetArray [][] = {alphabet.toCharArray(),iAlphabet.toCharArray()};

   String test = "HELLO WORLD";
   StringBuffer translated = new StringBuffer();
   for (int i = 0; i < test.length(); i++) {
       int index = alphabet.indexOf(test.charAt(i));
       if (index > 0) {
           translated.append(alphabetArray[1][index]);
       } else {
           translated.append(test.charAt(i));
       }
   }
   System.out.println("original sentence: " + test);
   System.out.println("translated sentence: " + translated.toString());
}

which prints:

original sentence: HELLO WORLD
translated sentence: SVOOL DLIOW
Sign up to request clarification or add additional context in comments.

Comments

2

Declare the arrays as shown below. And remember it's not a 26 x 26 array. It is a 2 x 26 array.

  String alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  String iAlphabet = "ZYXWVUTSRQPONMLKJIHGFEDCBA";
  char alphabetArray [][] = {alphabet.toCharArray(),iAlphabet.toCharArray()};

Print V from the second.

System.out.print(alphabetArray[1][4]);

Print E from the first.

System.out.print(alphabetArray[0][4]);

1 Comment

Not a problem. LoL!
1

You are putting an array of char where you need to place the only char. So remove the curly braces and simply put an array of char

Your Code should be like this

public static void main(String[] args) {
        String alphabet = ("ABCDEFGHIJKLMNOPQRSTUVWXYZ");
        String iAlphabet = ("ZYXWVUTSRQPONMLKJIHGFEDCBA");
        char[] charArray = alphabet.toCharArray();
        char[] charArray2 = iAlphabet.toCharArray();
        char alphabetArray[][] = { charArray, charArray2 };
        System.out.println(alphabetArray[0][23]);
    }

Comments

1

you could also take advantage of the fact that the letters 'A'-'Z' are in order

for example: if you want to translate a number to a letter in the alphabet you can just say

char a = 'A';
a+=num;

or if you want it to go backwards

char a = 'Z';
a-=num;

num being the equivelent index you would've given.

this is assuming that you want to make the array read-only of course, and some sort of validation before performing these operations would be recommended. (Verifying the number is positive and less than 26) if this works in your case, then that's great.

Comments

1

If you want to do a ceaser cipher: IE translate any character by any offset, you can do the following:

private static char offsetChar(char chr, int offset){
    chr = Character.toUpperCase(chr);
    //value from 0-25 representing letter
    int val = chr - 'A';
    //make sure that the offset doesn't make it overflow past 26
    val = (val + offset) % 26;
    // convert back to letter from number 0-25
    return (char)('A' + val);
}

also note that this will auto-capitalize the letter, if you don't want that to happen you can test if it is uppercase and return it in the correct state at the end using Character.isUpperCase and Character.toUpperCase and Character.toLowerCase

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.