Results 1 to 9 of 9
  1. #1
    lclclc is offline Member
    Join Date
    Aug 2009
    Posts
    22
    Rep Power
    0

    Default How to read a letter instead of a character from a given sentences

    May I know how to do with the String to read a letter from a given sentences?
    I can do it only with just read 20 character from a given senteces.
    Java Code:
    sentence.substring(0,21);
    But, how do I do with reading a letter?
    I try with substring but cannot work
    below is the try code.
    Java Code:
    sentence.substring(0,"  ");
    I put this " " to represent space, but it cannot take this parameter, what should I use instead?

    Thank you for the help.

  2. #2
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    By letter do you mean a word? Do you want to split a string using spaces?
    If that is the case then read the API specs for the String.split method.

  3. #3
    Addez is offline Senior Member
    Join Date
    Aug 2009
    Posts
    294
    Rep Power
    0

    Default

    sentence.split(" "); //splits at spaces

  4. #4
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    843
    Rep Power
    6

    Default

    Ur question is not clear.

    But,What I understood is that u want to read character know? Then u can go for this below method

    charAt
    public char charAt(int index)Returns the character at the specified index. An index ranges from 0 to length() - 1. The first character of the sequence is at index 0, the next at index 1, and so on, as for array indexing.

    Specified by:
    charAt in interface CharSequence
    Parameters:
    index - the index of the character.
    Returns:
    the character at the specified index of this string. The first character is at index 0.
    Throws:
    IndexOutOfBoundsException - if the index argument is negative or not less than the length of this string.
    Ramya:cool:

  5. #5
    lclclc is offline Member
    Join Date
    Aug 2009
    Posts
    22
    Rep Power
    0

    Default

    The question is ask me to print a string composed of first letter of the sentence, followed by printing first five characters of the sentence.
    I can do with printing first five characters of sentence by using the code sentence.substring(0,6).
    However, I do not know how to do with the first letter of the sentence.
    I try the guide from other two replier, but I can't do it
    sentence.split(" ");
    is an error.
    Still seeking for help.

  6. #6
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    843
    Rep Power
    6

    Default

    Could u please explain with example input and output u want.........Still not clear
    Ramya:cool:

  7. #7
    lclclc is offline Member
    Join Date
    Aug 2009
    Posts
    22
    Rep Power
    0

    Default

    Mickey Mouse is cute.
    //to print first 5 characters of this sentence.
    sentence.substring(0,6)
    output: Micke

    //to print first letter of this sentence.
    output: Mickey
    how should I do this?
    using this sentence.split(" "); I cannot get any output come out.
    How should I correct this code?

  8. #8
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    You should have done as suggested above and actually read the API specs for the String.split method.

    Do you know what it does and what it returns if you do sentence.split(" ")?

    P.S Mickey Mouse is not cute.

  9. #9
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    843
    Rep Power
    6

    Default

    Run this code below for knowing how split works.

    It just split the line by whatever delimiter u are giving and stores into array.


    Run this code below

    Java Code:
    class Test 
    {
    	public static void main(String[] args) 
    	{
    		String s = "Mickey Mouse is cute";
    		String outputArr[] = s.split(" "); 
    
    		for(int i=0; i < outputArr.length; i++)
    		System.out.println(outputArr[i]);
    	}
    }
    Ramya:cool:

Similar Threads

  1. Read from a certain character to a certain character
    By blackstormattack in forum New To Java
    Replies: 0
    Last Post: 03-16-2009, 12:36 PM
  2. Read character from Image area
    By sundarjothi in forum Advanced Java
    Replies: 2
    Last Post: 01-12-2009, 06:00 AM
  3. Replies: 1
    Last Post: 02-04-2008, 09:26 PM
  4. Creating random sentences
    By bluekswing in forum New To Java
    Replies: 4
    Last Post: 06-27-2007, 06:45 PM
  5. vars and if sentences in XSL-FO
    By Alan in forum XML
    Replies: 1
    Last Post: 05-31-2007, 03:24 PM

Posting Permissions

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