-1
import java.util.ArrayList;
import java.util.Arrays;

public class Solution {
    public static String solution(String x) {
        // convert String x to an array
        String[] arrayX = x.split("");
        
        // alphabet forwards, upper- and lowercase
        String[] alphabet = "abcdefghijklmnopqrstuvwxyz".split("");
        
        // alphabet backwards, upper- and lowercase
        String[] tebahpla = "zyxwvutsrqponmlkjihgfedcba".split("");
        
        ArrayList<String> decoded = new ArrayList<String>();
        for(int i = 0; i < arrayX.length; i++) {
            for(int j = 0; j < alphabet.length; j++) {
                if(alphabet[j] == arrayX[i]) {
                    decoded.add(tebahpla[j]);
                    break;
                } else if(arrayX[i] == alphabet[j].toUpperCase()) decoded.add(arrayX[i]);
            }
        }
        
        // turn arrayX back into a string
        String message = "";
        for(int j = 0; j < decoded.size(); j++) {
            message += decoded.get(j); System.out.println(decoded.get(j));
        }
        
        return message;
    }
}

So, I'm working on a project in Java and I need to get messages printed out in which characters in the message [z..a] are replaced with their opposites [a..z], excluding uppercase and special characters (those will remain the same). Currently, when using System.out.println(Solution.solution(String x)); in my main method, I'm not receiving any output except for a line of whitespace. I've tried pretty much everything that I can think of, but no luck, as my code generates no errors.

Kasizah
  • 39
  • 4
  • 1
    You have to add the original character after the inner for if it wasn't already added inside it – Iván Sep 05 '21 at 23:45
  • Are you trying to perform a [*Caesar shift*](https://en.wikipedia.org/wiki/Caesar_cipher) with the key length of 25? If that's the case, there's a better way of doing it. See [Java, How to implement a Shift Cipher (Caesar Cipher)](https://stackoverflow.com/questions/19108737/java-how-to-implement-a-shift-cipher-caesar-cipher/21820957#21820957). – Janez Kuhar Sep 05 '21 at 23:49
  • You may also want to take a look at [How to debug small programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) – Janez Kuhar Sep 05 '21 at 23:56
  • 1
    You're comparing `String`s like this: `alphabet[j] == arrayX[i]`. This is not the right way to do it. See [How do I compare strings in Java?](https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – Janez Kuhar Sep 05 '21 at 23:59

1 Answers1

0

Your code doesn't work because you use == instead of .equals() functions, in Java under the hoods objects are an abstraction pointing to a specific place in memory so for instance here: alphabet[1] is referenced as 15542 --> 'a' with 15542 the specific place in your RAM storing 'a', so even if arrayX[0] contains a its reference (for instance 17889) is different from 15542.

Put simply, if you want to compare the value of two objects you can't use == which directly compare Java objects (which mainly are an address space) and have to use .equals to compare the content.

alphabet[j] == arrayX[i] must become alphabet[j].equals(arrayX[i]) and arrayX[i] == alphabet[j].toUpperCase() must become arrayX[i].equals(alphabet[j].toUpperCase()).

Also your function can be made simpler by taking advantage of the fact that any Char is in a fact a number (a char), so for instance 'b' is actually 98 and can be converted to y with : 98 + 26 - (98 - 97)*2 with 26 as the alphabet length and 97 as the first letter of the alphabet.

Here :

public class Solution {
    public static String solution(String x) {
        String output = "";
        for (char c : x.toCharArray()) {
            if (Character.isLetter(c)) {
                char asciiBase = 97;
                if (Character.isUpperCase(c)) {
                    asciiBase = 65;
                }
                
                output += Character.toString((char) (c + 25 - (c - asciiBase)*2));
            }
        }
        
        return output;
    }
}
Mohameth
  • 376
  • 2
  • 10