473,411 Members | 2,285 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,411 software developers and data experts.

Looping over Character ArrayList and replacing characters with values from a HashMap

yoda
291 100+
So has the title says I need to find a away to loop over a Character ArrayList and replace the characters with a values from a HasMap.


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
  1. public ArrayList<Character> encode(ArrayList<Character> text)
  2.     {
  3.         HashMap<Character, Integer> charMap = new HashMap<Character, Integer>();
  4.         HashSet<Character> charSet = new HashSet<Character>(text);//gets the distinct characters 
  5.         ArrayList<Character> result = new ArrayList<Character>();
  6.         ArrayList<Character> filesText = new ArrayList<Character>(text);//passed in text
  7.  
  8.         int index = 0;//an index for the values to go with the key in the HashMap
  9.         for(Character chars : charSet)
  10.         {
  11.             result.add(chars);//adds the distinct values to the results ArrayList
  12.             charMap.put(chars, index);//adds the characters and the index to the HashMap 
  13.  
  14.             int i = 0;
  15.             /*
  16.              * sort of works but it's supposed to loop over the Arraylist containing
  17.              * all of the text.
  18.              * And then replace the characters in that ArrayList with the values from the HashMap
  19.              */
  20.             while( i < filesText.size())
  21.             {
  22.                 int mapIndex = charMap.get(chars);
  23.  
  24.                 char charIndex = (char)mapIndex;
  25.                 Character mapCharacter = new Character(charIndex);
  26.  
  27.                 if(mapCharacter.equals(filesText.get(i)))
  28.                 {
  29.                    result.add(mapCharacter); 
  30.                 }
  31.                 i++;
  32.             }
  33.  
  34.             index++;
  35.         }
  36.         //for testing to see what gets put into the array.
  37.         System.out.println(result);
  38.  
  39.         return result;//returns the encoded results array.
  40.     }
If you feel I've done some coding completely wrong, can you please explain why and a hint or a possible fix?

Thanks,

Yoda.
Apr 16 '11 #1
0 1665

Sign in to post your reply or Sign up for a free account.

Similar topics

9
by: Martin | last post by:
I have a HTML parser that reads product pages from various retailers - and I want to optimize it somewhat: I download all HTML before I start the parsing - and to do that I want to: - Get rid...
2
by: Richard S. Crawford | last post by:
I have a large table, tblMessage, which stores e-mail messages in text fields. I need to remove the carriage returns the data in these fields, but I have not yet figured out how to do so. I...
4
by: John | last post by:
Hi How can I replace characters like carriage return and double quotes from strings? Thanks Regards
3
by: Razzie | last post by:
Hey all, Let's say I have a textfile with 'marked' values in it, for example, something like this: Name......... *name* Street......... *street* Email.......... *email* And say I have 1...
0
by: Dave | last post by:
Hi all, I have a listbox that is complex bound by an arraylist. The problem is that when I delete an object from the arraylist, the listbox does not reflect those changes. I tried refreshing...
8
by: Chris A via AccessMonster.com | last post by:
I have an interesting problem that I have yet to come accross that I can't change data structure on because it is an export from filemaker I am reformatting for another dept. anyway. I have a table...
20
by: Ifoel | last post by:
Hi all, Sorry im beginer in vb. I want making programm looping character or number. Just say i have numbers from 100 to 10000. just sample: Private Sub Timer1_Timer() if check1.value= 1...
1
by: cmdolcet69 | last post by:
How can i get (gage) to represent gages 3 and 4 in my array list..... I have a total of 6 points. 4 points are on COM1 and 2 points are on COM2 Points 3 and 4 (which are on COM2) should...
2
by: John Smith | last post by:
Hi, I coded a python script that lets me parse a csv file into html code with a certain format, but I would like to replace every "<" and ">" character that appears within each column entry of...
10
by: wassssup | last post by:
hi i would like to know how to replace the 2nd/3rd... character in a string without knowing what value it holds.
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.