Results 1 to 13 of 13
Thread: count no of words in string
- 07-18-2011, 07:00 PM #1
Member
- Join Date
- Jul 2011
- Posts
- 43
- Rep Power
- 0
- 07-18-2011, 07:12 PM #2
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Look at how to use the string method split(...)
- 07-18-2011, 07:36 PM #3
Member
- Join Date
- Jul 2011
- Posts
- 43
- Rep Power
- 0
thanks for your advice
but i want to make String array size dyanamicJava 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); } }
i want to remove all the commos and fullstop and al lpuntations sign to be removed before it storedJava Code:String arr[]= new String[25];
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
- 07-18-2011, 08:04 PM #4
Arrays must be given a fixed size when they are defined.i want to make String array size dyanamic
There are classes that work like arrays that are dynamic: ArrayList
The String split method will do that.want to remove all the commos and fullstop
- 07-18-2011, 08:17 PM #5
Member
- Join Date
- Jul 2011
- Posts
- 43
- Rep Power
- 0
- 07-18-2011, 08:19 PM #6
Yes, it will do something, not sure.will substring method do it
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
- 07-18-2011, 08:48 PM #7
Member
- Join Date
- Jul 2011
- Posts
- 43
- Rep Power
- 0
it worked thanks norm
.gif)
i want to calculate time taken by user to enter each wordJava 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); } }
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
give me some clue to do this programJava 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"); } }
- 07-18-2011, 08:51 PM #8
See the System class's currentTimeMillis method to capture the current time.want to make time taken by user to enter each word
- 07-18-2011, 08:58 PM #9
Member
- Join Date
- Jul 2011
- Posts
- 43
- Rep Power
- 0
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)See the System class's currentTimeMillis method to capture the current time.
- 07-18-2011, 09:05 PM #10
If you get the time before a task and the time after the task, then you can subtract to get ...
- 07-19-2011, 06:43 AM #11
Member
- Join Date
- Jul 2011
- Posts
- 43
- Rep Power
- 0
- 07-19-2011, 07:32 AM #12
You can't. Why on earth do you need that?
- 07-19-2011, 10:49 AM #13
Member
- Join Date
- Jul 2011
- Posts
- 43
- Rep Power
- 0
Similar Threads
-
Count Words
By jjjkkk in forum New To JavaReplies: 8Last Post: 07-18-2011, 01:24 PM -
Read text file and count words.
By chdn202002 in forum New To JavaReplies: 5Last Post: 07-17-2011, 02:44 PM -
Removing words from a String
By nn12 in forum New To JavaReplies: 3Last Post: 03-15-2011, 10:50 AM -
[SOLVED] How to count the number of words in a string
By andy5605 in forum New To JavaReplies: 8Last Post: 02-04-2009, 08:55 PM -
Printing out ... depending on character count of previous words entered.
By TheRocket in forum New To JavaReplies: 5Last Post: 11-22-2008, 01:34 PM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks