• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Search for a single Character in a String Array

 
Jere Johnson
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Michael Morris
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Joel McNary
Bartender
Posts: 1840
Eclipse IDE Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1840
Eclipse IDE Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oops--Missed the first one that Michael caught.
That's what I get for not compiling and running it myself
 
Michael Morris
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Joel, I didn't compile it either.
 
Jere Johnson
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1840
Eclipse IDE Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not quite sure what you are asking. Perhaps you could provide an example?
 
Jere Johnson
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic