-2

Thanks for taking your time to look at my question. This is one of the methods I created for decrypting a message. This isn't the entire code, believe me I have been trying for hours messing with the code, I didnt just hop on here and hope you guys would do it for me, I'm just asking a HOW to do something!

Just so you guys know, I can't really change much from what I currently have because I'm limited based on the assignment.

My problem: I need to make any character that is an "x" equal a SPACE or " ". Basically I'm trying to hardcode every "x" within the string to become a space because it's not printing what it should be.

What I currently have:

public static String decryption(String s, int n)
{
    int originalChar, decryptedChar;
    String message = "";
    char c;

    for(int i = 0; i < s.length(); ++i)
    {
        c = s.charAt(i);
        decryptedChar = (int)c;
        if(decryptedChar + n > 126)
            originalChar = 32 + ((decryptedChar + n) - 113);
        // Problem here
        if(c.equals("x"))
            originalChar.equals(" ");
        else
            originalChar = decryptedChar + n;
        message = message + (char)originalChar;  
    }//end for loop
    return message;
}//end method

I marked the problem area. If anyone can tell me how to do this properly so that I can make any "x" equal " " instead that would be awesome! Thanks for your time.

synth
  • 21
  • 3
  • 2
    Possible duplicate of [How do I replace a character in a string in Java?](http://stackoverflow.com/questions/1234510/how-do-i-replace-a-character-in-a-string-in-java) – jpw Dec 10 '15 at 20:55
  • 4
    how can this possibly compile? char is primitive, it has no methods. You clearly haven't even tried running/debugging your own code. – user1231232141214124 Dec 10 '15 at 20:55
  • @redFive actually it works other than when I added the problem area, because I cant figure out how to properly write what I'm trying to accomplish – synth Dec 10 '15 at 21:03
  • How can you know its a problem area if you didn't even run the code? We aren't here to solve your homework without you even trying – user1231232141214124 Dec 10 '15 at 21:12
  • @redFive I dont understand where you're getting this idea that I haven't tried or haven't run the code. I've done both. I'm a beginner to java and have an honest particular question I'm asking and trying to solve. The code itself works, I'm trying to problem solve a problem within this method that is making the end result call to the method print something incorrect. – synth Dec 10 '15 at 21:26
  • You haven't run the code because it doesn't compile, I've already said that. Arguing that you tried the code you posted is only going to make you look even more foolish – user1231232141214124 Dec 10 '15 at 21:27

3 Answers3

0

your problem is with:

originalChar.equals(" ");

equals() method is a method that checks equality - it returns true if originalChar equals " ", and what matters in your case- it does not alter originalChar in any way, just compares it to " ".

if you want to set originalChar to be " ", you need to do originalChar = " "

In any case, a much easier solution would be:

s = s.replace("x"," ");
Nir Levy
  • 12,750
  • 3
  • 21
  • 38
  • @CubeJockey- yes, I doubt if this code even compiles. but putting everything else aside- equals should not be used to re-set value – Nir Levy Dec 10 '15 at 21:04
0

one of the problems is with .equals. That is not meant to do a comparison on a character, only Strings.

Further more below you try to assign a String to a character with ""s, a character assignment needs ''.

I dont get the encryption part so much so I mocked up just a simple replacement code.

Bonus: you may want to ensure both x and X are replaced.

public static void main(String[] args) {
    // TODO Auto-generated method stub
    String s = "xxistheXbestxandxmorexxHehehe";
    int n = 100;
    String message = decryption(s, n);
    System.out.println(message);

}


public static String decryption(String s, int n)
{
    int originalChar, decryptedChar;
    String message = "";
    String ret = "";
    char c;

    for(int i = 0; i < s.length(); ++i)
    {
        c = s.charAt(i);
        decryptedChar = (int)c;
        if(decryptedChar + n > 126)
            originalChar = 32 + ((decryptedChar + n) - 113);
        // Problem here
        if(c =='x')
            {originalChar = ' ';
            c = ' ';}
        else
            {originalChar = decryptedChar + n;
            c = c;}
        message = message + (char)originalChar;
        ret += c;
    }//end for loop
    //return message;
    return ret;
}//end method
iDeal
  • 53
  • 2
  • 8
0

Use the method replaceAll of the String class.

For example:

String a = "AxBxCxD";
String b = a.replaceAll("x"," ");

String b is what you want, that is "A B C D". You need b, because replaceAll does not change a. If you want to change a, you can set it to b:

a = b;

That's all :)

PieCot
  • 3,564
  • 1
  • 12
  • 20