473,560 Members | 3,039 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Help ArrayLis won't store, and how to remove duplicates????

Hi I'm still having some problems getting my head round this language.
A couple of things don't seem to work for me. First I am trying to
obtan a count of the number of words in a sting, so am using the split
function with ' ', but how do i get it to take into account
punctuation marks such as ',',',' etc?

Also I am then trying to add contents of an array of strings to an
arraylist, but only if the string isn't already there. I was using the
arraylist.conta ins method, but it is still adding duplicates, and
finally, I was trying to sort my arraylist, in alphabetical order
using arraylist.sort( ), but that doesn't seem to sort it fully, any
ideas on where i am going wrong, i have posted my code below. One
final question, before i add my strings of words to my arraylist, is
it possible to capitalise the first letter in each word? Thanks.

public static void ParseFile(strin g input, string output)
{
System.IO.TextR eader r = System.IO.File. OpenText(@"C:\i nput.txt");
System.IO.TextW riter w =
System.IO.File. CreateText(@"C: \output.txt");
string s = r. ReadToEnd();

ArrayList myList = new ArrayList();
int numOccur = 0;

string[] mySplit = s.Split(' ');
Console.WriteLi ne("Num of words is " + mySplit.Length) ;
for(int x = 0; x < mySplit.Length; x++)
{
if (!myList.Contai ns(mySplit[x]))
{
myList.Add(mySp lit[x]);
}
}
myList.Sort();

foreach (string item in myList)
{
w.Write(item + "\n");
}
r.Close();
w.Close();
}
Nov 16 '05 #1
11 5814
Split() is a very simplistic method. It can split on a single character, or
on multiple characters (have a look at the Split(char[]) override) but if
you have for instance quoted strings within which delimiters must be
ignored, as with:

Fred,Smith,"Sto ckton, MD"

.... or other special cases, the you'll have to write your own routine using
string manipulation methods and/or Regex.

Your code looks fine offhand. If you can provide your input.txt file and a
sample of the bad output that would be helpful in diagnosing your problem.

Incidentally, foreach does not guarantee that it will traverse a collection
in order, though in my experience it does do so with arrays and ArrayLists.
Still, before concluding that the ArrayList is not really sorted, I'd use a
for loop instead. Or look at it in the debugger.

As for capitalizing the first letter of each word -- you have to write a
method for that yourself. Simplistically, something like:

string ToProperCase(st ring strWord) {

if (strWord.Length == 0) {
return "";
} else if (strWord.Length == 1) {
return strWord.ToUpper ();
} else {
return Char.ToUpper(st rWord[0]).ToString() + strWord.Substri ng(1);
}

}

--Bob

"steve smith" <bo**********@h otmail.com> wrote in message
news:4b******** *************** ***@posting.goo gle.com...
Hi I'm still having some problems getting my head round this language.
A couple of things don't seem to work for me. First I am trying to
obtan a count of the number of words in a sting, so am using the split
function with ' ', but how do i get it to take into account
punctuation marks such as ',',',' etc?

Also I am then trying to add contents of an array of strings to an
arraylist, but only if the string isn't already there. I was using the
arraylist.conta ins method, but it is still adding duplicates, and
finally, I was trying to sort my arraylist, in alphabetical order
using arraylist.sort( ), but that doesn't seem to sort it fully, any
ideas on where i am going wrong, i have posted my code below. One
final question, before i add my strings of words to my arraylist, is
it possible to capitalise the first letter in each word? Thanks.

public static void ParseFile(strin g input, string output) {
System.IO.TextR eader r = System.IO.File. OpenText(@"C:\i nput.txt");
System.IO.TextW riter w = System.IO.File. CreateText(@"C: \output.txt");
string s = r. ReadToEnd();

ArrayList myList = new ArrayList();
int numOccur = 0;

string[] mySplit = s.Split(' ');
Console.WriteLi ne("Num of words is " + mySplit.Length) ;

for(int x = 0; x < mySplit.Length; x++) {

if (!myList.Contai ns(mySplit[x])) {
myList.Add(mySp lit[x]);
}
}

myList.Sort();

foreach (string item in myList) {
w.Write(item + "\n");
}
r.Close();
w.Close();
}
Nov 16 '05 #2
Hi my split still doesn't seem to be working correctly i have now
specified it to split by s.Split(' ',',','.',';'); but now i am
getting more words than are actually there, i will post the input
file, and hope you can point me in he right direction on how i should
split. Also I am still getting duplicates in the output file, any idea
what this could be? I will post a sample of this many thanks.

Input.txt:

No one would have believed in the last years of the nineteenth
century that this world was being watched keenly and closely by
intelligences greater than man's and yet as mortal as his own; that as
men busied themselves about their various concerns they were
scrutinised and studied, perhaps almost as narrowly as a man with a
microscope might scrutinise the transient creatures that swarm and
multiply in a drop of water. With infinite complacency men went to
and fro over this globe about their little affairs, serene in their
assurance of their empire over matter. It is possible that the
infusoria under the microscope do the same. No one gave a thought to
the older worlds of space as sources of human danger, or thought of
them only to dismiss the idea of life upon them as impossible or
improbable. It is curious to recall some of the mental habits of
those departed days. At most terrestrial men fancied there might be
other men upon Mars, perhaps inferior to themselves and ready to
welcome a missionary enterprise. Yet across the gulf of space, minds
that are to our minds as ours are to those of the beasts that perish,
intellects vast and cool and unsympathetic, regarded this earth with
envious eyes, and slowly and surely drew their plans against us. And
early in the twentieth century came the great disillusionment .

The planet Mars, I scarcely need remind the reader, revolves about the
sun at a mean distance of 140,000,000 miles, and the light and heat it
receives from the sun is barely half of that received by this world.
It must be, if the nebular hypothesis has any truth, older than our
world; and long before this earth ceased to be molten, life upon its
surface must have begun its course. The fact that it is scarcely one
seventh of the volume of the earth must have accelerated its cooling
to the temperature at which life could begin. It has air and water
and all that is necessary for the support of animated existence.

Yet so vain is man, and so blinded by his vanity, that no writer,
up to the very end of the nineteenth century, expressed any idea that
intelligent life might have developed there far, or indeed at all,
beyond its earthly level. Nor was it generally understood that since
Mars is older than our earth, with scarcely a quarter of the
superficial area and remoter from the sun, it necessarily follows that
it is not only more distant from time's beginning but nearer its end.

The secular cooling that must someday overtake our planet has
already gone far indeed with our neighbour. Its physical condition is
still largely a mystery, but we know now that even in its equatorial
region the midday temperature barely approaches that of our coldest
winter. Its air is much more attenuated than ours, its oceans have
shrunk until they cover but a third of its surface, and as its slow
seasons change huge snowcaps gather and melt about either pole and
periodically inundate its temperate zones. That last stage of
exhaustion, which to us is still incredibly remote, has become a
present-day problem for the inhabitants of Mars. The immediate
pressure of necessity has brightened their intellects, enlarged their
powers, and hardened their hearts. And looking across space with
instruments, and intelligences such as we have scarcely dreamed of,
they see, at its nearest distance only 35,000,000 of miles sunward of
them, a morning star of hope, our own warmer planet, green with
vegetation and grey with water, with a cloudy atmosphere eloquent of
fertility, with glimpses through its drifting cloud wisps of broad
stretches of populous country and narrow, navy-crowded seas.

And we men, the creatures who inhabit this earth, must be to them
at least as alien and lowly as are the monkeys and lemurs to us. The
intellectual side of man already admits that life is an incessant
struggle for existence, and it would seem that this too is the belief
of the minds upon Mars. Their world is far gone in its cooling and
this world is still crowded with life, but crowded only with what they
regard as inferior animals. To carry warfare sunward is, indeed,
their only escape from the destruction that, generation after
generation, creeps upon them.

And before we judge of them too harshly we must remember what
ruthless and utter destruction our own species has wrought, not only
upon animals, such as the vanished bison and the dodo, but upon its
inferior races. The Tasmanians, in spite of their human likeness,
were entirely swept out of existence in a war of extermination waged
by European immigrants, in the space of fifty years. Are we such
apostles of mercy as to complain if the Martians warred in the same
spirit?

The Martians seem to have calculated their descent with amazing
subtlety--their mathematical learning is evidently far in excess of
ours--and to have carried out their preparations with a well-nigh
perfect unanimity.
Output.txt:

1
1
The 1

and 1

at 1

beyond 1

came 1

fluctuating 1

flying 1

indicated 1

intellects 1

It 1

made 1

Ogilvy 1

People 1

softened 1

their 1

they 1

three 1

up 1

visible 1

were 1

with 1
" 1
"

1
"as
flaming 1
"The 1
(the 1
000 1
140 1
1894 1
2 1
35 1
A 39
A 39
A 39
A 39
A 39
A 39
A 39
A 39
A 39
A 39
A 39
A 39
A 39
A 39
A 39
A 39
A 39
A 39
A 39
A 39
A 39
A 39
A 39
A 39
A 39
A 39
A 39
A 39
A 39
A 39
A 39
A 39
A 39
A 39
A 39
A 39
A 39
A 39
A 39
A
flame 1
A
heavy 1
A
microscope 1
A
party 1
A
present-day 1
A
scrutiny 1
About 8
About 8
About 8
"Bob Grommes" <bo*@bobgrommes .com> wrote in message news:<Oh******* *******@TK2MSFT NGP09.phx.gbl>. ..
Split() is a very simplistic method. It can split on a single character, or
on multiple characters (have a look at the Split(char[]) override) but if
you have for instance quoted strings within which delimiters must be
ignored, as with:

Fred,Smith,"Sto ckton, MD"

... or other special cases, the you'll have to write your own routine using
string manipulation methods and/or Regex.

Your code looks fine offhand. If you can provide your input.txt file and a
sample of the bad output that would be helpful in diagnosing your problem.

Incidentally, foreach does not guarantee that it will traverse a collection
in order, though in my experience it does do so with arrays and ArrayLists.
Still, before concluding that the ArrayList is not really sorted, I'd use a
for loop instead. Or look at it in the debugger.

As for capitalizing the first letter of each word -- you have to write a
method for that yourself. Simplistically, something like:

string ToProperCase(st ring strWord) {

if (strWord.Length == 0) {
return "";
} else if (strWord.Length == 1) {
return strWord.ToUpper ();
} else {
return Char.ToUpper(st rWord[0]).ToString() + strWord.Substri ng(1);
}

}

--Bob

"steve smith" <bo**********@h otmail.com> wrote in message
news:4b******** *************** ***@posting.goo gle.com...
Hi I'm still having some problems getting my head round this language.
A couple of things don't seem to work for me. First I am trying to
obtan a count of the number of words in a sting, so am using the split
function with ' ', but how do i get it to take into account
punctuation marks such as ',',',' etc?

Also I am then trying to add contents of an array of strings to an
arraylist, but only if the string isn't already there. I was using the
arraylist.conta ins method, but it is still adding duplicates, and
finally, I was trying to sort my arraylist, in alphabetical order
using arraylist.sort( ), but that doesn't seem to sort it fully, any
ideas on where i am going wrong, i have posted my code below. One
final question, before i add my strings of words to my arraylist, is
it possible to capitalise the first letter in each word? Thanks.

public static void ParseFile(strin g input, string output) {
System.IO.TextR eader r = System.IO.File. OpenText(@"C:\i nput.txt");
System.IO.TextW riter w = System.IO.File. CreateText(@"C: \output.txt");
string s = r. ReadToEnd();

ArrayList myList = new ArrayList();
int numOccur = 0;

string[] mySplit = s.Split(' ');
Console.WriteLi ne("Num of words is " + mySplit.Length) ;

for(int x = 0; x < mySplit.Length; x++) {

if (!myList.Contai ns(mySplit[x])) {
myList.Add(mySp lit[x]);
}
}

myList.Sort();

foreach (string item in myList) {
w.Write(item + "\n");
}
r.Close();
w.Close();
}

Nov 16 '05 #3
If you want to split on multiple characters you have to pass a character array, e.g.,

s.Split(new char[] {' ',',','.',';'}) ;

I assume that's what you mean.

I know that this will parse "140,000,00 0" into three words, and I expect it to parse "matter. It" (with 2 spaces before It) into "matter", "It" and two empty strings (the "word" between the period and the space, and the "word" between the two spaces would be considered words, I think). As a side note, in VS 2005 (CLR 2.0) there will be an option you can set to not return empty strings if you wish. It's managed by a new enum called StringSplitOpti ons and some new overrides to the Split() method.

I will try to take a closer look at this tonight, but I may run out of time, and I'll be out of town and out of touch until Wed.

--Bob
"steve smith" <bo**********@h otmail.com> wrote in message news:4b******** *************** **@posting.goog le.com...
Hi my split still doesn't seem to be working correctly i have now
specified it to split by s.Split(' ',',','.',';'); but now i am
getting more words than are actually there, i will post the input
file, and hope you can point me in he right direction on how i should
split. Also I am still getting duplicates in the output file, any idea
what this could be? I will post a sample of this many thanks.


Nov 16 '05 #4
Bob Grommes <bo*@bobgrommes .com> wrote:
If you want to split on multiple characters you have to pass a
character array, e.g.,

s.Split(new char[] {' ',',','.',';'}) ;

I assume that's what you mean.


No need for that, as s.Split takes a params char[] parameter.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #5
steve smith <bo**********@h otmail.com> wrote:
Hi my split still doesn't seem to be working correctly i have now
specified it to split by s.Split(' ',',','.',';'); but now i am
getting more words than are actually there, i will post the input
file, and hope you can point me in he right direction on how i should
split. Also I am still getting duplicates in the output file, any idea
what this could be? I will post a sample of this many thanks.


Hmm. That's very odd - I've tried your code and it nearly works fine. I
added '\r' and '\n' to the split chars, as otherwise you get the start
of each new line as a word in itself. Here's what the results look
like:
000
140
35
a
about
accelerated
across
admits
affairs
after
against
air
alien
all
almost
already
amazing
an
and
And
animals
animated
any
apostles
approaches
are
Are
area
as
assurance
at
At
atmosphere
attenuated
barely
be
beasts
become
before
begin
beginning
begun
being
belief
believed
beyond
bison
blinded

(etc)

Could you post the *exact* code you used to generate the output you
posted? The code you wrote in your first post doesn't include numbers,
so I assume you've changed the code somewhat anyway.

Here's the code I used:

using System;
using System.IO;
using System.Collecti ons;

class Test
{

public static void Main()
{
System.IO.TextR eader r = System.IO.File. OpenText("input .txt");
System.IO.TextW riter w =
System.IO.File. CreateText("out put.txt");
string s = r. ReadToEnd();

ArrayList myList = new ArrayList();

string[] mySplit = s.Split(' ',',','.',';',' \r','\n');
Console.WriteLi ne("Num of words is " + mySplit.Length) ;
for(int x = 0; x < mySplit.Length; x++)
{
if (!myList.Contai ns(mySplit[x]))
{
myList.Add(mySp lit[x]);
}
}
myList.Sort();

foreach (string item in myList)
{
w.Write(item + "\n");
}
r.Close();
w.Close();
}
}

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #6
> arraylist.conta ins method, but it is still adding duplicates, and

The Contains will tell you if an object is already in the list, it does not
look at the values of the objects. You should look at teh StringCollectio n
class. The Contains method of this class actualy looks at the value.

Etienne Boucher
Nov 16 '05 #7
Etienne Boucher <et*****@novat. qc.ca> wrote:
arraylist.conta ins method, but it is still adding duplicates, and


The Contains will tell you if an object is already in the list, it does not
look at the values of the objects.


Yes it does - ArrayList.Conta ins calls Equals on either the object
you're looking for with an argument of each of the references within
the list, or vice versa. Here's a program to show that:

using System;
using System.Collecti ons;

class Foo
{
public override bool Equals (object o)
{
Console.WriteLi ne ("Equals called");
return true;
}
}

class Test
{
public static void Main()
{
ArrayList al = new ArrayList();

Foo f = new Foo();
al.Add(f);
Foo g = new Foo();
Console.WriteLi ne (al.Contains(g) );
}
}

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #8
Thanks for proving me wrong, it's great to have you around Jon. I'm curious
however, doesn't...

string s1 = "1";
string s2 = "2";
(object) s1 == (object) s2;

call Object.Equals? Why does it still call String.Equals with Contains? Mmm,
I just tested myself with...

static void Main(string[] args)
{
string s1 = "1";
string s2 = "1";
Console.WriteLi ne(Test(s1, s2));
}

static bool Test(object o1, object o2)
{
return o1.Equals(o2);
}

and it does call String.Equals. I always thought both references had to be
of type string for that to happen.

Etienne Boucher
"Jon Skeet [C# MVP]" <sk***@pobox.co m> a écrit dans le message de
news:MP******** *************** *@msnews.micros oft.com...
Etienne Boucher <et*****@novat. qc.ca> wrote:
arraylist.conta ins method, but it is still adding duplicates, and


The Contains will tell you if an object is already in the list, it does not look at the values of the objects.


Yes it does - ArrayList.Conta ins calls Equals on either the object
you're looking for with an argument of each of the references within
the list, or vice versa. Here's a program to show that:

using System;
using System.Collecti ons;

class Foo
{
public override bool Equals (object o)
{
Console.WriteLi ne ("Equals called");
return true;
}
}

class Test
{
public static void Main()
{
ArrayList al = new ArrayList();

Foo f = new Foo();
al.Add(f);
Foo g = new Foo();
Console.WriteLi ne (al.Contains(g) );
}
}

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 16 '05 #9
Etienne Boucher <et*****@novat. qc.ca> wrote:
Thanks for proving me wrong, it's great to have you around Jon. I'm curious
however, doesn't...

string s1 = "1";
string s2 = "2";
(object) s1 == (object) s2;

call Object.Equals?
Because the normal reference type equality operator is equivalent to
calling Object.Referenc eEquals.
Why does it still call String.Equals with Contains?
Because Contains don't use

if (x==y)

it uses

if (x.Equals(y))
Mmm, I just tested myself with...

static void Main(string[] args)
{
string s1 = "1";
string s2 = "1";
Console.WriteLi ne(Test(s1, s2));
}

static bool Test(object o1, object o2)
{
return o1.Equals(o2);
}

and it does call String.Equals. I always thought both references had to be
of type string for that to happen.


Both expressions have to be string for x==y to call the overloaded
equality operator, which is equivalent to calling String.Equals.

o1.Equals(o2) is calling the overridden Object.Equals(O bject), however,
which is just a normal overridden method call.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
2512
by: Herman | last post by:
Hi everyone, I'm implementing Dijkstra's algorithm for a class project, and I'm having trouble compiling the class that runs the algorithm. All of the other files compile fine except for this one. I am using the compiler like this: javac -classpath .. <filename.java> The source code of the classes follow. Dijkstra.java is the class...
8
5459
by: baustin75 | last post by:
Posted: Mon Oct 03, 2005 1:41 pm Post subject: cannot mail() in ie only when debugging in php designer 2005 -------------------------------------------------------------------------------- Hello, I have a very simple problem but cannot seem to figure it out. I have a very simple php script that sends a test email to myself. When I...
5
1623
by: trode | last post by:
Hello all. For the first time, our company might make use of the MS Access portion of the office package. Problem is, I don't know it well enough to whip out a desired program. What we want to do is have two Tables. One table called PRODUCTS, containing "partnumbers" with "descriptions". The other table called "AUDITED PRODUCTS" that...
8
298
by: shawnews | last post by:
Ok...I was persented this problem and i have it almost worked out (I think). A customer of mine has asked for a database to store information based on a written form he used in the past. The nature of the form is such that there are several areas of info but the tables would NOT form any kind of one-to-many relationship. I know it is rare,...
0
5541
by: gunimpi | last post by:
http://www.vbforums.com/showthread.php?p=2745431#post2745431 ******************************************************** VB6 OR VBA & Webbrowser DOM Tiny $50 Mini Project Programmer help wanted ******************************************************** For this teeny job, please refer to: http://feeds.reddit.com/feed/8fu/?o=25
5
2802
by: Richard Gromstein | last post by:
Hello, I have an exercise that I need to finish very soon and I really need help understanding what to do and how exactly to do it. I am working on reading the chapter right now and working on it myself, but I have a feeling I won't get far. Please help me complete the exercise. I am posting what needs to be done and will be updating with...
16
3477
by: ARC | last post by:
Hello all, So I'm knee deep in this import utility program, and am coming up with all sorts of "gotcha's!". 1st off. On a "Find Duplicates Query", does anyone have a good solution for renaming the duplicate records? My thinking was to take the results of the duplicate query, and somehow have it number each line where there is a duplicate...
3
2837
by: ryan.paquette | last post by:
In the table there are 2 fields in which I wish to limit (i.e. No Duplicates) Although I do not want to limit them to "No Duplicates" separately. I need them to be limited to "No Duplicates" as if they were one field. The 2 fields are "Employee_Name" & "Training_Course". *(There is another table for Employees & another for Training...
118
4563
by: Chuck Cheeze | last post by:
This might be in the wrong group, but... Here is an example of my data: entry_id cat_id 1 20 2 25 3 30 4 25 5 35
0
7636
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...
0
7557
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7849
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7603
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
1
5461
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5176
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...
1
2051
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1171
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
882
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...

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.