Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 9 of 9

Thread: Replacing letters in a string NOT WORKING.

  1. #1
    Member
    Join Date
    Oct 2010
    Posts
    38
    Thanks
    11
    Thanked 1 Time in 1 Post

    Default Replacing letters in a string NOT WORKING.

    I'm changing my encryption program so that instead of numbers it uses a combination like D5 or A9. This will make it easier for the decryption part of the program. It's not working properly though. when I enter a lowercase a it gives me C061, but its supposed to change to P1. I don't understand why it's spitting out the wrong stuff! This happens with all the other letters too.

    pdphrase = post decryption phrase
    pephrase = post encryption phrase

    Code for replacing the a:
    			if (pdphrase.contains("a")) {
     
    				pdphrase = pdphrase.replaceAll("a", "P1");
     
    			}

    Please help!

    EDIT: I took out the part about decrypting the C061 because I was checking the wrong string.
    Last edited by sp11k3t3ht3rd; January 12th, 2011 at 10:38 AM.


  2. #2
    Junior Member
    Join Date
    Sep 2010
    Location
    Netherlands
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Replacing letters in a string NOT WORKING.

    It seemed to work when i tried it.
    Tbh, i haven't seen the rest of your code so that might be it but. what i did was this:
    public String checkString() {
        String pdphrase = null;
        try {
            System.out.println("Type in a string you want encrypted!");
            pdphrase = br.readLine();
        } catch(IOException io) {
            System.out.println(io);
        }
        	if (pdphrase.contains("a")) {
                        pdphrase = pdphrase.replace("a", "P1");
                        System.out.println(pdphrase);
                        return pdphrase;
    			}
        return null;
        }

  3. #3
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 286 Times in 225 Posts
    Blog Entries
    4

    Default Re: Replacing letters in a string NOT WORKING.

    That code looks OK to me.. Is it still causing problems?

    If so, please post all of your code for us to look at.
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers

  4. #4
    Member
    Join Date
    Oct 2010
    Posts
    38
    Thanks
    11
    Thanked 1 Time in 1 Post

    Default Re: Replacing letters in a string NOT WORKING.

    This is all of my code. The weird thing is that I also am making the same program in C++. It uses the same changes (in both programs a gets changes to P1) but they both do it wrong, but in the same way. entering a in both programs will give you V62. Its really weird.

    public class MEDS {
     
    	public static void main(String [] args) {
     
    		userInput input = new userInput();
    		String choice = "Enter your choice: ";
    		String dphrase = "Enter the phrase you want encrypted: ";
    		String pdphrase;
    		String ephrase = "Enter the phrase you want decrypted: ";
    		String pephrase;
     
    		System.out.println("Welcome to M.E.D.S.");
    		System.out.println("");
    		System.out.println("Mike's Encryption / Decryption Software");
    		System.out.println("");	
    		System.out.println("Options:");
    		System.out.println("-----------------");
    		System.out.println("| 1. Encryption |");
    		System.out.println("| 2. Decryption |");
    		System.out.println("-----------------");
    		System.out.println("");
     
    		input.getUserInput(choice);
     
    		if (input.input.equals("1")) {
     
    			System.out.println("");
     
    			pdphrase = input.getUserInput(dphrase);
     
    			if (pdphrase.contains("a")) {
     
    				pdphrase = pdphrase.replaceAll("a", "Z2");
     
    			}
     
    			if (pdphrase.contains("b")) {
     
    				pdphrase = pdphrase.replaceAll("b", "C4");
     
    			}
     
    			if (pdphrase.contains("c")) {
     
    				pdphrase = pdphrase.replaceAll("c", "F1");
     
    			}
     
    			if (pdphrase.contains("d")) {
     
    				pdphrase = pdphrase.replaceAll("d", "T0");
     
    			}
     
    			if (pdphrase.contains("e")) {
     
    				pdphrase = pdphrase.replaceAll("e", "R5");
     
    			}
     
    			if (pdphrase.contains("f")) {
     
    				pdphrase = pdphrase.replaceAll("f", "Q1");
     
    			}
     
    			if (pdphrase.contains("g")) {
     
    				pdphrase = pdphrase.replaceAll("g", "K2");
     
    			}
     
    			if (pdphrase.contains("h")) {
     
    				pdphrase = pdphrase.replaceAll("h", "O0");
     
    			}
     
    			if (pdphrase.contains("i")) {
     
    				pdphrase = pdphrase.replaceAll("i", "K6");
     
    			}
     
    			if (pdphrase.contains("j")) {
     
    				pdphrase = pdphrase.replaceAll("j", "N5");
     
    			}
     
    			if (pdphrase.contains("k")) {
     
    				pdphrase = pdphrase.replaceAll("k", "L1");
     
    			}
     
    			if (pdphrase.contains("l")) {
     
    				pdphrase = pdphrase.replaceAll("l", "S5");
     
    			}
     
    			if (pdphrase.contains("m")) {
     
    				pdphrase = pdphrase.replaceAll("m", "R1");
     
    			}
     
    			if (pdphrase.contains("n")) {
     
    				pdphrase = pdphrase.replaceAll("n", "S3");
     
    			}
     
    			if (pdphrase.contains("o")) {
     
    				pdphrase = pdphrase.replaceAll("o", "S0");
     
    			}
     
    			if (pdphrase.contains("p")) {
     
    				pdphrase = pdphrase.replaceAll("p", "A8");
     
    			}
     
    			if (pdphrase.contains("q")) {
     
    				pdphrase = pdphrase.replaceAll("q", "L6");
     
    			}
     
    			if (pdphrase.contains("r")) {
     
    				pdphrase = pdphrase.replaceAll("r", "E4");
     
    			}
     
     
    			if (pdphrase.contains("s")) {
     
    				pdphrase = pdphrase.replaceAll("s", "I3");
     
    			}
     
    			if (pdphrase.contains("t")) {
     
    				pdphrase = pdphrase.replaceAll("t", "F3");
     
    			}
     
    			if (pdphrase.contains("u")) {
     
    				pdphrase = pdphrase.replaceAll("u", "M3");
     
    			}
     
    			if (pdphrase.contains("v")) {
     
    				pdphrase = pdphrase.replaceAll("v", "P7");
     
    			}
     
    			if (pdphrase.contains("w")) {
     
    				pdphrase = pdphrase.replaceAll("w", "L3");
     
    			}
     
    			if (pdphrase.contains("x")) {
     
    				pdphrase = pdphrase.replaceAll("x", "J7");
     
    			}
     
    			if (pdphrase.contains("y")) {
     
    				pdphrase = pdphrase.replaceAll("y", "H8");
     
    			}
     
     
    			if (pdphrase.contains("z")) {
     
    				pdphrase = pdphrase.replaceAll("z", "N2");
     
    			}
     
    			if (pdphrase.contains("A")) {
     
    				pdphrase = pdphrase.replaceAll("A", "N3");
     
    			}
     
    			if (pdphrase.contains("B")) {
     
    				pdphrase = pdphrase.replaceAll("B", "R0");
     
    			}
     
    			if (pdphrase.contains("C")) {
     
    				pdphrase = pdphrase.replaceAll("C", "B3");
     
    			}
     
    			if (pdphrase.contains("D")) {
     
    				pdphrase = pdphrase.replaceAll("D", "O4");
     
    			}
     
    			if (pdphrase.contains("E")) {
     
    				pdphrase = pdphrase.replaceAll("E", "Y4");
     
    			}
     
    			if (pdphrase.contains("F")) {
     
    				pdphrase = pdphrase.replaceAll("F", "N0");
     
    			}
     
    			if (pdphrase.contains("G")) {
     
    				pdphrase = pdphrase.replaceAll("G", "G6");
     
    			}
     
    			if (pdphrase.contains("H")) {
     
    				pdphrase = pdphrase.replaceAll("H", "Y1");
     
    			}
     
    			if (pdphrase.contains("I")) {
     
    				pdphrase = pdphrase.replaceAll("I", "U8");
     
    			}
     
    			if (pdphrase.contains("J")) {
     
    				pdphrase = pdphrase.replaceAll("I", "L4");
     
    			}
     
    			if (pdphrase.contains("K")) {
     
    				pdphrase = pdphrase.replaceAll("K", "E7");
     
    			}
     
    			if (pdphrase.contains("L")) {
     
    				pdphrase = pdphrase.replaceAll("L", "W0");
     
    			}
     
    			if (pdphrase.contains("M")) {
     
    				pdphrase = pdphrase.replaceAll("M", "Y3");
     
    			}
     
    			if (pdphrase.contains("N")) {
     
    				pdphrase = pdphrase.replaceAll("N", "E0");
     
    			}
     
    			if (pdphrase.contains("O")) {
     
    				pdphrase = pdphrase.replaceAll("O", "S1");
     
    			}
     
    			if (pdphrase.contains("P")) {
     
    				pdphrase = pdphrase.replaceAll("P", "R6");
     
    			}
     
    			if (pdphrase.contains("Q")) {
     
    				pdphrase = pdphrase.replaceAll("Q", "F4");
     
    			}
     
    			if (pdphrase.contains("R")) {
     
    				pdphrase = pdphrase.replaceAll("R", "C0");
     
    			}
     
     
    			if (pdphrase.contains("S")) {
     
    				pdphrase = pdphrase.replaceAll("S", "O6");
     
    			}
     
    			if (pdphrase.contains("T")) {
     
    				pdphrase = pdphrase.replaceAll("T", "Y0");
     
    			}
     
    			if (pdphrase.contains("U")) {
     
    				pdphrase = pdphrase.replaceAll("U", "B8");
     
    			}
     
    			if (pdphrase.contains("V")) {
     
    				pdphrase = pdphrase.replaceAll("V", "Q0");
     
    			}
     
    			if (pdphrase.contains("W")) {
     
    				pdphrase = pdphrase.replaceAll("W", "O5");
     
    			}
     
    			if (pdphrase.contains("X")) {
     
    				pdphrase = pdphrase.replaceAll("X", "B6");
     
    			}
     
    			if (pdphrase.contains("Y")) {
     
    				pdphrase = pdphrase.replaceAll("Y", "B1");
     
    			}
     
     
    			if (pdphrase.contains("Z")) {
     
    				pdphrase = pdphrase.replaceAll("Z", "V6");
     
    			}
     
    			System.out.println("");
    			System.out.println("Encrypted Phrase:");
    			System.out.println("");
    			System.out.println(pdphrase);
    			System.out.println("");
     
     
    		}
     
    		if (input.input.equals("2")) {
     
    			System.out.println("");
     
    			pephrase = input.getUserInput(ephrase);
     
    			if (pephrase.contains("V62")) {
     
    				pephrase = pephrase.replaceAll("V62" , "a");
     
    			}
     
     
    			System.out.println("");
    			System.out.println("Decrypted Phrase:");
    			System.out.println("");
    			System.out.println(pephrase);
    			System.out.println("");
     
    		}
     
    	}
     
    }

    I haven't finished the decryption code yet.

  5. #5
    Member DavidFongs's Avatar
    Join Date
    Oct 2010
    Location
    Minneapolis, MN
    Posts
    107
    Thanks
    1
    Thanked 45 Times in 41 Posts

    Default Re: Replacing letters in a string NOT WORKING.

    Seems pretty obvious.... Although your original code is nowhere to be found in the post that contains "all" of your code

    Lets say the original String was something simple like "a"

    The first if would be true, and execute:
    if (pdphrase.contains("a")) {
     
                    pdphrase = pdphrase.replaceAll("a", "Z2");
     }

    Now pdphrase is "Z2"..... The rest of the if statements would be false, until the last one

     
                if (pdphrase.contains("Z")) {
     
                    pdphrase = pdphrase.replaceAll("Z", "V6");
     
                }


    So pdphrase would change from Z2 to V62.

    You are replacing already encrypted values.
    Last edited by DavidFongs; January 13th, 2011 at 05:28 PM.

  6. #6
    Member
    Join Date
    Oct 2010
    Posts
    38
    Thanks
    11
    Thanked 1 Time in 1 Post

    Default Re: Replacing letters in a string NOT WORKING.

    Wow... I can't believe I didn't see that. Any suggestions on different values I could use or should I just make the capital letters the same as the lowercase letters?
    Last edited by sp11k3t3ht3rd; January 13th, 2011 at 05:39 PM.

  7. #7
    Member DavidFongs's Avatar
    Join Date
    Oct 2010
    Location
    Minneapolis, MN
    Posts
    107
    Thanks
    1
    Thanked 45 Times in 41 Posts

    Default Re: Replacing letters in a string NOT WORKING.

    You can use your same values for replacement, you just need to make sure you only replace characters that were originally in the String to be "encrypted"

    This can be done many ways. The simplest is probably to "encrypt" each character one by one starting from index 0.

  8. #8
    Member
    Join Date
    Oct 2010
    Posts
    38
    Thanks
    11
    Thanked 1 Time in 1 Post

    Default Re: Replacing letters in a string NOT WORKING.

    So use a loop with a position variable that increments each time it goes through all the characters? Suggestion on loop type?

  9. #9
    Member
    Join Date
    Oct 2010
    Posts
    38
    Thanks
    11
    Thanked 1 Time in 1 Post

    Default Re: Replacing letters in a string NOT WORKING.

    Never mind, I'm just going to keep it as it is. That way if anyone got a hold of the code they'd get tripped up a bit.

Similar Threads

  1. [SOLVED] replacing elements in a string
    By sp11k3t3ht3rd in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 10th, 2011, 06:31 PM
  2. [SOLVED] replacing subString
    By nasi in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 21st, 2010, 09:47 PM
  3. dispose()'ing a JFrame and replacing it
    By musasabi in forum What's Wrong With My Code?
    Replies: 5
    Last Post: May 14th, 2010, 02:31 PM
  4. Generating random letters and numbers together
    By newJava in forum Java Theory & Questions
    Replies: 3
    Last Post: March 19th, 2010, 04:08 AM
  5. How to remove letters
    By noobish in forum Java Theory & Questions
    Replies: 13
    Last Post: October 3rd, 2009, 10:36 PM