Results 1 to 4 of 4
  1. #1
    Join Date
    Aug 2008
    Location
    kolkata
    Posts
    21
    Rep Power
    0

    Exclamation how to get the characters one by one from a String?

    I'm a newbie, I was learning the "String" & got stuck on a question i.e how to get the characters one by one from a String?
    Actually I had a program the will take one sentence from the user. That will give a output-
    The no of consonants, vowels, digits on that line.
    I went on like this...
    code:
    import java.util.*;
    class TestSentence{
    public static void main(String[] args)
    {
    Scanner s=new Scanner(System.in);
    System.out.println("Enter any Sentence...:");
    String s1=s.nextLine();
    //
    ...
    }
    now I've that String...but can't read one by one characters from that as we can do from the array by their index....
    So, if you tell me how to do this I'll be grateful to you.........

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Java Code:
    public class Test {
        public static void main(String[] args) {
            String s = "hello world";
            System.out.println("s = " + s);
            char[] chars = s.toCharArray();
            System.out.printf("chars[%d] = %s%n", 3, chars[3]);
            String fourth = String.valueOf(chars[3]);
            String sixth = s.substring(6, 7);
            System.out.printf("fourth = %s  sixth = %s%n",
                               fourth, sixth);
        }
    }

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,423
    Rep Power
    25

    Default

    Also look at String.charAt() and StringBuffer class

  4. #4
    Join Date
    Aug 2008
    Location
    kolkata
    Posts
    21
    Rep Power
    0

Similar Threads

  1. characters + strings
    By Gilgamesh in forum New To Java
    Replies: 3
    Last Post: 03-02-2008, 09:10 PM
  2. Replies: 3
    Last Post: 12-17-2007, 02:35 AM
  3. Removing characters
    By kDude in forum New To Java
    Replies: 3
    Last Post: 12-03-2007, 02:38 AM
  4. special characters
    By ravian in forum New To Java
    Replies: 2
    Last Post: 11-16-2007, 01:28 PM
  5. Getting all characters in a String
    By Alayna in forum New To Java
    Replies: 2
    Last Post: 05-20-2007, 11:49 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •