Results 1 to 13 of 13
Like Tree1Likes
  • 1 Post By sunde887

Thread: count no of words in string

  1. #1
    fakepics500 is offline Member
    Join Date
    Jul 2011
    Posts
    43
    Rep Power
    0

    Default count no of words in string

    i want to count the no words in a String

    e.g
    I am the best of all
    output:
    6

    i am not getting how to make this work,plzzzz give some idea

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Look at how to use the string method split(...)
    fakepics500 likes this.

  3. #3
    fakepics500 is offline Member
    Join Date
    Jul 2011
    Posts
    43
    Rep Power
    0

    Default

    thanks for your advice

    Java Code:
    import java.io.*;
    
    class wor
    {
    	public static void main(String ar[]) throws IOException
    	{
    		
    		DataInputStream d= new DataInputStream(System.in);
    		System.out.println("enter the string");
    		String s=d.readLine();
    		int words=0;
    		String arr[]= new String[25];
    		arr=s.split(" ");
    			
    		System.out.println("no of words in the line are"+arr.length);
    	}
    }
    but i want to make String array size dyanamic
    Java Code:
    String arr[]= new String[25];
    i want to remove all the commos and fullstop and al lpuntations sign to be removed before it stored
    e.g i like you, i am sorry.
    it should be stored as
    arr[]={"i","like","you","i","am","sorry"}
    i.e by removing cooma and full stop

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,390
    Rep Power
    25

    Default

    i want to make String array size dyanamic
    Arrays must be given a fixed size when they are defined.
    There are classes that work like arrays that are dynamic: ArrayList

    want to remove all the commos and fullstop
    The String split method will do that.

  5. #5
    fakepics500 is offline Member
    Join Date
    Jul 2011
    Posts
    43
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post

    The String split method will do that.
    will substring method do it
    Java Code:
    for(int i=0;i<arr.length;i++)
    {
           arr[i]=arr[i].subString(0,arr[i].lenght());
    }

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,390
    Rep Power
    25

    Default

    will substring method do it
    Yes, it will do something, not sure.
    Why don't you write a short simple program and test it and see what you get.

    You can use:
    System.out.println(java.util.Arrays.toString(arr)) ; // to show contents of array

  7. #7
    fakepics500 is offline Member
    Join Date
    Jul 2011
    Posts
    43
    Rep Power
    0

    Default

    it worked thanks norm

    Java Code:
    import java.io.*;
    
    class wor
    {
    	public static void main(String ar[]) throws IOException
    	{
    		
    		DataInputStream d= new DataInputStream(System.in);
    		System.out.println("enter the string");
    		String s=d.readLine();
    		int words=0;
    		String arr[]= new String[25];
    		arr=s.split(" ");
    			//System.out.println(java.util.Arrays.toString(arr)) ;
    		for(int i=0;i<arr.length;i++)
    		{
    				int j=arr[i].length();
    				if((arr[i].contains(","))||(arr[i].contains(".")))
        				   arr[i]=arr[i].substring(0,j-1);
    		}
    		//System.out.println(java.util.Arrays.toString(arr)) ;
    		System.out.println("no of words in the line are"+arr.length);
    	}
    }
    i want to calculate time taken by user to enter each word
    eg: he is great
    output
    he 30
    is 20
    great 60

    i made a code which use to calculate time taken by user to enter a line but now want to make time taken by user to enter each word

    Java Code:
    import java.io.*;
    import java.util.*;
    class type
    {
    	public static void main(String ar[]) throws IOException
    	{
    		Scanner s= new Scanner(System.in);
    		DataInputStream d= new DataInputStream(System.in);
    		int no;
    		String str;
    		System.out.println("\n\n\n\n\t\t\t\t\tTypeing speed contest\n\n\n\n");
    		System.out.println("type the below given line\n\n i love you and i will kill you");
    		System.out.println("when your are ready press 1");
    		no=s.nextInt();
    		
    		Date d1 = new Date();
    		long l1=d1.getTime();
    		System.out.println("time 1="+l1);
    		str=d.readLine();
    		Date d2 = new Date();
    		long l2=d2.getTime();
    		System.out.println("time 2="+l2);
    		double time= (double)l2-l1;
    		System.out.println("time ="+time);
    		time=time/100;
    		time=time/60;	
    		System.out.println("time ="+time);
    		double len=str.length();
    		double speed=len/time;
    		System.out.println("your typing speed is="+(long)speed+"charaters/min");
    	}
    }
    give me some clue to do this program

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,390
    Rep Power
    25

    Default

    want to make time taken by user to enter each word
    See the System class's currentTimeMillis method to capture the current time.

  9. #9
    fakepics500 is offline Member
    Join Date
    Jul 2011
    Posts
    43
    Rep Power
    0

    Default

    See the System class's currentTimeMillis method to capture the current time.
    i not getting clue how to use System class's currentTimeMillis or Date class getTime because i need to get time after every word is entered(i.e after every space and carrage return)

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,390
    Rep Power
    25

    Default

    If you get the time before a task and the time after the task, then you can subtract to get ...

  11. #11
    fakepics500 is offline Member
    Join Date
    Jul 2011
    Posts
    43
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    If you get the time before a task and the time after the task, then you can subtract to get ...
    yes but i how will i take before every word is entered as i am reading a line not words

  12. #12
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    You can't. Why on earth do you need that?

  13. #13
    fakepics500 is offline Member
    Join Date
    Jul 2011
    Posts
    43
    Rep Power
    0

    Default

    Quote Originally Posted by Junky View Post
    You can't. Why on earth do you need that?
    thanks for the reply

Similar Threads

  1. Count Words
    By jjjkkk in forum New To Java
    Replies: 8
    Last Post: 07-18-2011, 01:24 PM
  2. Read text file and count words.
    By chdn202002 in forum New To Java
    Replies: 5
    Last Post: 07-17-2011, 02:44 PM
  3. Removing words from a String
    By nn12 in forum New To Java
    Replies: 3
    Last Post: 03-15-2011, 10:50 AM
  4. Replies: 8
    Last Post: 02-04-2009, 08:55 PM
  5. Replies: 5
    Last Post: 11-22-2008, 01:34 PM

Posting Permissions

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