Results 1 to 5 of 5
  1. #1
    pindo is offline Member
    Join Date
    Aug 2012
    Posts
    7
    Rep Power
    0

    Default using hashmap to compare two strings/characters

    i need to write a code that kind of works like an encryption code.
    the user will input a string (A-Z, 0-9) this will create a map between the two characters

    for example the default string is ABCDEFGHIJKL, the user inputs 1234PQRSTUVW.
    1 will map to A, 2 will map to B, 3 to C and so on.
    the user can then enter a string and this string will be encrypted with the 'new' numbers/letters.
    for example the user will enter string: 'HI' and the output will be 'ST' as 'H' is mapped to 'S' and 'I' to 'T'


    any help is much appreciated,
    pindo

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,474
    Rep Power
    21

    Default Re: using hashmap to compare two strings/characters

    What code have you got so far?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,048
    Blog Entries
    7
    Rep Power
    23

    Default Re: using hashmap to compare two strings/characters

    You don't need Maps for that; two simple Strings, one with the encoded characters and one with the decoded characters (as in your example) and the indexOf( ... ) method will be enough.

    kind regards,

    Jos
    The only person who got everything done by Friday was Robinson Crusoe.

  4. #4
    pindo is offline Member
    Join Date
    Aug 2012
    Posts
    7
    Rep Power
    0

    Default Re: using hashmap to compare two strings/characters

    managed to figure it out =)
    Java Code:
            String input,encrypted, usrInput; 
            String origAlpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789., !";
            Scanner keyboard = new Scanner (System.in); 
            System.out.println("'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789., !'" );
            System.out.println("Please enter the string above in a different order so we can create a one-to-one mapping for encryption");
            System.out.println("For example: '0123456789., !PQRSTUVWXYZABCDEFGHIJKLMNO' ");
            input = keyboard.nextLine();
            int tempEncrypt = 0;
            encrypted = "";
            Scanner keybrd = new Scanner (System.in);
            System.out.println("Please enter a sentence to be encrypted:");
            usrInput=keybrd.nextLine();
            for (int i=0; i <usrInput.length(); i++) {
               tempEncrypt = origAlpha.indexOf(usrInput.charAt(i));               
               encrypted =  encrypted + input.charAt(tempEncrypt);
               
           }
           System.out.println("The encrypted version of your string is: " +encrypted);
        }

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,048
    Blog Entries
    7
    Rep Power
    23

    Default Re: using hashmap to compare two strings/characters

    Glad to read that it works; see? two Strings are enough to get those Caesar encryptions/decryptions implemented. Those Maps would've worked too but they're overkill here ...

    kind regards,

    Jos
    The only person who got everything done by Friday was Robinson Crusoe.

Similar Threads

  1. How do you compare strings (see program)
    By EscSequenceAlpha in forum New To Java
    Replies: 5
    Last Post: 05-04-2012, 12:22 PM
  2. Compare two strings
    By Proshitness in forum JDBC
    Replies: 1
    Last Post: 10-12-2011, 09:06 PM
  3. Compare two strings
    By roud9 in forum New To Java
    Replies: 1
    Last Post: 11-05-2010, 12:57 AM
  4. Compare between 2 Strings
    By ChaosINC in forum New To Java
    Replies: 3
    Last Post: 01-17-2010, 12:39 PM
  5. how to compare two strings
    By elizabeth in forum New To Java
    Replies: 7
    Last Post: 08-06-2007, 03:57 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •