Knowee
Questions
Features
Study Tools

Tom is playing scrabble and forms new words. He wants to count the number of characters present in the word formed by him. Help him to do this using the below java program.Code :import java.util.Scanner;public class Main { public static void main(String[] args) { Scanner sc=new Scanner(System.in); String str=sc.next(); //find length and print the same          int len=  ;                System.out.println("The length of the word is "+)  ; }}

Question

Tom is playing scrabble and forms new words. He wants to count the number of characters present in the word formed by him. Help him to do this using the below java program.Code :import java.util.Scanner;public class Main { public static void main(String[] args) { Scanner sc=new Scanner(System.in); String str=sc.next(); //find length and print the same          int len=  ;                System.out.println("The length of the word is "+)  ; }}

...expand
🧐 Not the exact question you are looking for?Go ask a question

Solution

Here is the completed Java program that will help Tom count the number of characters in the word he forms in Scrabble:

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter the word:");
        String str = sc.next();
        //find length and print the same
        int len = str.length();
        System.out.println("The length of the word is " + len);
    }
}

In this program, we first import the Scanner class from the java.util package. This class is used to get input from the user. In the main method, we create an object of the Scanner class and use its next() method to get the word entered by the user. We then use the length() method of the String class to find the length of the word (i.e., the number of characters in it) and store it in the variable 'len'. Finally, we print the length of the word.

This problem has been solved

Similar Questions

Raghu's tutor provides him a simple sentence and asks him to count the number of words in the sentence. The words are separated by space.Example, if the tutor provides the sentence as "Welcome to Java", the output should beNumber of words 3The words are WelcometoJavaFill the space in the below code so that the code fulfills his requirement. Code :                Scanner sc=new Scanner(System.in); String str=sc.nextLine(); String words[ ]  =   ; int count=  ;  //count holds the number of words in the sentence System.out.println("Number of words "+); System.out.println("The words are "); for(String    :     ) //for each loop System.out.println(x);

Write a java program to accept a String and the perform the following operations:a) Count the number of words with the first and the last letter being same.

Develop a program with a method to print the count of a given character in a string and return the sum of the respective indices.Hint: use charAt method to fetch a character in a string. String s="apple"; s.charAt(0) fetches the first character.

This exercise is to test your understanding of Java Strings. A sample String declaration:String myString = "Hello World!"The elements of a String are called characters. The number of characters in a String is called the length, and it can be retrieved with the String.length() method.Given two strings of lowercase English letters, and , perform the following operations:Sum the lengths of and .Determine if is lexicographically larger than (i.e.: does come before in the dictionary?).Capitalize the first letter in and and print them on a single line, separated by a space.Input FormatThe first line contains a string . The second line contains another string . The strings are comprised of only lowercase English letters.Output FormatThere are three lines of output:For the first line, sum the lengths of and .For the second line, write Yes if is lexicographically greater than otherwise print No instead.For the third line, capitalize the first letter in both and and print them on a single line, separated by a space.Sample Input 0hellojavaSample Output 09NoHello JavaExplanation 0String is "hello" and is "java". has a length of , and has a length of ; the sum of their lengths is .When sorted alphabetically/lexicographically, "hello" precedes "java"; therefore, is not greater than and the answer is No.When you capitalize the first letter of both and and then print them separated by a space, you get "Hello Java".

Fill the code to split the words in a String and print themimport java.util.Scanner;public class Main{    public static void main(String args[])    {                Scanner sc= new Scanner(System.in);                String name= ;                 //fill code to get the words in the String               String[] words = ;               for(String    :  )                    System.out.println(str);    }}

1/3

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.