aspose file tools*
The moose likes Beginning Java and the fly likes Search for a single Character in a String Array Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Search for a single Character in a String Array" Watch "Search for a single Character in a String Array" New topic
Author

Search for a single Character in a String Array

Jere Johnson
Greenhorn

Joined: Mar 29, 2003
Posts: 28
Hello again,
Im attempting to write a method to find a single character and count how many times it appears in
a string array. Is this correct?

[ April 24, 2003: Message edited by: Jeremiah Coleman ]

Diapers are the best invention
Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451
Hi Jeremiah,
Looks like you have a couple of problems:
1. for (int j=0; j< array.length; j++) should be for (int j=0; j<array[i].length(); j++)
2. if (textValue.charAt (i) == (lookChar)) should be if (textValue.charAt (j) == (lookChar)).
Other than that it looks like it ought to work.


Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius - and a lot of courage - to move in the opposite direction. - Ernst F. Schumacher
Joel McNary
Bartender

Joined: Aug 20, 2001
Posts: 1824

The theory is fine, but try compiling it and running it to see what happens. After you do that, read on
It won't work because of a few minor points:
1). You don't define the data type for textValue. You should say:

2). You are using the wrong variable when doing your comparisons.

--although they are traditional, this is a good reason not to use i and j. I myself would use "index" and "position" respectively
3). Style points (OK, it won't not work because of style, but while I'm here...). While legal to ignore "{" and "}" for single-line blocks, it is preferred to include them. Two reasons: it makes the code easier to read, and (more importantly) if you had to go back and add a statement to the block, you have to remember to put the curley braces in. If they're there in the first place, it makes it easier later on (I was burned several times on this one before I learned better )
in addition, your } curley brace should line up with the leftmost "if"--again, makes it easier to read.
Joel McNary
Bartender

Joined: Aug 20, 2001
Posts: 1824

Oops--Missed the first one that Michael caught.
That's what I get for not compiling and running it myself
Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451
Hey Joel, I didn't compile it either.
Jere Johnson
Greenhorn

Joined: Mar 29, 2003
Posts: 28
Works great but,
I got one problem still.
When i search for a character, it adds itself to
the array. Can i manage this somehow?

Thanks for the reply.
Joel McNary
Bartender

Joined: Aug 20, 2001
Posts: 1824

I'm not quite sure what you are asking. Perhaps you could provide an example?
Jere Johnson
Greenhorn

Joined: Mar 29, 2003
Posts: 28
My question is how can i keep the program from
adding the character that im searching for to
the array??
I'm thinking it might be one of my boolean variables or something in the ActionPerformed method.
Jere Johnson
Greenhorn

Joined: Mar 29, 2003
Posts: 28
Also when i enter "end" and hit enter, it
adds to my array.
Is there a way i can manipulate this also from happening?
Thank you for your time. I appreciate it.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Search for a single Character in a String Array