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



Home Answers Viewqa Java-Beginners letter counting for the smallest word

Have Programming Question? Ask it here!
 
 
 


kristina warden
letter counting for the smallest word
1 Answer(s)      8 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
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
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
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
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
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
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 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
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
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
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
gOFFICE
gOFFICE       Free online word processor, outpput can be pdf or html letters, allows personalization in letter templates. Read full Description
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
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
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
display all words containing the letter which i gave as input...
display all words containing the letter which i gave as input...  If i enter any character,it have to display the names which having that character..write a program using java example(output) Enter any character: a Names: Abi
finout longest word in sentence
finout longest word in sentence  write a program , how to find out the longest word in a sentence using java   Hi Friend, Try...; } } LongestWord(){ String word = ""; for(int i=0;i<stringArray.length;i++){ if(i==0
print word with max vowels
print word with max vowels  if suppose i hava entered a sentence and in that sentence i have to print a word which has maximum vowels in it.... i have tried it but i am not getting it...please help
display a list of names(when we press first letter)
display a list of names(when we press first letter)  If i gave...("Enter letter: "); String letter=input.next().substring(0,1...++){ if(countries[i].startsWith(letter)){ System.out.println(countries
com in ms word
com in ms word  how to use com object to fetch data from ms word and storing it into sql database
com in ms word
com in ms word  how to use com object to fetch data from ms word and storing it into sql database
Display set of names in array when we press the first letter
click the starting letter (like in gmail if we press the letter it will show...("Enter letter: "); String letter=input.next().substring(0,1...++){ if(countries[i].startsWith(letter)){ System.out.println
Auto complete of word search using ajax with java
Auto complete of word search using ajax with java  I want to display the list of words when I type the first letter of the word in a text box. I am using jsp to design the form. I want ajax sample to achieve this feature. Its
Auto complete of word search using ajax with java
Auto complete of word search using ajax with java  I want to display the list of words when I type the first letter of the word in a text box. I am using jsp to design the form. I want ajax sample to achieve this feature. Its
Java word extraction program. - Java Beginners
Java word extraction program.  Need to code a public class named... an integer for the starting letter position followed by 5 integers specifying the number...- character word. The starting position should be based on the leftmost character
Display the data to MS word
the database(say im searching using an id) and should display it on the ms word , i want it to be in a good format. FOr example my word doc has to be Name... a word doc would help a lot!! thank you
Scanning a word in a TEXT document......
Scanning a word in a TEXT document......  Hi Genius persons... I'm having many resumes in my FOLDER called HARISH in D:(colon) i want to scan... by searching the JAVA and .net words present in the files..if JAVA word is present means
Scanning a word in a TEXT document......
Scanning a word in a TEXT document......  Hi Genius persons... I'm having many resumes in my FOLDER called HARISH in D:(colon) i want to scan... by searching the JAVA and .net words present in the files..if JAVA word is present means
from number to word
from number to word  i want to know weather there is any method that can be use in changing value from number to word. Example if i write ten thousand, it will automatically be written as 10000.   Java convert number
 

 

 

DMCA.com