1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
Tema [17]
3 years ago
7

Write a program that prompts the user for his or her name. Display the number of words and characters in the entered name.

Computers and Technology
1 answer:
Law Incorporation [45]3 years ago
4 0
<h2><u>Answer:</u></h2>

// import the Scanner class to allow for user input

import java.util.Scanner;

// define a class for the program

public class NumOfWordsAndChars {  

   // the main method

   public static void main(String[] args) {

       // create an object of the Scanner class, call it <em>input</em>

       Scanner input = new Scanner(System.in);

      //1. Prompt the user to enter their name

       System.out.println("Please enter your name");

       //using the input object, receive and store the entered

       //name in a string variable called <em>name</em>.

       String name = input.nextLine();

       // declare and initialize to zero, a variable noOfWords

       // that will hold the number of words in the name.

       int noOfWords = 0;

       // 2. Get the number of words.        

       // Check to see if the user actually entered a name.

       // If they did enter a name, use the split() function to break

       // the entered name into its constituent words and store

       // these words in an array variable called <em>words.</em>

<em>        </em>// From the words array, the number of words can be found

       // by simply getting the length of the array using <em>words.length</em>

<em>        </em>// Store the number of words in the variable <em>noOfWords</em>

       if (name != null && !name.isEmpty()){

           String words [] = name.split("\\s+");

           noOfWords = words.length;

       }

       // 3. Get the number of characters.

       // the number of characters in a string is found using the function lenght()

      // store the number of characters in an integer variable, <em>noOfChars</em>

       int noOfChars = name.length();

       // 4. Display the number of words and characters

       System.out.println("No of words: " + noOfWords);

       System.out.println("No of characters: " + noOfChars);

   }                       // End of main method

}                          // End of the class declaration

<h2><u>Explanation</u>:</h2>

The above code has been written in Java.

The code contains comments explaining every part of the code. Please carefully go through the comments in the code.

The source code has also been attached to this reponse. Please download the file and you may run it on your Java enabled local machine.

Note that the split() function in the code receives an argument which is a regular expression representing one or more whitespace characters i.e \\s+.

This means that the string (name) should be split into its constituent words since a word is typically a non-space character in a string.

<em>Hope this helps!</em>

Download java
You might be interested in
If the pc­doctor software is installed on a computer's hard drive, what two different ways can the program be started?
xeze [42]
<span> The PC-doctor software is system information tool used to diagnose and  test the computer and determine its configuration.</span>
If the PC-­doctor software is installed on a computer's hard drive, you should start the program in two different ways:<span>
1. Press function key during startup sequence
2. Start menu shortcut</span>


8 0
4 years ago
In this paper https://arxiv.org/pdf/2008.10150.pdf, I'm having a hard time understanding how the assignment in the picture is de
anastassius [24]

Answer:

f*(i,z) = log pxz

Explanation:

they round

4 0
3 years ago
Read 2 more answers
Which is an example of a natural separation of visitors from employees?
natulia [17]

Answer:

d) desk

Explanation:

srry if this is wrong ;

4 0
3 years ago
Explain mportance of using Microsoft Excel in pharmaceutical science​
goldenfox [79]

Answer:

research the question and go to setting and press advanced search and it will give answer

4 0
3 years ago
Find the different pup
Nastasia [14]
What’s the question?
6 0
3 years ago
Other questions:
  • What is the difference between a denial-of-service attack and a distributed denial-of-service attacks? which is potentially more
    10·1 answer
  • Which ipsec component is software that handles the tasks of encrypting, authenticating, decrypting, and checking packets?
    10·1 answer
  • The Leader Motive Profile (LMP) includes a: a. high need for power. b. low need for achievement. c. moderate need for power. d.
    6·1 answer
  • the part of the computer that provides access to the internet is the A.modem B. keyboard C. monitor or D.system unit
    7·2 answers
  • The carbon-14 isotope is important because it allows scientists to determine the ___________ of an organic sample.
    14·1 answer
  • What are the programs required to work in computer
    7·1 answer
  • Which of the following protects intellectual property?copyright
    9·2 answers
  • On a scale of 1-10 how would you rate your pain
    12·1 answer
  • How to change your username in brainly​
    12·2 answers
  • Diane changes the size of an image with the format picture pane and keeps the original ratio of height to width of the object. W
    8·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!