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
Welcome to the Java Programming Forums
The professional, friendly Java community. 21,500 members and growing!
The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.
>> REGISTER NOW TO START POSTING
Members have full access to the forums. Advertisements are removed for registered users.
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)
Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
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?
Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
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.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
Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code
joshft91 (April 17th, 2011)
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.
if ("(<{[".indexOf(string.charAt(position)) >= 0) { blah blah }