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
You can create a ____ partition to hold files that are created temporarily, such as files used for printing documents (spool fil
Lisa [10]
/var is the answer to then empty space
3 0
3 years ago
Insurance can help you:a. minimize monthly expenses
KIM [24]
This is an easy answer sure in the back of your mind u knew it was b good luck :).

4 0
2 years ago
You trained a binary classifier model which gives very high accuracy on thetraining data, but much lower accuracy on validation
kompoz [17]

Answer:

a. This is an instance of overfitting.

Explanation:

In data modeling and machine learning practice, data modeling begins with model training whereby the training data is used to train and fit a prediction model. When a trained model performs well on training data and has low accuracy on the test data, then we say say the model is overfitting. This means that the model is memorizing rather Than learning and hence, model fits the data too well, hence, making the model unable to perform well on the test or validation set. A model which underfits will fail to perform well on both the training and validation set.

4 0
2 years ago
When programming, which of the following is true of the editor?
OLEGan [10]

Answer:

The correct answer is It is a program that checks for grammatical errors.

Explanation:

In programming, when we want to check that our grammar is correct, we can use an editor.

This is a program that allows you to correct spelling through a software application. It analyzes the entered text and automatically checks whether it is spelled correctly or not, comparing the words with its internal database.

Given this information we can say that the correct answer is It is a program that checks for grammatical errors.

5 0
3 years ago
Suppose that a t-shirt comes in five sizes: S, M, L, XL, XXL. Each size comes in four colors. How many possible t-shirts are the
guapka [62]

Answer:

120

Explanation:

This question is basically asking how many ways can we arrange 5 sizes of shirts if they come in 4 colors.

5 permutation of 4

To find permutation, we use the formula

P(n, r) = n! / (n - r)!

Where

n is the size of the shirts available, 5

r is the color of each shirt available, 4

P(5, 4) = 5! / (5 - 4)!

P(5, 4) = (5 * 4 * 3 * 2 * 1) / 1!

P(5, 4) = 120 / 1

P(5, 4) = 120

Therefore, the total number of shirts available is 120

3 0
2 years ago
Other questions:
  • Attacker player X is standing still on a corner kick, as the ball is played, he jumps up with his arms flailing above his should
    8·1 answer
  • The tone a writer takes is referred to as the writing _____.
    7·2 answers
  • Implement the function pairSum that takes as parameters a list of distinct integers and a target value n and prints the indices
    11·1 answer
  • How would you define data science ?
    10·1 answer
  • When you purchase donuts, they come in boxes of 12, so 24 donuts take 2 boxes. All donuts need to be in a box, so if you buy 13
    10·1 answer
  • Make a list of five primary raw materials, for each one, indicate and industrial material that is created from it
    10·2 answers
  • Logan has developed an excellent presentation with interesting content. He received great feedback on the evaluation
    10·1 answer
  • Help pweeze this is due today :(<br><br> I will give u brainliest just pweeze, I need this answer :(
    8·1 answer
  • Jonah wants to type an assignment using Word. Arrange the steps he needs to follow in the correct order.?
    13·1 answer
  • Software piracy is acceptable as it helps us obtain software cheaper or sometimes even for free.
    11·2 answers
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!