I've got a java project in which I must replace all v's(big or small) with ag',r and replace all m's(big or small) with ssad, all g's(big or small) with jeb..w and all b's(big or small) with dug>?/. I'm stumped on what to do. I know how to normally replace chars but how do you replace a letter(big or small) with more than 1 letter? If you could help me get started I'd appreciate it greatly. Thanks. I already know what the Tester class has to be but the Crypto class is what I'm having major problems with. Maybe it's something obvious that I'm just not seeing so if you could give some insight or help me get started, as I mentioned before, I'll be highly appreciative.


Talon

see the documentation for String.replaceAll(String, String).

I'll check it out and repost back if I find something out or not. Thanks for the help.

Here's my Tester class, making my other one with the encrypt and decrypt method is my problem. I can't seem to get how to replace a char with a String.

import TerminalIO.*;
public class Tester
{
    public static void main(String args[])
    {
        KeyboardReader reader = new KeyboardReader();
        System.out.print("Enter a sentence that is to be encrypted");
        String sntnc = reader.readLine();
        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);
    }
}

And what's the problem?
I don't see any attempt to do anything of the kind in there...

String has methods to turn a String into a character array, and to read a character array into a String.
Character provides a means to turn a char into a String.
CharSequence (from which String derives) has methods to manipulate itself on a character level.

That was my Tester class, the one used to run the whole thing. I've got what I want it to do but like I said earlier I can't figure out how to make my encrypt method. I'm probably going to just have to take a 0 on this project cause for some reason I'm just not getting this.

Member Avatar for iamthwee

That was my Tester class, the one used to run the whole thing. I've got what I want it to do but like I said earlier I can't figure out how to make my encrypt method. I'm probably going to just have to take a 0 on this project cause for some reason I'm just not getting this.

Maybe if you gave us a detailed explanation of how your encryption method might work we could help.

The thing is I don't know how to do it. If I could give you a detailed explanation of it I would. All I know is I have to find a way to replace all of those letters, big or small, with the letters we have to. I know how to call it and everything but the actual perimeters are what I need. We have done work with replace methods and arrays but not so much with arrays. I have a basic concept of arrays so if you could give me an idea in laments terms maybe I can make something.

Here's an idea I had for the encrypt method but the thing is when I replcae the v with the ag',r since we have to also replace a "g" it encrypt the g in the "ag',r" also. Any advice?

public String sntnc;

public Crypto(String crypt)
    {
        sntnc = crypt;
    }

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

> when I replcae the v with the ag',r since we have to also replace a "g" it encrypt the g in the "ag',r" also. Any advice

Well that's not a problem with the language. It's a problem with your logic.

Your encryption and decryption method should be complete and without ambiguity. Have you looked at encryption using the caesar cipher method?

or ROT13 as it's more correctly called.

Hardly solid encryption, but a nice placeholder.

I am a first year java student, this isn't exactly real encryption it's just a project. The things you mentioned I have no clue what they are...FYI.

Member Avatar for iamthwee

I am a first year java student, this isn't exactly real encryption it's just a project. The things you mentioned I have no clue what they are...FYI.

The caesar cipher is about as simple as it gets.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.