Results 1 to 5 of 5
- 09-03-2012, 08:33 AM #1
Member
- Join Date
- Aug 2012
- Posts
- 7
- Rep Power
- 0
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
- 09-03-2012, 01:52 PM #2
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
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.
- 09-03-2012, 02:01 PM #3
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,606
- Blog Entries
- 7
- Rep Power
- 17
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,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 09-09-2012, 11:16 AM #4
Member
- Join Date
- Aug 2012
- Posts
- 7
- Rep Power
- 0
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); }
- 09-09-2012, 12:59 PM #5
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,606
- Blog Entries
- 7
- Rep Power
- 17
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,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
Similar Threads
-
How do you compare strings (see program)
By EscSequenceAlpha in forum New To JavaReplies: 5Last Post: 05-04-2012, 12:22 PM -
Compare two strings
By Proshitness in forum JDBCReplies: 1Last Post: 10-12-2011, 09:06 PM -
Compare two strings
By roud9 in forum New To JavaReplies: 1Last Post: 11-04-2010, 11:57 PM -
Compare between 2 Strings
By ChaosINC in forum New To JavaReplies: 3Last Post: 01-17-2010, 11:39 AM -
how to compare two strings
By elizabeth in forum New To JavaReplies: 7Last Post: 08-06-2007, 03:57 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks