Results 1 to 11 of 11
  1. #1
    DreamNaut is offline Member
    Join Date
    Oct 2010
    Posts
    12
    Rep Power
    0

    Default Substring indexing

    Hello I am writing a method that adds letters to a string. The letters added depends on whether the string begins with a vowel and ends with a consonant.

    I can check if the string starts with a vowel, because the substring index number is always (0,1). How would I get the substring index number of the last letter since I don't know what it will be (user input)?







    public static String convertToken(String original)
    {
    String initial=" ";
    String consonants="bcdfghjklmnpqrstvwxyz";
    String vowels = "aeiou";
    String vowelRule = "a";
    String consonantRule = "us";
    String endVowelRule="ta";
    String endConsonantRule="tus";
    if((vowels.indexOf(original.substring(0, 1))> -1)&&(consonants.lastIndexOf(original.substring(??? ))> ??)){
    initial+=original;
    initial+=vowelRule;
    }
    else{

    System.out.println("test");

    }
    return initial.toString();
    }
    Last edited by DreamNaut; 11-26-2010 at 08:00 PM.

  2. #2
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    5

    Default

    substring(original.length()-1) ??

  3. #3
    DreamNaut is offline Member
    Join Date
    Oct 2010
    Posts
    12
    Rep Power
    0

    Default

    i tried that but i didn't include the (), doh. ill try it again thanks

  4. #4
    DreamNaut is offline Member
    Join Date
    Oct 2010
    Posts
    12
    Rep Power
    0

    Default

    that didnt work, i also tried

    if((vowels.indexOf(original.substring(0, 1))> -1)&&(vowels.lastIndexOf(original.length()-1)== -1))

    which from what i understand if the lastIndexOf method can't find the index position it returns -1... but the program won't catch it, it simply ignores the second statement (or maybe it follows it? lol).

  5. #5
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    that didnt work

    It would be helpful if you posted a short runnable example of what you tried and a description of the output and what you expected or intended.

  6. #6
    DreamNaut is offline Member
    Join Date
    Oct 2010
    Posts
    12
    Rep Power
    0

    Default

    import java.util.StringTokenizer;

    class addLetters
    {
    public static void main(String[] args)
    {

    //User inputs string here

    String sentence = "Eager age";

    System.out.println(convertString(sentence.toLowerC ase()));
    }

    public static String convertString(String input){
    String initial="";
    String token;
    StringTokenizer st=new StringTokenizer(input);

    while (st.hasMoreTokens()) {

    token=st.nextToken();
    initial+=convertToken(token);
    }

    return initial.toString();

    }
    public static String convertToken(String original)
    {
    String initial=" ";
    String vowels = "aeiou";
    String vowelRule = "-a";
    //checks for words beginning with a vowel and ending with a consonant

    if((vowels.indexOf(original.substring(0,1))> -1)&&(vowels.lastIndexOf(original.length()-1)== -1)){
    initial+=original;
    initial+=vowelRule;
    }
    else{

    System.out.println("Did not meet the requirements.");

    } return initial.toString();
    }

    }


    The program is designed to read a sentence with no punctuation and a single space between each word.

    For every word that starts with a vowel and ends with a consonant, the letter "a" should be added to the word.

    Expected output: "eager-a" "Does not meet the requirements."

    Actual output: "eager-a" "age-a"

    In short the program is not checking to make sure the last character is a consonant.

    Any help is appreciated!
    Last edited by DreamNaut; 11-27-2010 at 01:39 AM.

  7. #7
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Java Code:
    if((vowels.indexOf(original.substring(0,1))> -1)&&(vowels.lastIndexOf(original.length()-1)== -1)){

    Shouldn't the second half of that condition be:

    Java Code:
    vowels.indexOf(original.substring(original.length()-1))== -1

  8. #8
    DreamNaut is offline Member
    Join Date
    Oct 2010
    Posts
    12
    Rep Power
    0

    Default

    Doh!!! thanks so much, its working now

  9. #9
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    You're welcome.

  10. #10
    lolagreenn is offline Member
    Join Date
    Jan 2011
    Location
    US
    Posts
    1
    Rep Power
    0

    Default Maddona radio star

    Hei, I've merely discovered that Madonna has appeared inside the united kingdom papers more then any celeb in the last ten years. That is a thing extremely interesting. Being a truly super star she is played out on all radio stations worldwhide.

  11. #11
    goldest's Avatar
    goldest is offline Senior Member
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    469
    Rep Power
    5

    Wink

    This is not Madonna fan club.

    Go away spammer! :mad:
    Java Is A Funny Language... Really!
    Click on * and add to member reputation, if you find their advices/solutions effective.

Similar Threads

  1. re-indexing particular fields
    By caballo7 in forum Lucene
    Replies: 0
    Last Post: 06-08-2010, 06:46 AM
  2. Indexing XML using lucene
    By peliukasss in forum Lucene
    Replies: 0
    Last Post: 03-28-2010, 10:20 PM
  3. file indexing
    By vislawath.jawaharlal in forum Threads and Synchronization
    Replies: 0
    Last Post: 04-07-2009, 01:15 PM
  4. Lucene Re-Indexing
    By connect2srinath in forum Lucene
    Replies: 1
    Last Post: 05-11-2008, 05:35 PM
  5. Indexing starting with 1
    By ravian in forum New To Java
    Replies: 4
    Last Post: 01-04-2008, 12:03 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
  •