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]
3 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]3 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
Chris needs to modify the default bullets that are used in a nonnumbered list in Word.
laiz [17]

Answer:

he should select the arrow next to the bullet list

Explanation:

6 0
3 years ago
Read 2 more answers
Please help me asapppp!​
pychu [463]

of which class is this question

6 0
3 years ago
Read 2 more answers
Write a method that takes three numerical String values and sums their values.
Sphinxa [80]

Answer:

<u>Algorithm</u>:

  1. Take a int variable c=0.
  2. Run the loop from 0 to String length.
  3. Get the each index value using charAt() method which will return the char value.
  4. Convert that char into int by typecasting or any other way.
  5. Now check that ASCII fall into the range of 48-57 for 0-9 int number.
  6. If it matches then first convert that number into integer using Integer.parseInt().
  7. Then add with the c and store to the same variable i.e c.
  8. Repeat from 3-7 till the end of the loop.
  9. At the end of loop , Print c outside the loop.

Explanation:

// Java program to calculate sum of all numbers present  

// in a string containing alphanumeric characters  

class GFG  

{  

   // Function to calculate sum of all numbers present  

   // in a string containing alphanumeric characters  

   static int findSum(String str)  

   {  

       // A temporary string  

       String temp = "";  

       // holds sum of all numbers present in the string  

       int sum = 0;  

       // read each character in input string  

       for(int i = 0; i < str.length(); i++)  

       {  

           char ch = str.charAt(i);  

           // if current character is a digit  

           if (Character.isDigit(ch))  

               temp += ch;  

           // if current character is an alphabet  

           else

           {  

               // increment sum by number found earlier  

               // (if any)  

               sum += Integer.parseInt(temp);  

               // reset temporary string to empty  

               temp = "0";  

           }  

       }  

       // atoi(temp.c_str()) takes care of trailing  

       // numbers  

       return sum + Integer.parseInt(temp);  

   }    

   // Driver code  

   public static void main (String[] args)  

   {      

       // input alphanumeric string  

       String str = "1abc5yz7";  

       System.out.println(findSum(str));  

   }  

}  

4 0
3 years ago
Read 2 more answers
What is data mining?1) A particular attribute of information 2) The common term for the representation of multidimensional infor
tiny-mole [99]

Answer:

option 4)

Explanation:

  • Data mining is characterized as a procedure used to extricate usable information from a bigger arrangement of any crude data. It suggests breaking down information designs in huge groups of data utilizing at least one programming.  
  • Data mining is utilized to find patterns and connections in the information so as to help settle on better business choices.  
  • Information or data mining can help spot deals patterns, create more astute advertising efforts, and precisely foresee client faithfulness.
3 0
3 years ago
Which can be used to enter and manipulate information in a database?<br><br> ANSWER: C) a form
alexgriva [62]

Answer:

a form

Explanation:

it says the answer already in the question

3 0
4 years ago
Other questions:
  • What is the primary purpose for a screen saver in windows?
    8·1 answer
  • In a word processing program, the ribbon or menus contain the
    12·1 answer
  • You need to test a condition and then execute one set of statements if the condition is true. If the condition is false, you nee
    9·1 answer
  • If needed, you can pull over on the shoulder of the freeway to take a break if you are tired. true or false (drivers ed) need as
    11·1 answer
  • "Common knowledge" must always be credited or else it is considered plagiarism?<br> True<br> Fales
    5·1 answer
  • Which of these are correctly formatted python dictionaries? Check all that apply.
    5·2 answers
  • Taken together, the physical and data link layers are called the ____________________. Internet layer Hardware layer Internetwor
    15·1 answer
  • Here's another question!<br><br> What is some iterative programming structure uses?
    13·1 answer
  • You are creating a mobile version of a community discussion site for busy moms. Users post questions and other topics for discus
    10·1 answer
  • A company has employees who write down passwords, which should be kept private, on sticky notes. Given this scenario, what shoul
    12·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!