This is a simple encryption but I can't figure it out. What I have to do is get the distinct characters from the text that's been passed in and then assign values to them using a HashMap. I've done that. Also in front of the file there should be the number of distinct characters and the distinct characters them selves I need help with that too. But replacing the values you the ArrayList is another thing if someone could help me that would be awesome! My code will be commented to help explain what I'm trying to say.
example:
Original text (ignore spaces):
p r o g r a m m i n g _ p r a c t i c e
encoded file:
(12)p r o g a m i n _ c t e (0) (1) (2) (3) (1) (4) (5) (5) (6) (7) (3) (8) (0) (1) (4) (9) (10) (6) (9) (11)
Here's my code so far ( only half works ):
Expand|Select|Wrap|Line Numbers
- public ArrayList<Character> encode(ArrayList<Character> text)
- {
- HashMap<Character, Integer> charMap = new HashMap<Character, Integer>();
- HashSet<Character> charSet = new HashSet<Character>(text);//gets the distinct characters
- ArrayList<Character> result = new ArrayList<Character>();
- ArrayList<Character> filesText = new ArrayList<Character>(text);//passed in text
- int index = 0;//an index for the values to go with the key in the HashMap
- for(Character chars : charSet)
- {
- result.add(chars);//adds the distinct values to the results ArrayList
- charMap.put(chars, index);//adds the characters and the index to the HashMap
- int i = 0;
- /*
- * sort of works but it's supposed to loop over the Arraylist containing
- * all of the text.
- * And then replace the characters in that ArrayList with the values from the HashMap
- */
- while( i < filesText.size())
- {
- int mapIndex = charMap.get(chars);
- char charIndex = (char)mapIndex;
- Character mapCharacter = new Character(charIndex);
- if(mapCharacter.equals(filesText.get(i)))
- {
- result.add(mapCharacter);
- }
- i++;
- }
- index++;
- }
- //for testing to see what gets put into the array.
- System.out.println(result);
- return result;//returns the encoded results array.
- }
Thanks,
Yoda.