The Artima Developer Community
Sponsored Link

Legacy Java Answers Forum
November 2001

Advertisement

Advertisement

This page contains an archived post to the Java Answers Forum made prior to February 25, 2002. If you wish to participate in discussions, please visit the new Artima Forums.

Message:

Program impossible

Posted by Distressed Student on November 26, 2001 at 8:17 AM

I have been asked the following question to solve can anyone help!!

Assuming that a sentence consists of a sequence of words separated by single spaces and terminated by a full stop, write and implement a Java program that will read in a sentence from the keyboard and will print out each word on a separate line together with the number of letters in each word. For example:

INPUT
Mary had a little lamb.

OUTPUT -
Mary 4
had 3
a 1
little 6
lamb 4

The sentence should be short enough to be entered on one line of input. It should be treated as a sequence of characters, which may be read in one at a time. Each character must be either a letter or a space or a full stop. The program will not start reading the characters until the sentence is terminated and the ENTER key is pressed. The first digit of the number of letters in each word should be vertically aligned and you may assume that no word will be longer than 20 characters.

I tried to get a solution myself but if you read in it as character you can't read the length of each word that is to be returned. If anybody has a solution please respond as I am in great difficulty here!!

Thanks
A distressed programming student.



Replies:

Sponsored Links



Google
  Web Artima.com   
Copyright © 1996-2009 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us