The Artima Developer Community
Sponsored Link

Java Answers Forum
java program

1 reply. Most recent reply: Nov 13, 2002 9:30 AM by Charles Bell

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a flat view of this topic  Flat View
Previous Topic   Next Topic
Threaded View: This topic has 1 reply on 1 page
Marco

Posts: 1
Nickname: ger
Registered: Nov, 2002

java program Posted: Nov 13, 2002 5:58 AM
Reply to this message Reply
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

Could someone write out this entire program in java?


Re: java program Posted: Nov 13, 2002 9:30 AM
Reply to this message Reply
Posted by: Charles Bell    Posts: 519 / Nickname: charles / Registered: Feb, 2002
import java.util.*;
import java.io.*;
 
public class Test{
	
	public static void main(String[] args){
		Test test = new Test();
		test.test();
		System.exit(0);
	}
	
 
	
	public void test(){
		try{
			BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
			System.out.println("INPUT");
			String lineRead = br.readLine();
			StringTokenizer st = new StringTokenizer(lineRead, " .");
			System.out.println("OUTPUT - ");
			while (st.hasMoreTokens()){
				String nextToken = st.nextToken();
				System.out.println(nextToken + " " + nextToken.length());
			}			
		}catch(IOException ioe){
			System.err.println("IOException" + ioe.getMessage());
		}
		
	}
}


Topic: String Tokenizer Output Previous Topic   Next Topic Topic: Need one-2one ON line tutorial. willing to pay ?50.00 for each task

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use