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.

Results 1 to 8 of 8

Thread: Go through string one char at a time

  1. #1
    Junior Member
    Join Date
    Oct 2010
    Posts
    27
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Go through string one char at a time

    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


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Go through string one char at a time

    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!

  3. #3
    Junior Member
    Join Date
    Oct 2010
    Posts
    27
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Go through string one char at a time

    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.

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Go through string one char 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!

  5. #5
    Junior Member
    Join Date
    Oct 2010
    Posts
    27
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Go through string one char at a time

    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.

    if (string.charAt(position) == ( '(' || '<' || '{' || '[' )) {
    blah blah 
    }
    I got that statement to check one char... but apparently I can't use the "or" operator.
    What am I doing wrong?

  6. #6
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: Go through string one char at a time

    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

  7. The Following User Says Thank You to newbie For This Useful Post:

    joshft91 (April 17th, 2011)

  8. #7
    Junior Member
    Join Date
    Oct 2010
    Posts
    27
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Go through string one char at a time

    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

  9. #8
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Go through string one char at a time

    An easier solution.
    if ("(<{[".indexOf(string.charAt(position)) >= 0) {
        blah blah 
    }

Similar Threads

  1. Char array to a String array with hex
    By fortune2k in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 20th, 2014, 01:01 PM
  2. Char to String - Gender method.
    By ibby50 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: November 2nd, 2012, 12:42 PM
  3. [SOLVED] String Matcher finding only char not a whole string
    By Kakashi in forum What's Wrong With My Code?
    Replies: 11
    Last Post: February 18th, 2011, 09:58 AM
  4. Convert CHAR to STRING
    By fh84 in forum What's Wrong With My Code?
    Replies: 11
    Last Post: October 29th, 2009, 09:21 PM
  5. Need help with concatenizing char? to string?
    By bh-chobo in forum Java SE APIs
    Replies: 3
    Last Post: October 16th, 2009, 08:40 AM