Why won't it gives me the output that I want?

I have been working on this project for about 2 weeks now but I can't seem to figure out why it won't give me

The output that I want which is -

Original sentence = This is a very big morning.

Encrypted sentence = This is a ag',rery dug>?\ijedug>?..w ssadorninjedug>?..w.

Decrypted sentence = This is a very big morning.

However I seem to keep getting -

Original Sentence = This is a very big morning.

Encrypted sentence = This is a very dug>?/ig morning.

Decrypted sentence = This is a very big morning.

Here is my code-

import java.io.*; 
import java.util.*; 
import java.io.*; 
import java.util.*; 

public class ED { 

public static void main(String args[]) { 
    Scanner kbReader = new Scanner(System.in); 
    System.out.print("Enter a sentence that is to be encrypted:"); 
    String sntnc = kbReader.nextLine(); 
    System.out.println("Original Sentence = " + sntnc); 

    Crypto myCryptObj = new Crypto(); 
    String encryptdSntnc = myCryptObj.encrypt(sntnc); 
    System.out.println("Encrypted sentence = " + encryptdSntnc); 

    String decryptdSntnc = myCryptObj.decrypt(encryptdSntnc); 
    System.out.println("Decrypted sentence = " + decryptdSntnc); 
 } 
} 

class Crypto { 

public String x1; 

public String acceptor(String sntnc) { 
    String x1 = sntnc; 
    return x1; 
} 

public String encrypt(String sntnc) { 
    x1 = sntnc.replace("v", "ag',r"); 
    x1 = sntnc.replace("V", "ag',r"); 
    x1 = sntnc.replace("M", "ssad"); 
    x1 = sntnc.replace("m", "ssad"); 
    x1 = sntnc.replace("g", "jeb..w"); 
    x1 = sntnc.replace("G", "jeb..w"); 
    x1 = sntnc.replace("B", "dug>?/"); 
    x1 = sntnc.replace("b", "dug>?/"); 
    return x1; 
} 

public String decrypt(String sntnc) { 
    x1 = sntnc.replace("ag',r", "v"); 
    x1 = sntnc.replace("ag',r", "V"); 
    x1 = sntnc.replace("ssad", "M"); 
    x1 = sntnc.replace("ssad", "m"); 
    x1 = sntnc.replace("jeb..w", "g"); 
    x1 = sntnc.replace("jeb..w", "G"); 
    x1 = sntnc.replace("dug>?/", "B"); 
    x1 = sntnc.replace("dug>?/", "b"); 
    return x1; 
} 
}

Upvotes: 1

Views: 80

Answers (3)

Khaled.K
Khaled.K

Reputation: 5960

Test Code Here

On the condition that the input doesn't contain numbers, this should do the trick:

/* package whatever; // don't place package name! */

import java.io.*; 
import java.util.*; 
import java.io.*; 
import java.util.*; 

/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{

    public static void main(String args[])
    { 
        Scanner kbReader = new Scanner(System.in); 
        System.out.print("Enter a sentence that is to be encrypted:"); 
        String sntnc = kbReader.nextLine(); 
        System.out.println("\nOriginal Sentence = " + sntnc); 

        Crypto myCryptObj = new Crypto(); 
        String encryptdSntnc = myCryptObj.encrypt(sntnc); 
        System.out.println("Encrypted sentence = " + encryptdSntnc); 

        String decryptdSntnc = myCryptObj.decrypt(encryptdSntnc); 
        System.out.println("Decrypted sentence = " + decryptdSntnc); 
     }

} 

class Crypto
{
    public String x1; 

    public String acceptor(String sntnc)
    { 
        String x1 = sntnc; 
        return x1; 
    } 

    public String encrypt(String sntnc)
    { 
        x1 = sntnc;

        x1 = x1.replaceAll("[vV]", "1"); // 1
        x1 = x1.replaceAll("[mM]", "2"); // 2
        x1 = x1.replaceAll("[gG]", "3"); // 3
        x1 = x1.replaceAll("[bB]", "4"); // 4

        x1 = x1.replaceAll("1", "ag',r");
        x1 = x1.replaceAll("2", "ssad");
        x1 = x1.replaceAll("3", "jeb..w");
        x1 = x1.replaceAll("4", "dug>?/");



        return x1;
    } 

    public String decrypt(String sntnc)
    {
        x1 = sntnc;

        x1 = x1.replaceAll("dug\\>\\?/","4");
        x1 = x1.replaceAll("jeb\\.\\.w","3");
        x1 = x1.replaceAll("ssad","2");
        x1 = x1.replaceAll("ag',r","1");

        x1 = x1.replaceAll("4","B");
        x1 = x1.replaceAll("3","G");
        x1 = x1.replaceAll("2","M");
        x1 = x1.replaceAll("1","V");

        return x1;
    }
}

Upvotes: 0

Reut Sharabani
Reut Sharabani

Reputation: 31339

Java's Strings are immutable. You can't change them.

So you have to return a new string after encrypting/decrypting.

Luckily, all String's methods that result in a new string, actually create a new string. replace included.

So use this:

public String encrypt(String sntnc) { 
    x1 = sntnc.replace("v", "ag',r"); 
    x1 = x1.replace("V", "ag',r"); 
    x1 = x1.replace("M", "ssad"); 
    x1 = x1.replace("m", "ssad"); 
    x1 = x1.replace("g", "jeb..w"); 
    x1 = x1.replace("G", "jeb..w"); 
    x1 = x1.replace("B", "dug>?/"); 
    x1 = x1.replace("b", "dug>?/"); 
    return x1; 
} 

public String decrypt(String sntnc) { 
    x1 = sntnc.replace("ag',r", "v"); 
    x1 = x1.replace("ag',r", "V"); 
    x1 = x1.replace("ssad", "M"); 
    x1 = x1.replace("ssad", "m"); 
    x1 = x1.replace("jeb..w", "g"); 
    x1 = x1.replace("jeb..w", "G"); 
    x1 = x1.replace("dug>?/", "B"); 
    x1 = x1.replace("dug>?/", "b"); 
    return x1; 
} 

Or shorter (make sure to format with your IDE):

public String encrypt(String sntnc) { 
    return sntnc.replace("v", "ag',r").replace("V", "ag',r").replace("M", "ssad").replace("m", "ssad").replace("jeb..w", "g").replace("jeb..w", "G").replace("dug>?/", "B").replace("dug>?/", "b");  

public String decrypt(String sntnc) { 
    return sntnc.replace("ag',r", "v").replace("ag',r", "V").replace("ssad", "M").replace("ssad", "m").replace("jeb..w", "g").replace("jeb..w", "G").replace("dug>?/", "B").replace("dug>?/", "b"); 
} 

Order of replacement should matter here, so be careful

Upvotes: 2

Brian Kurz
Brian Kurz

Reputation: 141

The reason you are only seeing the encryption for the 'b' character is because in your encrypt and decrypt you are assigning x1 the result of performing the replace on sntnc, but you keep using sntnc.replace() for each line, so if you step through with the debugger you will see that each replace is happening, but when you return x1 it only has the last substitution. To get the result you are looking for, you would need to perform each subsequent replace on x1 instead of sntnc. Alternatively, you could assign sntnc to x1 and the. In encrypt() and decrypt() start by assigning sntnc to x1 and replacing all of the sntnc.replace() with x1.replace().

Upvotes: 1

Related Questions