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
astra-53 [7]
2 years ago
6

Write a Console Java program that asks the user to enter one sentence on the keyboard. Output on the console:

Computers and Technology
1 answer:
Nady [450]2 years ago
6 0

Answer:

Here is the JAVA program:

import java.util.Scanner;  //to accept input from user

public class Main{  //class name

public static void main(String[] args){ //start of main function

String sentence;  //to store the input sentence

int i, length;  //declare variables

int count[] = new int[256];  //creates a counter array

  Scanner input = new Scanner(System.in);  //creates Scanner class object to accept input from user

       System.out.println("Enter a sentence: ");  //prompts user to enter a sentence

       sentence = input.nextLine();  //scans and reads the entire sentence string from user

   String[] words = sentence.split("\\s+");  //splits the sentence into an array with separator as one or more white spaces and stores this array in words

   System.out.println("Number of words: " + words.length);  //display the number of words in sentence by using length which returns the length of the words array

     sentence = sentence.toLowerCase();  // converts the sentence into lower case to ignore case sensitivity

     sentence = sentence.replaceAll("\\s+","");  //removes the empty spaces from sentence

     length = sentence.length();  //stores the length of the sentence string into length variable

       for (i = 0; i < length; i++) {  //iterates through the length of the sentence

           count[(int) sentence.charAt(i)]++;        }  //counts occurrence of each alphabet in the sentence and stores it in count array

       for (i = 0; i < 256; i++) {  displays occurrence of alphabet in sentence

           if (count[i] != 0) {  //if count array is not empty

               System.out.println((char) i + "-" + count[i]);  }}}}

//prints each alphabet and its count

           

Explanation:

The program prompts the user to enter a sentence. Suppose the user enters "We won" so

sentence = "We won"

Now this sentence splits into an array with white space as separator and stored in words array. So words array has two words of sentence now i.e. "We" and "won"

Next words.length is used to return the length of words array. So length of words array is 2 hence

number of words = 2

Next the sentence is converted to lower case and spaces are removes so sentence becomes:

wewon

sentence.length() returns the length of sentence which is 5.

Next the for loop is used. At each iteration of this loop occurrence each character of sentence is added to the count[] array. charAt() is used to return a character at each index position of string. So the frequency of every character is counted and stored in count array.

Next is a for loop that is used to display each alphabet in the sentence along with its frequency in alphabetic order.

Hence the entire program gives the output:

Number of words: 2                                                                                                             e-1                                                                                                                            n-1                                                                                                                            o-1                                                                                                                            w-2

The screenshot of the output of example given in the question is attached.

You might be interested in
Where can elicitation techniques be used?
Nikitich [7]

Answer:

An elicitation technique is any of a number of data collection techniques used in anthropology, cognitive science, counseling, education, knowledge engineering, linguistics, management, philosophy, psychology, or other fields to gather knowledge or information from people.

Explanation:

3 0
2 years ago
In which of the following work situations would word processing software be most appropriate to make the task easier?
Olegator [25]
Where is the situations?

3 0
3 years ago
Which one of these is the most commonly used hardware interface for attaching peripherals to a microcomputer?
Tanzania [10]
Hey there!

The most commonly used hardware interface for attaching peripherals to a microcomputer is the universal series bus, or USB, port. Many computers have at least 2 to 4 USB ports that allow for multiple devices to be plugged in at once. In terms of wired devices as opposed to wireless, this is the main way that a mouse, a keyboard, a smartphone (for charging and syncing information), and many other devices are connected to computers. 

Hope this helped you out! :-)
4 0
2 years ago
Write a function nexthour that receives one integer argument, which is an hour of the day, and returns the next hour. This assum
Vinvika [58]

Answer:

The function in C is as follows:

int nexthour(int tme){

   tme = tme%12 + 1;

   return tme;

}

Explanation:

This defines the function

int nexthour(int tme){

Ths calculates the next hour using % operator

   tme = tme%12 + 1;

This returns the next hour

   return tme;

}

5 0
2 years ago
Caracteristicas de los valores numericos en excel
Vilka [71]
Todos los libros de Excel están formados por hojas, cada una de las hojas contiene más de 17 mil millones de celdas y en cada una de esas celdas podremos almacenar nuestros datos. Por esta razón es importante conocer los tipos de datos que podemos ingresar en cada una de las  celdas de Excel


hope this help
6 0
2 years ago
Other questions:
  • Click to review the online content. Then answer the question(s) below, using complete sentences. Scroll down to view additional
    8·1 answer
  • An important task that the operating system performs is ____, which keeps track of the files stored on a computer so that they c
    15·1 answer
  • Create a function named first_a that uses a list comprehension. The function will take a single integer parameter n. Find every
    13·1 answer
  • Suppose that a is a one-dimensional array of ints with a length of at least 2. Which of the following code fragments successfull
    8·1 answer
  • Comments should be written in what type of language
    6·1 answer
  • Write an expression to compute the quartic root of x. the quartic root of a number is the square root of its square root. exampl
    11·1 answer
  • PLEASEEE HELPPP
    7·1 answer
  • Use Spreadsheet Functions and Formulas
    6·1 answer
  • PYTHON
    5·1 answer
  • What is the difference between Remote Assistance and Remote Desktop?
    5·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!