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 3 of 3

Thread: removing special characters from a string, and help finding an extra space in string output

  1. #1
    Member
    Join Date
    Feb 2013
    Location
    earth
    Posts
    88
    Thanks
    12
    Thanked 9 Times in 9 Posts

    Default removing special characters from a string, and help finding an extra space in string output

    d


  2. #2
    Junior Member
    Join Date
    Feb 2013
    Location
    Germany
    Posts
    27
    Thanks
    0
    Thanked 5 Times in 5 Posts

    Default Re: removing special characters from a string, and help finding an extra space in string output

    The array at position 2 (the a) has not a real char value. So you must either:

    	    // stepthrough the sentence array, get the first character
    	    // of each string @ each index, and store in firstLetters array
    	    for (int x = 0; x < sentence.length; x++)
    	    { 
    	      // only grab the 1st letter if word is > 1 character in length
    	      if (sentence[x].length() > 1)
    	      {
    	        firstLetters[x] = sentence[x].charAt(0);
    	      }else{
    	    	  firstLetters[x] = ' ';
    	      }
    	    }

    fill it with a default ' ' at the above position of your code or you must check against the int value of a char. In java a char is an int value, simply said

    	    // append the letter @ each index in firstLettersp[] array to the end of
    	    // each word @ ea. relative index in sentence[x] array
    	    for (int x = 0; x < sentence.length; x++)
    	    {
    	      if ( firstLetters[x] != 0)
    	      {
    	       sentence[x] = sentence[x] + firstLetters[x];
    	      }
    	    }

    Try to output (int) aChar for a better understanding of it. It's a kind of working with ascii-tables and so on.

  3. The Following User Says Thank You to janpiel For This Useful Post:

    javaiscool (March 28th, 2013)

  4. #3
    Member
    Join Date
    Sep 2012
    Posts
    128
    Thanks
    1
    Thanked 14 Times in 14 Posts

    Default Re: removing special characters from a string, and help finding an extra space in string output

    Looks like the error starts earlier:

    sentence[]: [i, have, a, dog]

    firstLetters array: [ , h, , d]


    The first letters are white space. You need to trim it at that point.

Similar Threads

  1. Trouble with getting middle characters from a string.
    By doonar15 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 24th, 2013, 06:41 PM
  2. Double to string giving extra values
    By sinsand in forum Collections and Generics
    Replies: 1
    Last Post: August 23rd, 2011, 05:57 AM
  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. Finding frequency and probability of characters in a string
    By Aberforth in forum What's Wrong With My Code?
    Replies: 4
    Last Post: October 31st, 2010, 02:02 AM
  5. How to check whether the string contains only the specified characters ????
    By j_kathiresan in forum Java Theory & Questions
    Replies: 3
    Last Post: April 30th, 2010, 08:49 AM