LetterCounter.java - import [email protected] Lemuel M [email protected] Book Title From Control Structures through Objects by Tony Gaddis Page 267

LetterCounter.java - import [email protected] Lemuel...

This preview shows page 1 out of 1 page.

import java.util.Scanner; /** * @author Lemuel M. Uhuru * @Date June 25, 2013 * Book Title: From Control Structures through Objects by Tony Gaddis * Page: 267 Challenge 5: Letter Counter * @Description Write a program that asks the user to enter a string, and then asks * the user to enter a character. The program should count and display the number of * times that the specified character appears in the string. */ public class LetterCounter { public static void main(String[] args) { // Declare variable to store string String text; // Declare var to store character char character; // Create Scanner object Scanner keyboard = new Scanner(System.in);
Image of page 1

You've reached the end of your free preview.

Want to read the whole page?

Unformatted text preview: // Prompt the user for String input System.out.println("Enter a string: "); text = keyboard.nextLine(); // Prompt the user for Char input System.out.println("Enter a character: "); character = keyboard.nextLine().charAt(0); // Declare counter variable int count = 0; // Construct for loop to calculate the number of // times that the specified character appears in the string. for (int i = 0; i < text.length(); i++) { if (text.charAt(i) == character) { count += 1; } } // Diplay the number of times the character appears System.out.println("The character appears " + count + " times"); } }...
View Full Document

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern

Stuck? We have tutors online 24/7 who can help you get unstuck.
A+ icon
Ask Expert Tutors You can ask You can ask You can ask (will expire )
Answers in as fast as 15 minutes
A+ icon
Ask Expert Tutors