Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Answers Viewqa Java-Interview-Questions how to count words in string using java

Have Programming Question? Ask it here!
 
 
 


ASHUTOSH PRADHAN
how to count words in string using java
3 Answer(s)      3 years and 8 months ago
Posted in : Java Interview Questions


how to count words in string using corejava


Advertisement
View Answers

January 8, 2011 at 3:48 PM


Hi Friend,

Try the following code:

import java.util.*;
class CountWords{
    public static void main(String[] args) 
    {
        Scanner input=new Scanner(System.in);
        System.out.print("Enter string: ");
        String st=input.nextLine();
        int count=0;
        StringTokenizer stk=new StringTokenizer(st," ");
        while(stk.hasMoreTokens()){
            String token=stk.nextToken();
            count++;
        }
        System.out.println("Number of words are: "+count);
    }
}

Thanks



January 8, 2011 at 3:50 PM


Hello,

Try this:

import java.util.*;
class CountWords{
    public static void main(String[] args) 
    {
        Scanner input=new Scanner(System.in);
        System.out.print("Enter string: ");
        String st=input.nextLine();
        int count=0;
        String arr[]=st.split(" ");
        System.out.println("Number of words are: "+arr.length);
    }
}

Thanks



July 26, 2011 at 12:46 AM


import java.util.*;
import java.io.*;
class Tokensex
{
    public static void main(String [] args)throws Exception
    {
        InputStream is= System.in;
        InputStreamReader ir=new InputStreamReader(is);
        BufferedReader br= new BufferedReader(ir);
        char [] ch= new char[600];
        int i=br.read(ch,0,ch.length);
        String st1= new String(ch,0,ch.length);
        int count=0;
        StringTokenizer st=new StringTokenizer(st1);
        while(st.hasMoreTokens())
        {
            String token=st.nextToken(" ");
            count ++;
        }
        System.out.println("total no of words are:" + count);
    }
};


