• Post Reply Bookmark Topic Watch Topic
  • New Topic

Character Array question  RSS feed

 
Josh Theisen
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alright, the title may be a bit misleading but I have no idea how else to phrase it.
I have an array of type char, and it contains 5 characters (say '1','2','3','4','5').

Next, I'll be going through a string one character at a time... if it's one of the above characters, I
want my program to do something else - is it possible when I'm going through my string to check
the array to see if it's one of the characters? Or do I have to do this the "hard" way with if/then checks
or switch statements?

Thanks
 
Nitin Surana
Ranch Hand
Posts: 129
Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is method boolean contains(char) in String class. You can use that.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If your array is sorted, you could use the Arrays.binarySearch(char[], char) method
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My suggestion would be to put the characters in a Set (e.g. HashSet). Then when you iterate through your string, you can use set.contains(c) to check if it's there or not.

(Any collection will work, but HashSets are particularly fast for checking whether something is in it, if you're not bothered about the order).

 
Josh Theisen
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think that the set/collection will work -- thanks.

Another question, even though it's not directly related ... say I'm going through this string character by character...
In this string there are numbers. I'll also be using a stack... if I find numbers I'll push them on to the stack - however, when
I pop them off I want it to act as a single int/number.

For example, I come across a 1 - push. The next char is a 2 - push to stack. When I pop 2 and then 1, I want it to be the int 12. Is that possible?

 
dennis deems
Ranch Hand
Posts: 808
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Josh Theisen wrote:I think that the set/collection will work -- thanks.

Another question, even though it's not directly related ... say I'm going through this string character by character...
In this string there are numbers. I'll also be using a stack... if I find numbers I'll push them on to the stack - however, when
I pop them off I want it to act as a single int/number.

For example, I come across a 1 - push. The next char is a 2 - push to stack. When I pop 2 and then 1, I want it to be the int 12. Is that possible?



I suggest appending them to a StringBuilder/StringBuffer instead. Then you can just call sb.toString().
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Josh Theisen wrote:In this string there are numbers. I'll also be using a stack... if I find numbers I'll push them on to the stack - however, when
I pop them off I want it to act as a single int/number.

For example, I come across a 1 - push. The next char is a 2 - push to stack. When I pop 2 and then 1, I want it to be the int 12. Is that possible?

if your string is "123 456", are you planning on pushing 1,2,3,4,5,6 before popping anything off? if so, how will you know the difference between that and "12 34 56"?
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!