Hey guys - so I need to write a method that will take a String as a parameter, and then I'll need it to go through that string one character at a time.
What would be the best way to do this?
Thanks
Printable View
Hey guys - so I need to write a method that will take a String as a parameter, and then I'll need it to go through that string one character at a time.
What would be the best way to do this?
Thanks
Recommended reading: String (Java Platform SE 6)
Would it be possible to use substrings? And somehow go through it ...
What I need it to do is if it finds a certain character, to put it into a stack. I have everything else added for stacks and stuff, but I'm not sure how to make it grab one character at a time.
What happened when you tried it using substrings?
Alright - I decided not to use substrings and decided to use the charAt method.
I'm getting somewhere on it ... but I've run in to sort of a problem. I need to check to see if a string contains certain characters ... I was able to make it check one fine, but when I tried to add multiple, I got an error. Here's the if statement I need to get to work.
I got that statement to check one char... but apparently I can't use the "or" operator.Code :if (string.charAt(position) == ( '(' || '<' || '{' || '[' )) { blah blah }
What am I doing wrong?
To use the 'or' operator properly, you must specify each condition explicitly.
The way you're doing it is: IF X == Y || Z
The correct way is to do the following: IF X == Y || X == Z
Thanks for the reply, newbie - ended up helping me a bit. Decided it was easier to put the string into an array of chars and to go from there.
/solved
An easier solution.
Code java:if ("(<{[".indexOf(string.charAt(position)) >= 0) { blah blah }