473,698 Members | 2,271 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

yoda
291 Contributor
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 1681

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

Similar topics

9
3444
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 of all HTML parts that I don't need, i.e. <head>, <title>, <javascript> etc. I'm considering using eregi_replace for this. Anyone have an example of how to set this up ? I tried this with no luck:
2
7518
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 thought that the way to do this would be with the REPLACE function; unfortunately, of course, the REPLACE function cannot work with TEXT fields. I tried CASTing the text field to VARCHAR(8000); however, some of the rows have more than 8000...
4
2500
by: John | last post by:
Hi How can I replace characters like carriage return and double quotes from strings? Thanks Regards
3
2102
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 or more objects with properties equal to the name of those
0
1685
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 the listbox with no luck and, because it is already bound, I can't just delete the item directly from the listbox without an error (because it already has a datasource, you are not allowed to delete/add anything to the listbox directly). The...
8
4612
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 like so... Table 1 Field1 Field2 Field3 E1 April 2006 AA, BB, CC E2 April 2006 AA, BB, CC,DD, EE E3 April 2006 AA, BB
20
2838
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 then
1
1400
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 populate selectDualDSICOM2.Add(gage), however the array is populated with points 1 and point 2. Can anyone help?
2
2461
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 the csv file (they are parsed as strings) with the html equivalents of "&lt;" and "&gt;". example csv file row: "FIXED","All","Enable <audioentry"
10
3538
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
8674
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9028
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8861
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7728
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5860
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4369
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4619
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2330
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2001
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.