Results 1 to 7 of 7
  1. #1
    abhi7080 is offline Member
    Join Date
    Dec 2011
    Location
    india
    Posts
    16
    Rep Power
    0

    Default how to get last two words from string

    I have done following progrramm to get last two words from string but i am succesfull in geting one word only.
    can somebody help me for geting last two words from string.

    here is my code and it geting one exception also at run time that shown below .
    Java Code:
    import java.util.*;
    public class wordcount {
       public static void main (String[] args) 
       	{
       		Scanner sc= new Scanner(System.in);
       		 System.out.println("enter string");
            	              String str= sc.nextLine();   
    	     StringTokenizer tkn= new StringTokenizer(str," ");
       	     if(tkn.hasMoreTokens())                                       
       	     {
       	     int cnt= tkn.countTokens();  	
       	    System.out.println ("Shows all initials and last word of string  ");
       	     	int i=0 ;
       	     	  for(; ; )
       	     	  {
       	     	    	
       	     	String s=tkn.nextToken();
       	     	System.out.print (s.charAt(0)+ " " );
       	     	  	i++ ;                             
    
                         	   if(i==cnt-1)           
                      	    {
                       	   System.out.println (tkn.nextToken());
                       	
                                            }
                  	
                  	
                     	   
                                        }
                   
                    
       	       }  
       	 }
              }
    o/p is:
    enter string
    abhijit prakash bagal
    total no of words in a string are 3
    total no fo characters in a string are 19
    Shows all initials and last word of string
    a p bagal
    Exception in thread "main" java.util.NoSuchElementException
    at java.util.StringTokenizer.nextToken(StringTokenize r.java:332)
    at wordcount.main(wordcount.java:53)

    Process completed.
    Last edited by Norm; 01-15-2012 at 02:54 PM. Reason: added code tags

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: how to get last two words from string

    Exception in thread "main" java.util.NoSuchElementException
    at java.util.StringTokenizer.nextToken(StringTokenize r.java:332)
    Have you read the API doc for the nextToken() method to see why it throws that Exception?
    The Scanner class has methods for testing if there are more tokens before you try to read that next token.

    help me for geting last two words from string.
    To solve that you need to know when you have the last word.
    To get the last two words, you will need to always save the current word and the previous word.
    Last edited by Norm; 01-15-2012 at 02:56 PM.

  3. #3
    stchman's Avatar
    stchman is offline Member
    Join Date
    Apr 2011
    Location
    Saint Louis, Missouri
    Posts
    97
    Rep Power
    0

    Default Re: how to get last two words from string

    I would look at the split( regex ) in the String class. I think it would make life a lot easier for you.
    If you aren't programming in Java, well that's just too bad.
    I'd rather be using Ubuntu.

  4. #4
    abhi7080 is offline Member
    Join Date
    Dec 2011
    Location
    india
    Posts
    16
    Rep Power
    0

    Default Re: how to get last two words from string

    Quote Originally Posted by Norm View Post
    Have you read the API doc for the nextToken() method to see why it throws that Exception?
    The Scanner class has methods for testing if there are more tokens before you try to read that next token.


    To solve that you need to know when you have the last word.
    To get the last two words, you will need to always save the current word and the previous word.
    I didnt get answer yet. Please tell me what to do?

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: how to get last two words from string

    Explain the steps you need to take to solve the problem.
    To get the last two words, you will need to save the previous word you looked at. When you are looking at the last word, then that last word and the previously saved word are the last two words

  6. #6
    abhi7080 is offline Member
    Join Date
    Dec 2011
    Location
    india
    Posts
    16
    Rep Power
    0

    Default Re: how to get last two words from string

    Can You add that single code to my given program that will display the last tow words and remove the exception please.

  7. #7
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,457
    Rep Power
    20

    Default Re: how to get last two words from string

    Um, this is a forum -- not a code mill. Show your best efforts in the form of an SSCCE (Short, Self Contained, Compilable and Executable) example and tell us what's wrong with it.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Flag and words in one string
    By NetJava in forum New To Java
    Replies: 0
    Last Post: 11-20-2011, 06:44 PM
  2. Reverse words of string.
    By Vikash Kumar Singh in forum Advanced Java
    Replies: 6
    Last Post: 08-07-2011, 01:28 PM
  3. count no of words in string
    By fakepics500 in forum New To Java
    Replies: 12
    Last Post: 07-19-2011, 11:49 AM
  4. Removing words from a String
    By nn12 in forum New To Java
    Replies: 3
    Last Post: 03-15-2011, 11:50 AM
  5. Analyze a string of words
    By zoe in forum Advanced Java
    Replies: 2
    Last Post: 07-26-2007, 11:01 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •