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
Printable View
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
Look at how to use the string method split(...)
thanks for your advice
but i want to make String array size dyanamicCode: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 storedCode: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
Arrays must be given a fixed size when they are defined.Quote:
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.Quote:
want to remove all the commos and fullstop
Yes, it will do something, not sure.Quote:
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
it worked thanks norm :(y):
i want to calculate time taken by user to enter each wordCode: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 programCode: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");
}
}
See the System class's currentTimeMillis method to capture the current time.Quote:
want to make time taken by user to enter each word
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)Quote:
See the System class's currentTimeMillis method to capture the current time.
If you get the time before a task and the time after the task, then you can subtract to get ...
You can't. Why on earth do you need that?