Character userCharacter is read from input. Write a while loop that reads characters from input until character 's' is read. Then, count the total number of characters read. Character 's' should not be included in the count. Ex: If the input is h s, then the output is: 1 1 import java.util.Scanner; 2 3 public class CountCalculator { 4 5 6 7 8 9 10 11 12 public static void main(String[] args) { Scanner scnr = new Scanner (System.in); char userCharacter; int result; result = 0; userCharacter = scnr. next().charAt(0); *Your code goes here */

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

I need help with this problem in Java

## Challenge Activity: 6.2.2 - While Loops

**Objective:**

Create a program that reads characters from input until the character 's' is encountered. The program will then count and output the total number of characters read, excluding 's'.

### Example:
- **Input:** `h s`
- **Output:** `1`

### Instructions:

Implement the logic to achieve the above objective by completing the Java program below.

```java
import java.util.Scanner;

public class CountCalculator {
    public static void main(String[] args) {
        Scanner scnr = new Scanner(System.in);
        char userCharacter;
        int result;

        result = 0;
        userCharacter = scnr.next().charAt(0);
        
        // Your code goes here
        
        System.out.println(result);
    }
}
```

### Steps:
1. **Import the Scanner:** This is used to read input from the user.
2. **Declare Variables:**
   - `userCharacter` to store the character input.
   - `result` to store the count of characters.
3. **Initialize Scanner:** Use `scnr` to capture input from `System.in`.
4. **Initiate the While Loop:**
   - Read characters and count until 's' is encountered.
   - Exclude 's' from the count.
5. **Output the Result:** Print the number of characters counted before 's'.

### Key Points:
- Ensure that any character read as 's' should not be included in the count.
- Use a while loop to iterate through the input character-by-character.
- Update and print the result once the loop condition is fulfilled.

Consider testing with different inputs to verify the program's accuracy and handling of edge cases.
Transcribed Image Text:## Challenge Activity: 6.2.2 - While Loops **Objective:** Create a program that reads characters from input until the character 's' is encountered. The program will then count and output the total number of characters read, excluding 's'. ### Example: - **Input:** `h s` - **Output:** `1` ### Instructions: Implement the logic to achieve the above objective by completing the Java program below. ```java import java.util.Scanner; public class CountCalculator { public static void main(String[] args) { Scanner scnr = new Scanner(System.in); char userCharacter; int result; result = 0; userCharacter = scnr.next().charAt(0); // Your code goes here System.out.println(result); } } ``` ### Steps: 1. **Import the Scanner:** This is used to read input from the user. 2. **Declare Variables:** - `userCharacter` to store the character input. - `result` to store the count of characters. 3. **Initialize Scanner:** Use `scnr` to capture input from `System.in`. 4. **Initiate the While Loop:** - Read characters and count until 's' is encountered. - Exclude 's' from the count. 5. **Output the Result:** Print the number of characters counted before 's'. ### Key Points: - Ensure that any character read as 's' should not be included in the count. - Use a while loop to iterate through the input character-by-character. - Update and print the result once the loop condition is fulfilled. Consider testing with different inputs to verify the program's accuracy and handling of edge cases.
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 3 steps with 1 images

Blurred answer
Knowledge Booster
Types of Loop
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education