Related Tutorials/Questions & Answers:
how to count words in string using java
how to count words in string using java  how to count words in string...: "); String st=input.nextLine(); int count=0...()){ String token=stk.nextToken(); count
Count words in a string method?
Count words in a string method?  How do you Count words in a string... count the length of arr which return the number of words in the inputted string...=br.readLine(); int count=0; String arr[]=st.split
Advertisements
Java count frequency of words in the string
Java count frequency of words in the string. In this tutorial, you will learn how to count the occurrence of each word in the given string. String... of words in a string. In the given example, we have accepted a sentence
Count numbers of spaces and number of words from the input string
Count numbers of spaces and number of words from the input string Java In this section, you will learn how to count the number of spaces and words from... the number of spaces. Now, in order to count the number of words, we have splitted
Java reverse words in a string using only loops
Java reverse words in a string using only loops In this tutorial, you will learn how to reverse words in a string without using any inbuilt methods like...[start++]=c[end]; c[end--]=temp; } return c; } public static void main(String
Java Reverse words of the String
Reverse words of the String Java Programming In this section, we are going to reverse the words of the string. For this, we have allowed the user to enter... the string: java is a programming language Reversed Words: language
A Program To Reverse Words In String in Java .
A Program To Reverse Words In String in Java .  A Program To Reverse Words In String in Java :- For Example :- Input:- Computer Software Output :- Software Computer
A Program To Reverse Words In String in Java .
A Program To Reverse Words In String in Java .  A Program To Reverse Words In String in Java :- For Example :- Input:- Computer Software Output :- Software Computer
A Program To Reverse Words In String in Java .
A Program To Reverse Words In String in Java .  A Program To Reverse Words In String in Java :- For Example :- Input:- Computer Software Output :- Software Computer
A Program To Reverse Words In String in Java .
A Program To Reverse Words In String in Java .  A Program To Reverse Words In String in Java :- For Example :- Input:- Computer Software Output :- Software Computer
A Program To Reverse Words In String in Java .
A Program To Reverse Words In String in Java .  A Program To Reverse Words In String in Java :- For Example :- Input:- Computer Software Output :- Software Computer
Breaking a string into words without using StringTokenizer
Breaking a string into words without using StringTokenizer  how can we Break a string into words without using StringTokenizer ??   The given code convert the string into words. import java.util.*; class StringExample
A Program To Reverse Words In String in Java
A Program To Reverse Words In String in Java  A Program To Reverse Words In String in Java for example: Input:- Computer Software Output :- Software Computer without using split() , StringTokenizer function or any extra
Program to count the number of unique words in a file using HashMap
Program to count the number of unique words in a file using HashMap  ...*; public class CountUniqueWords { public static void main(String args....txt"); ArrayList arr=new ArrayList(); HashMap<String
java program to insert data into a file and count the number of words from the file???????
java program to insert data into a file and count the number of words from the file???????  java program to insert data into a file and count the number of words from the file
Java count words from file
Java count words from file In this section, you will learn how to determine the number of words present in the file. Explanation: Java has provides several... by using the StringTokenizer class, we can easily count the number of words
JavaScript Count Words
JavaScript Count Words In this section, you will learn how to count words... to enter words of their choice. The javascript takes the value of the text area and using the regular expression, determine the number of words and finally display
highlight words in an image using java
highlight words in an image using java  Hai all,In my application left side image is there and right side an application contains textboxes like... want to highlight name in the image using java/jsp/javascript.please help me
Breaking the String into Words
Breaking the String into Words       In this section, you will learn how to break the string into words. The java.util.*; package provides you a simple method to breaking the string
Find number of words begin with the specified character
Count number of words begin with the specified character In this section, you will learn how to count the number of words that begin with the specified... + "-" + count); } } Output: Enter string: Hi How are you
Exercise - Count Words
Java: Exercise - Count Words Problem Write a method which counts the number of words in a string. Assume that a word is defined as a sequence of letters... between words should only count if they include at least one blank so
JavaScript split string into words.
JavaScript split string into words.  How to split string into words using javascript?   <html> <head> <title><...; Description: The split() method is used to split a string into an array
Count Characters from the input string Java
Count Characters from the input string of Java In this section, you will learn how to count the occurrence of each character in the string. For this, we have prompted the user input the string. The BufferedReader class read
read string - using loops in Java
read string - using loops in Java  Write a program to read a string composed of an unknown number of words, then count the number of words in the string, and Display the longest and shortest words, with first letter Uppercase
Java Word Count - Word Count Example in Java
'numWords' is used to count the number of words. linecount(String fileName... Java Word Count - Word Count Example in Java  ... to count the number of lines, number of words and number of characters
Searching English words in a string
Searching English words in a string  My task is to find English words and separate them from a string with concatenated words..for example AhdgdjHOWAREgshshYOUshdhfh I need to find if there exists any English words.   
Exercise - Count Words
Java: Exercise - Count Words Problem Write a method which counts the number of words in a string. Assume that a word is defined as a sequence of letters... between words should only count if they include at least one blank so
Example: Count Bad Words
Java NotesExample: Count Bad Words Your grandmother has learned to program in Java, and one of the first things she wants to do is search thru the grandchildren's email to make sure they aren't using any "bad" words. She doesn't know
Java Count Vowels
Java Count Vowels       In this program you will learn how to count vowels in a String. Here you will be asked to enter a String of your own choice and then you will get
reverse words in a string(words seperated byone or more spaces)
reverse words in a string(words seperated byone or more spaces)  reverse words in a string(words separated by one or more spaces
Retrieve a list of words from a website and show a word count plus a specified number of most frequently occurring words
Retrieve a list of words from a website and show a word count plus a specified...; Integer count; // number of occurrences WordPair(String word... "words" form the document, and one by one, store each word as a key into a Map<
J2ME count character into string
J2ME count character into string  i am new in J2ME, my problem is how to count character into number, i had been research google for almost 2 days and got nothing...some one plz give me a punch :) as an example, 1. i got a text
Programming: Count Words - Dialog
Java NotesProgramming: Count Words - Dialog Description Write a program which counts the number of words in text the user enters. Assume that a word... error-prone1 Hints Blank counter. The number of words it the number
Programming: Count Words - Dialog
Java NotesProgramming: Count Words - Dialog Description Write a program... words, and so is "test this". Only count blanks if the last..., go back to ask for another line. Only count "words" that contain alphabetic
count the repeated character in one string
count the repeated character in one string  to find the how character occurrence in one String (for example the string is -java means there is 2... java.util.*; class CountCharacters { public static void main(String[] args) throws
Java: Example - Words to array
Java: Example - Words to array This example shows how to convert words to an array. We will use StringTokenizer to achieve the this.   Some times is is required to words into an array, to solve this you can use
How to convert into to String in Java?
How to convert into to String in Java?  Hi, How to convert into to String in Java? Thanks
How to Append String In Java?
How to Append String In Java?  How to write program in in Java for appending to a String? How to Append String In Java? What is the best way to do... the StringBuilder class. Check the example at Appending String in java. You can
Using Min, Max and Count on HQL.
Using Min, Max and Count on HQL.  How to use Min, Max and Count on HQL
Java count vowels
Java count vowels In this section you will learn how to count the number... will read the string once you enter the string. Then we have taken a count.... to console. Example  : A Code to count the number of vowel in a string
String length without using length() method in java
String length without using length() method in java  How to count length of string without using length() method in java?   Here is Example...); } } Output:- String length : 9 Description: This example count
Java Convert date to words
Java Convert date to words In this tutorial, you will learn how to convert date into words. Just like a number, you can also convert a date into words. Here...(" "); show(st4[q - 2]); } } } public void show(String s
Program using String functions - Java Beginners
Program using String functions  Write a program to accept a sentence and find out how many times the word "the" occurs?  Hi Friend, Try... { public static void main(String[] args) { int count = 0
tO FIND UNIQUE WORDS IN A FILE USING HASHMAP
tO FIND UNIQUE WORDS IN A FILE USING HASHMAP  import java.util.*; import java.io.*; public class countunique { private String[] splitter...))) i++; String word = str.substring(count + 1, i
Count Palindromes from the string
Count Palindromes from the string In this section, we are going to find out... to input a sentence or string. This string is then break down into tokens using... of the string. After that we have matched the tokens with the reversed words
How can i add a new count to this source code ? - Java Beginners
How can i add a new count to this source code ?  Hi everyone I...;ZHTYPE (Just Logical) Please tell me how can i see this output? Code... No. of found I get XTSM/XTS/TRX/XHAN and now i want count total of this ZHTYPE
How can i add a new count to this source code ? - Java Beginners
How can i add a new count to this source code ?  Hi everyone I...;ZHTYPE (Just Logical) Please tell me how can i see this output? Code... No. of found I get XTSM/XTS/TRX/XHAN and now i want count total of this ZHTYPE
Java program - convert words into numbers?
Java program - convert words into numbers?   convert words into numbers?   had no answer sir
Count Records using the Prepared Statement
\PreparedStatement>java CountRecords Count records example using prepared... Count Records using the Prepared Statement  ... to count all records of the database table by using the PreparedStatement
Count characters from text file in Java
Count characters from text file Using Java Here we are going to count... all the spaces between the string using replaceAll() method and then convert it into character array in order to count the occurrence of each character
Advertisements
 

 

 

DMCA.com