kristina warden
letter counting for the smallest word
1 Answer(s)      a year and 4 months ago
Posted in : Java Beginners

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); } }

Advertisement
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
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
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
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
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
Welcome! to the News letter archive
News Letter Archive     ... letter sent our users.   Newsletters are regularly distributed...; News Letter Archive Date Subject
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
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
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
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 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
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
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
Determine Average by removing Largest and Smallest Number
Determine Average by removing Largest and Smallest Number In this section, you will learn how to remove largest and smallest number from the 10 numbers... and smallest number. Now to remove these highest and lowest numbers from the array, we have
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
MicroSoft word
MicroSoft word  sir i want to develop an swing application like the microsoft word Document.can you help me pls
counting the values in input file and and writing the output to a file
counting the values in input file and and writing the output to a file  Can any one please correct this code. This is to read a file and later it should write the output to a file. Input file has 20 rows and 4 columns. If we
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
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
Compare two word file
Compare two word file  How to compare two word file using Java
Write String in Word document
Write String in Word document  How to read and write strings in word document
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