d
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.
d
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.
javaiscool (March 28th, 2013)
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.