letter counting for the smallest word

letter counting for the smallest word

How do I program my java program to only count the letters in the smallest of two words? this is what I have and it works EXCEPT that it counts all of the letters not just the smallest word..where did I go wrong?

import java.util.*; class CountCharacters { public static void count(final String str){ Runnable readRun1 = new Runnable() { public void run() { try{ Thread.sleep(5000); String st=str.replaceAll(" ", ""); char ch[]=st.toCharArray(); int count=ch.length; System.out.println("Count of smallest word is: "+count); } catch(Exception ex) { }
} }; Thread thread1 = new Thread(readRun1); thread1.start(); } public static void main(String[] args) throws Exception{ Scanner input=new Scanner(System.in); System.out.print("Please enter passwords: "); String str=input.nextLine(); count(str); } }

View Answers

December 19, 2013 at 11:12 AM

In your code we have to split the word based on the delimiter. But you are replacing the delimiter with "".

import java.util.*;

class Test {
    public static void count(final String str) {
        Runnable readRun1 = new Runnable() {
            public void run() {
                try {
                    Thread.sleep(5000);
                    /*String st = str.replaceAll(" ", "");
                    char ch[] = st.toCharArray();
                    int count = ch.length;*/

                    String[] tokens=str.split(" "); //Splitting  the word based on the space delimiter

                    //assume the small word length as 0
                    int smallWordLength=0;

                    if(tokens.length>=0) {
                        smallWordLength=tokens[0].length();

                        //to check any word is less than our assume word.If it is there we are taking that word as small word
                        for(int i=1;i<tokens.length;i++) {
                            String token=tokens[i];
                            if(token.length()<smallWordLength)
                                smallWordLength=token.length();
                        }
                    }

                    // displaying the all the words those length is smallWordLength
                    for(String token:tokens) {
                        if(token.length()==smallWordLength)
                            System.out.println("Samallword'"+token+"' length is:" + smallWordLength);
                    }
                } catch (Exception ex) {
                }
            }
        };
        Thread thread1 = new Thread(readRun1);
        thread1.start();
    }

    public static void main(String[] args) throws Exception {
        Scanner input = new Scanner(System.in);
        System.out.print("Please enter passwords: ");
        String str = input.nextLine();
        count(str);
    }
}

output:
Please enter passwords: kristina warden abasha
Samallword'warden' length is:6
Samallword'abasha' length is:6









Related Tutorials/Questions & Answers:
letter counting for the smallest word
letter counting for the smallest word  How do I program my java... and it works EXCEPT that it counts all of the letters not just the smallest word...(); int count=ch.length; System.out.println("Count of smallest word
how to delete a letter in a word?
how to delete a letter in a word?  how to delete a letter in a word? for example if i enter= roseindia, i want to delete 's', then output= roeindia
Advertisements
POI Word document (Letter Template)
POI Word document (Letter Template)  Dear Team, i need code for generating word document(letter format). i am unable to get the code for formats, font settings, letter type settings. please help me for the same. Thanks
Counting specific word occurences in a file
Counting specific word occurences in a file  Hello I have a log file from the proxy which consists all the browsing history with date,time,url,ip address etc as fields.Now i need to take only main part of the url like the domain
java word counting - Java Beginners
java word counting  Hi I want a code in java that replaces a word...... but this will change all the occurrence even if its part of another word the run...();System.out.println("enter the letter to be replaced(which yiou want to change
word and character counting - Java Beginners
word and character counting  here is the java code i made but i have to add something where it will read the inFile and display the number of words... = null; String word; int numLines = 0; int numWords = 0
Display first letter of every word in uppercase
Display first letter of every word in upper case In this Java tutorial section, you will learn how to display the first letter of every word in uppercase...;inputString = br.readLine(); FirstLetter letter=new FirstLetter
Counting
ModuleNotFoundError: No module named 'counting'
ModuleNotFoundError: No module named 'counting'  Hi, My Python... 'counting' How to remove the ModuleNotFoundError: No module named 'counting... to install padas library. You can install counting python with following
ModuleNotFoundError: No module named 'counting'
ModuleNotFoundError: No module named 'counting'  Hi, My Python... 'counting' How to remove the ModuleNotFoundError: No module named 'counting... to install padas library. You can install counting python with following
ModuleNotFoundError: No module named 'letter'
ModuleNotFoundError: No module named 'letter'  Hi, My Python... 'letter' How to remove the ModuleNotFoundError: No module named 'letter'... to install padas library. You can install letter python with following command
ModuleNotFoundError: No module named 'letter'
ModuleNotFoundError: No module named 'letter'  Hi, My Python... 'letter' How to remove the ModuleNotFoundError: No module named 'letter'... to install padas library. You can install letter python with following command
Finding smallest value, then the next etc
Finding smallest value, then the next etc  hi, I know how to search for the smallest value index in an unsorted array so far with this: double... becomes true, I want to search for next smallest value that isn't the one I just
word program - Java Beginners
within the word a letter that the user entered,change the value of the array... method that counts the number of occurrencies of a letter in a word and returns... a letter in the alphabet occurs in a word. Please enter a word: howdy (sample
ModuleNotFoundError: No module named 'residue-counting'
ModuleNotFoundError: No module named 'residue-counting'  Hi, My... named 'residue-counting' How to remove the ModuleNotFoundError: No module named 'residue-counting' error? Thanks   Hi, In your
ModuleNotFoundError: No module named 'residue-counting'
ModuleNotFoundError: No module named 'residue-counting'  Hi, My... named 'residue-counting' How to remove the ModuleNotFoundError: No module named 'residue-counting' error? Thanks   Hi, In your
ModuleNotFoundError: No module named 'cell_counting'
ModuleNotFoundError: No module named 'cell_counting'  Hi, My... 'cell_counting' How to remove the ModuleNotFoundError: No module named 'cell_counting' error? Thanks   Hi, In your python
frequency of a letter in a string
frequency of a letter in a string  Could someone answer in netbeans please Write a program that takes in a string and a letter from the user and displays the number of appearances of the entered letter in the entered string
ModuleNotFoundError: No module named 'reciprocal_smallest_distance'
ModuleNotFoundError: No module named 'reciprocal_smallest_distance'  ...: No module named 'reciprocal_smallest_distance' How to remove the ModuleNotFoundError: No module named 'reciprocal_smallest_distance' error
ModuleNotFoundError: No module named 'reciprocal_smallest_distance'
ModuleNotFoundError: No module named 'reciprocal_smallest_distance'  ...: No module named 'reciprocal_smallest_distance' How to remove the ModuleNotFoundError: No module named 'reciprocal_smallest_distance' error
Automatic Capital letter
Automatic Capital letter  Morning sir, Can you help me, i want to make a automatic capital letter. I have a jTextfield1, if i type aaaaaa ,it will be automatic AAAAAA. can you help me give a source code. Thank you very much
ModuleNotFoundError: No module named 'letter-list'
ModuleNotFoundError: No module named 'letter-list'  Hi, My Python... 'letter-list' How to remove the ModuleNotFoundError: No module named 'letter-list' error? Thanks   Hi, In your python environment
ModuleNotFoundError: No module named 'phoenix-letter'
ModuleNotFoundError: No module named 'phoenix-letter'  Hi, My... named 'phoenix-letter' How to remove the ModuleNotFoundError: No module named 'phoenix-letter' error? Thanks   Hi, In your python
Incrementing a Double by the Smallest Possible Amount
Incrementing a Double by the Smallest Possible Amount In this section, you will earn how to increment the double value by the smallest possible amount... by the smallest possible amount, we have invoked the previousDouble() method of ChoiceFormat
ModuleNotFoundError: No module named 'counting-three-dijit'
ModuleNotFoundError: No module named 'counting-three-dijit'  Hi...: No module named 'counting-three-dijit' How to remove the ModuleNotFoundError: No module named 'counting-three-dijit' error? Thanks   Hi
ModuleNotFoundError: No module named 'cosmo-pair-counting'
ModuleNotFoundError: No module named 'cosmo-pair-counting'  Hi, My... named 'cosmo-pair-counting' How to remove the ModuleNotFoundError: No module named 'cosmo-pair-counting' error? Thanks   Hi
ModuleNotFoundError: No module named 'counting-three-dijit'
ModuleNotFoundError: No module named 'counting-three-dijit'  Hi...: No module named 'counting-three-dijit' How to remove the ModuleNotFoundError: No module named 'counting-three-dijit' error? Thanks   Hi
ModuleNotFoundError: No module named 'counting-three-dijits'
ModuleNotFoundError: No module named 'counting-three-dijits'  Hi...: No module named 'counting-three-dijits' How to remove the ModuleNotFoundError: No module named 'counting-three-dijits' error? Thanks   Hi
letter count problem - Java Beginners
letter count problem  i have a problem in my java coding to count two characters of a string. eg to count the letter "ou" in the string "How do you feel today?". The answer should be 5. but i still have error compiling
counting the positive,negative and zero nos. - Java Beginners
counting the positive,negative and zero nos.  Create a program that will input n number, and determine the number of positive, negative
Welcome! to the News letter archive
News Letter Archive     ... letter sent our users.  ADS_TO_REPLACE_1 Newsletters are regularly... Letter Archive Date Subject
MicroSoft word
MicroSoft word  sir i want to develop an swing application like the microsoft word Document.can you help me pls
Check for character is letter or not.
Description: This tutorial demonstrate the use of Character.isLetter() method which checks whether the value stored in char data-type variable is letter or not. Code: public class CheckLetter 
Check for character is letter or digit.
Description .style1 { background-color: #FFFFCC; } Description: This tutorial demonstrate how to check a character variable value is a digit or letter using isLetterOrDigit() method. Code:ADS_TO_REPLACE_1
ModuleNotFoundError: No module named 'odoo10-addon-mail-as-letter'
ModuleNotFoundError: No module named 'odoo10-addon-mail-as-letter'  ...: No module named 'odoo10-addon-mail-as-letter' How to remove the ModuleNotFoundError: No module named 'odoo10-addon-mail-as-letter' error? Thanks
ModuleNotFoundError: No module named 'odoo9-addon-mail-as-letter'
ModuleNotFoundError: No module named 'odoo9-addon-mail-as-letter'  Hi...: No module named 'odoo9-addon-mail-as-letter' How to remove the ModuleNotFoundError: No module named 'odoo9-addon-mail-as-letter' error? Thanks
ModuleNotFoundError: No module named 'trytond-account-dunning-letter'
ModuleNotFoundError: No module named 'trytond-account-dunning-letter' ...: No module named 'trytond-account-dunning-letter' How to remove the ModuleNotFoundError: No module named 'trytond-account-dunning-letter' error
ModuleNotFoundError: No module named 'ascii-letter-classifier'
ModuleNotFoundError: No module named 'ascii-letter-classifier'  Hi...: No module named 'ascii-letter-classifier' How to remove the ModuleNotFoundError: No module named 'ascii-letter-classifier' error? Thanks   
Java Word Processor
occur after the word as before. o if the original first letter of a word...Java Word Processor  Problem: Design and implement a class called... of words (a hyphenated word is one word), o number of sentences (ends
How to delete line starting with a letter in java Regex?
How to delete line starting with a letter in java Regex?  Hi, i 'm trying to code with regex and java. i want to know how it's possible to delete lines which starts with a letter like G or g in a text. i tried
Word Count
Word Count       This example counts the number of occurrences of  a specific word in a string. Here we are counting the occurrences of word "you" in a string
Easy Counting Java Program...Please Help? - Java Beginners
Easy Counting Java Program...Please Help?  As I said, I am a beginner at Java. I came across this problem that Im sure uses java. Could you please... NOT contain variables with the same letter OR the same subscript (eg, a set may
NUMBER OF VOWELS IN A WORD
NUMBER OF VOWELS IN A WORD  WRITE A PROGRAM THAT WILL INPUT A WORD THEN IT WILL DETERMINE THE NUMBER OF VOWELS PRESENT IN THE INPUTTED WORD
Write String in Word document
Write String in Word document  How to read and write strings in word document
Compare two word file
Compare two word file  How to compare two word file using Java
Counting the highest sequence of zeros and one's - Java Beginners
Counting the highest sequence of zeros and one's  Count the highest sequence value of '0's and '1's. for a given string with zeroes and ones. Input: You should ask user to enter any bit string with zeros and ones sequence
Get the smallest double greater than the given value
Get the smallest double greater than the given value This section demonstrates you how to get the smallest double value greater than the given value. Java... and generate the new smallest double value which greater than the specified
counting identifiers,no.of inputs and outputs - Java Magazine
counting identifiers,no.of inputs and outputs  Hi, my name is jyothi.iam doing my miniproject the title is 'evaluation of complexity measure using cognitive metrics'.actually i want to take the program as input and i want
Problem reading word file
Problem reading word file  Deepak you provide me code for extarcting equation from a word file and also to write in a word file.But when I again want to read preveously created word file(created by your code) it gives an error
open word document
open word document  how to open a word document ??   Please go through the following link: Java Read word document file The above link.... It will be helpful for you to read data from word document

Ads