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
Alexxx [7]
4 years ago
10

Write a Java program that will 1. Ask the user to type in a sentence, using a JOptionPane.showInputDialog(). 2. The program will

examine each letter in the string and count how many time the upper-case letter 'E' appears, and how many times the lower-case letter 'e' appears. The key here is to use the charAt method in class String. 3. Using a JOptionPane.showMessageDialog(), tell the user how many upper and lower case e's were in the string. 4. Repeat this process until the user types the word "Stop". (Check out the method equalsIgnoreCase in class String to cover all upper/lower case possibilities of the word "STOP").
Please provide a code for this in JAVA.

Computers and Technology
1 answer:
Paraphin [41]4 years ago
6 0

Answer:

Here is the JAVA program :

import javax.swing.JOptionPane;   //to use JOptionPane  

public class Main {   //class name

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

   int ECount= 0;   //counts number of upper case E

   int eCount= 0;  //counts number of lower case e

   String sentence;   //stores input sentence string

sentence = JOptionPane.showInputDialog(null, "Type a sentence (type stop to exit)");  //uses showInputDialog method to display a dialog box which prompts user to enter a sentence

 int length = sentence.length();  //stores the length of the sentence

 while (!sentence.equalsIgnoreCase("stop"))  //the loop continues to execute until the user types stop

{ for (int i = 0; i < length; i++) { //loops through the sentence

             if (sentence.charAt(i) == 'E')  //if the character is an uppercase E

                  ECount += 1;  //adds 1 to the count of Ecount

             if (sentence.charAt(i) == 'e')  //if the character in a sentence is lowercase e

                  eCount += 1;}  //adds 1 to the count of lower case e

JOptionPane.showMessageDialog(null, "There are " + ECount + " number of E's and " + eCount + "  number of e's in " +sentence);   //displays the number of times uppercase and lowercase (E and e) occur in sentence

sentence = JOptionPane.showInputDialog(null, "Type a sentence (type stop to exit");  //prompts user to enter the next sentence

length = sentence.length();  //stores the length of sentence at each iteration of while loop until user enters stop

eCount=0;  //resets the counter of lowercase e

ECount=0;  } } } //resets the counter of uppercase e

Explanation:

The program uses showInputDialog() method that displays a dialog box on output screen. This dialog box prompts the user to enter a string (sentence) Suppose user enters "Evergreen" as input sentence so

sentence = "Evergreen"

Next the length of this string is computed using length() method and stored in length variable. So

length = sentence.length();

length = 9

The for loop iterates through this input sentence until the variable i is less than length i.e. 9

At first iteration

if (sentence.charAt(i) == 'E') becomes

if (sentence.charAt(0) == 'E')

Here charAt() method is used which returns the character at the specified index i. This if condition is true because the character at 0-th index (first character) of the sentence Evergreen is E. So ECount += 1; statement adds 1 to the count of upper case E so

ECount = 1

At second iteration:

if (sentence.charAt(1) == 'E')

this condition is false because the character at 1st index of sentence is 'v' which not an upper case E so the program moves to next if condition if (sentence.charAt(i) == 'e') which is also false because the character is not a lower case e either.

At third iteration:

if (sentence.charAt(2) == 'E')

this condition is false because the character at 2nd index of sentence is 'e' which not an upper case E so the program moves to next if condition if (sentence.charAt(i) == 'e') which is true because the character is a lower case e. So the statement eCount += 1; executes which adds 1 to the count of e.

eCount = 1

So at each iteration the sentence is checked for an upper case E and lower case e. After the loop breaks, the program moves to the statement:

JOptionPane.showMessageDialog(null, "There are " + ECount + " number of E's and " + eCount + "  number of e's in " +sentence);

which displays the number of uppercase E and lower case e in a message dialog box. So the output is:

There are 1 number of E's and 3 number of e's in Evergreen.

Then because of while loop user is prompted again to enter a sentence until the user enters "stop". Now the user can enter Stop, StOp, STOp etc. So equalsIgnoreCase method is used to cover all upper/lower case possibilities of the word "stop".

The screenshot of the program and its output is attached.

You might be interested in
PLEASE HELP
vitfil [10]

Answer:

1. I would ask her how big is her house, because when it’s big fiber optic cable is the best choice.

2. I would also ask her how loud is her house, as STP cable would be best choice.

Explanation:

Fiber optic cables have larger bandwidth than conventional metal cables. The amount of information transmitted per unit time using fiber over other transmission media is higher when compared to other alternatives. Power loss is minimal as this aid transmission over a long distance, this is why it is the best for big houses.

STP cables, are known for their better efficiency. But are expensive and hard to install, way stiffer and needs a lot of space.

5 0
3 years ago
When a web page author includes meta keywords, like sex, that have little to do with the information on the page, the author is
OLga [1]
This practice is known as "ClickBait".
8 0
3 years ago
Why is Thomas Jefferson considered an accomplished man , or a Renaissance man?​
lesantik [10]
The term “Renaissance man” means for a very clever person to be good at many different things.
4 0
3 years ago
17. One of the earliest methods used to print illustrations was
lord [1]
The correct option is C.
Lithography is a method of printing, which is originally based on the immiscibility of oil and water. Lithography printing basically involves, drawing on limestones with wax crayons, applying ink unto the stone and then printing the image on paper. It was one of the earliest method used to print illustrations.
8 0
3 years ago
Read 2 more answers
Using words input, output, processor, CPU, memory, RAM, ROM, and storage,
Mashcka [7]

here because brainly sucks I hate this website stop censoring litteraly everything, can't get away with calling someone a poopy head on here

8 0
3 years ago
Other questions:
  • What is the name of the intel technology that allows a processor to handle two threads at the same time?
    6·1 answer
  • What is that tool that makes liquid metal? It is used for wires, Ik I might sound dumb but I'm rlly curious​
    7·1 answer
  • 3k means about 3 thousand bytes. how would you express two hundred million bytes? .
    8·1 answer
  • What does it mean to say RAM is volatile? *
    9·1 answer
  • Select the correct answer. Tom recently visited a science exhibition where he learned that computer experts programmed robots to
    6·2 answers
  • How many keys are on a microsoft windows standard keyboard
    7·2 answers
  • When a primitive type variable is passed as an argument to a method, what is passed into the receiving method's parameter variab
    12·1 answer
  • Write a program in Python to sum to numbers:<br><br> Urgently needed, please.
    14·1 answer
  • Why is brainly so addictinggggggggg!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    14·1 answer
  • Which statement best describes how the programming layer of abstraction in
    10·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!