Hi,
Just a small excerise for me.
A program that counts number of words in a string.
The program which i have written now is - -
import java.io.*;
-
class countword
-
{
-
private static BufferedReader stdin = new BufferedReader(new InputStreamReader( System.in ));
-
-
public static void main(String arg[]) throws IOException {
-
System.out.println("Enter a string: ");
-
String input = stdin.readLine();
-
-
String[] arr = input.split(" ");
-
int length = arr.length;
-
System.out.println ("Lenght of string: "+input+" is:"+length);
-
}
-
}
-
But this counts even extra blank spaces. Like this string "I am new to JAVA",
or " I am new to java" should count only as 5 words, but the result shows more than that.
Any suggestions to change the logic?
Cheers,
Kishore
12 55893
Hi,
Just a small excerise for me.
A program that counts number of words in a string.
The program which i have written now is -
import java.io.*;
class countword
{
private static BufferedReader stdin = new BufferedReader(new InputStreamReader( System.in ));
public static void main(String arg[]) throws IOException {
System.out.println("Enter a string: ");
String input = stdin.readLine();
String[] arr = input.split(" ");
int length = arr.length;
System.out.println ("Lenght of string: "+input+" is:"+length);
}
}
But this counts even extra blank spaces. Like this string "I am new to JAVA",
or " I am new to java" should count only as 5 words, but the result shows more than that.
Any suggestions to change the logic?
Cheers,
Kishore
From - String[] arr = input.split(" ");
then you do - int count = 0;
-
for(int i = 0; i <arr.length;i++) {
-
if(arr[i].equals(" ")) {
-
}
-
else {
-
count++;
-
}
-
}
-
and
hey,
that was a very quick reply!! Really unexpected and great ;-)
it works fine.
Thanks a lot!
Cheers,
Kishore
hey,
that was a very quick reply!! Really unexpected and great ;-)
it works fine.
Thanks a lot!
Cheers,
Kishore
Anytime Kishore. Just remember to use code tags next time you post your code either in helping others or in asking more questions
Anytime Kishore. Just remember to use code tags next time you post your code either in helping others or in asking more questions
I am a newbie to this forum, but i get what u meant :-)
hey, is there any other method WITHOUT USING SPLIT() for this program?
I am a newbie to this forum, but i get what u meant :-)
hey, is there any other method WITHOUT USING SPLIT() for this program?
Combination of split and a regular expression is the best approach. The tokenizer route is becoming archaic
Combination of split and a regular expression is the best approach. The tokenizer route is becoming archaic
No clue how to go about it...HELP!!!
No clue how to go about it...HELP!!!
You'd need to go through a regular expression tutorial first. One is found here
Looks like the solution is instead to use input.split("\\s+") to get the count instead of input.split(" ") and then just output the length.
"one&two#three|four" will return as 4 words.
"I'm here" 3 words and so on.
Just didn't think " " is enough to separate words. - public static int countWords(String s) {
-
int counter = 0;
-
boolean word = false;
-
int endOfLine = s.length()-1;
-
-
for (int i = 0; i < s.length(); i++) {
-
//if the char is letter, word = true.
-
if (Character.isLetter(s.charAt(i)) == true && i != endOfLine) {
-
word = true;
-
//if char isnt letter and there have been letters before (word == true), counter goes up.
-
} else if (Character.isLetter(s.charAt(i)) == false && word == true) {
-
counter++;
-
word = false;
-
//last word of String, if it doesnt end with nonLetter it wouldnt count without this.
-
} else if (Character.isLetter(s.charAt(i)) && i == endOfLine) {
-
counter++;
-
}
-
}
-
return counter;
-
}
Try splitting with Regular Expressions instead of using " " as delimiter: - String[] arr = input.split("\\s+");
-
int length = arr.length;
That code will split String on any space character (space \n \r or \t) (if there is more characters in a row it will split only once).
Could use the following code:
import java.io.*;
public class Countstringwords {
private static BufferedReader stdin = new BufferedReader(new InputStreamReader( System.in ));
public static void main(String arg[]) throws IOException {
System.out.println("Enter a string: ");
String input = stdin.readLine();
int i,l = 0;
String[] arr = input.split(" ");
int length = arr.length;
for(i=0;i<length;i++){
System.out.println(arr[i]);
if(!"".equals(arr[i])){l++;}
}
System.out.println ("Lenght of string: "+input+" is:"+l);
} }
can u explain that.,please.,i need explanation.,tnx
Sign in to post your reply or Sign up for a free account.
Similar topics
by: vic |
last post by:
Hello, Dean
Try this:
select distinct c1, c2 into #tmp_1 from t1
select count(*) as cnt from #tmp_1
drop table #tmp_1
With best regards.
|
by: DataFreakFromUtah |
last post by:
Hello!
No question here, just a procedure for the archive.
Search critera:
count records imported count data imported count number of rows
imported
count number of records imported record import...
|
by: tranky |
last post by:
hi,
only one information,please...
how to count number of anonymous online users?
I use ASP.NET 2.0
thank u
tranky
|
by: Mack |
last post by:
Hi all,
I want to write a program to count number of bits set in a number.
The condition is we should not loop through each bit to find whether
its set or not.
Thanks in advance,
-Mukesh
|
by: supriyanaidu |
last post by:
Hi
i am new to this forum . i am doing small project in that i want to count the number of words in the given .for example in my give file the content will be in this format
Firstname, last...
|
by: mfaisalwarraich |
last post by:
Hi Everybody,
I am using the following code to get the recordset of an external database.
Dim dbPatients As Database
Dim rsCountPatients As Recordset ' to count number of...
|
by: jambonjamasb |
last post by:
I am wanting to create a report that summarises the number of items within a date range.
For example I have a FIELD called System_Change.
This is a drop down COMBOBOX that uses words like unix,...
|
by: jlt206 |
last post by:
This code <?php include("counter.php")?> on the webpage produces the count number. (function code below)
I want to place the current number into a variable $MemberNo or into a FormField to be sent...
|
by: cathrine babe |
last post by:
How To Count Number Of Words In A Sentence
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
| |