Question

JAVA How do I write a code that receive a string, detect the length of each...

JAVA
How do I write a code that receive a string, detect the length of each token with array, save the frequency of the length and print it? If the token length is longer than 10, the length is considered to be 0. If the frequency of the lengtg is 0 the information will not be printed

ex)

If the input is "Count the length of the token tokenlongerthanten"

the output will be

Length0 : 1
Length2 : 1
Length3 : 2
Length5 : 3

0 0
Add a comment Improve this question Transcribed image text
Answer #1

I think you want the output to be length of token(0-10) and their frequency. This string  "Count the length of the token tokenlongerthanten" should result in output

length0-> tokenlongerthanten

length2-> of

length3-> the,the

length5-> count, token

length6-> length

I've used string tokenizer class for this operation. It extracts word by word from string as tokens. This extraction is defined by delimiter(to separate words)(here space is used). I've commented the code for better understanding.

Code

import java.util.ArrayList;

import java.util.StringTokenizer;

public class stringToken {

    //this function prints the desired output as in question

    static void findLength(String s){

        //frequency array

        int []freq=new int[11];

        //tokenizer set to input string which extract word seperated by space

        StringTokenizer tokenizer=new StringTokenizer(s," ");

        //while there are words left in string

        while(tokenizer.hasMoreTokens()){

            //extract word

            String token=tokenizer.nextToken();

            //check length

            if(token.length()>10)   //length greater than 10

                freq[0]++;

            else freq[token.length()]++;    //length smaller than 10

        }

        //printing output

        for(int i=0;i<=10;i++){

            if(freq[i]>0)

            System.out.println("Length"+i+":"+freq[i]);

        }

    }

    public static void main(String[] args) {

        String s="Count the length of the token tokenlongerthanten";

        findLength(s);  //calling function

    }

}

Output

For indentation purpose

.

Add a comment
Know the answer?
Add Answer to:
JAVA How do I write a code that receive a string, detect the length of each...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • WRITTEN IN JAVA Write a function as follows: Receive a string as a parameter.Count the length...

    WRITTEN IN JAVA Write a function as follows: Receive a string as a parameter.Count the length of the string.Count the occurrences of each distinguishable symbol in the string (use an array).Divide the factorial of the length by the product of the factorials of the occurrences.Return the quotient as the integer result.The point of this function is to find the result only by using the formulas for permutations.For example, BAAA the function counts that the string length is 4, that A...

  • JAVA CODE String[] str = in.nextLine().split("\\s"); for(int i=0;i<=str.length;i++){ if (str[i].equals(stop)) break;    if(i%2==0){ System.out.print(str[i]+" "); }...

    JAVA CODE String[] str = in.nextLine().split("\\s"); for(int i=0;i<=str.length;i++){ if (str[i].equals(stop)) break;    if(i%2==0){ System.out.print(str[i]+" "); }    } lets say the INPUT for the array is: girl, boy, man, woman, girl, man, boy i dont want the array to print any string twice, i want the output to be like this: print all of them but print each once ---------- OUTPUT: girl, boy, man, woman

  • Java programming: I need to create a method that is given a 2D char array and...

    Java programming: I need to create a method that is given a 2D char array and a String that returns a part of the original 2D array. The input string will tell the method which rows from the input array need to be returned. For example, a 5x5 char array input along with a string "0 4" would return rows 1 and 5 of the input array. The String input will always be numbers divided by spaces and will not...

  • I need help writing this code for java class. Starter file: Project3.java and input file: dictionary.txt...

    I need help writing this code for java class. Starter file: Project3.java and input file: dictionary.txt Project#3 is an extension of the concepts and tasks of Lab#3. You will again read the dictionary file and resize the array as needed to store the words. Project#3 will require you to update a frequency counter of word lengths every time a word is read from the dictionary into the wordList. When your program is finished this histogram array will contain the following:...

  • Write code in Java. In this step, functions and methods are synonymous. You may assume that...

    Write code in Java. In this step, functions and methods are synonymous. You may assume that strings only contain uppercase letters A through Z. 1. Write a function as follows: o Receive a string as a parameter. o Count the length of the string Count the occurrences of each distinguishable symbol in the string (use an array or map/dictionary). o Divide the factorial of the length by the product of the factorials of the occurrences. o Return the quotient as...

  • Exception handling to detect input String vs. Integer

    6.6 LAB: Exception handling to detect input String vs. IntegerThe given program reads a list of single-word first names and ages (ending with -1), and outputs that list with the age incremented. The program fails and throws an exception if the second input on a line is a String rather than an Integer. At FIXME in the code, add a try/catch statement to catch java.util.InputMismatchException, and output 0 for the age.Ex: If the input is:Lee 18 Lua 21 Mary Beth 19 Stu 33 -1then the output...

  • Write MIPS code that converts a string containing a number into an actual number. The function...

    Write MIPS code that converts a string containing a number into an actual number. The function to do this conversion, atoi (Array To Integer) should have a single input (the string) and output a single number (the integer). The Java/C code is as follows: atoi(a0: array) { int digit, number, i = 0; while( (array[i] >= '0') && (array[i] <= '9') ) { digit = array[i] - '0'; number = 10 * number + digit; i++; } return number; }

  • pls help java ASAP!!!!!!! Topic String Tokenizer Static Methods Static Variables Primitive Arrays Description Enhance the...

    pls help java ASAP!!!!!!! Topic String Tokenizer Static Methods Static Variables Primitive Arrays Description Enhance the last assignment by providing the following additional features: (The additional features are listed in bold below) Class Statistics In the class Statistics, create the following static methods (in addition to the instance methods already provided). • A public static method for computing sorted data. • A public static method for computing min value. • A public static method for computing max value. • A...

  • Language is Java. Thank you.It is a review test and I will find out the rest...

    Language is Java. Thank you.It is a review test and I will find out the rest if I just know that answers. 1) What is the output of the following code: public class Test public static void main(String] args) [ String s1 new String("Java String s2 new String("Java) System.out.print( (s1 s2)(s1.equals (s2))) A) false false B) true true C) false true D) true false E) None of the above 2) Given the following program: public class Test f public static...

  • ArraysAndFiles.java and PartiallyFilledArray.java. They are shells that do not do anything. You will be adding code...

    ArraysAndFiles.java and PartiallyFilledArray.java. They are shells that do not do anything. You will be adding code after the comments in the main method and using the javadoc documentation to implement the other methods. Task 1: Send array data to the screen In ArraysAndFiles.java, create a new method printOnScreen and compile it. In the main method of ArraysAndFiles.java, add the code to declare and initialize the array of Strings and call printOnScreen to print the array on the screen. Task 2:...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT