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

java methods Write a program whose input is a character and a string, and whose output indicates the number of times the charact

er appears in the string.

Computers and Technology
1 answer:
AfilCa [17]4 years ago
5 0

Answer:

Here is the JAVA program:

import java.util.Scanner; // for taking input from user

//class to count number of times a character appears in the string

public class CharacterCounter

//method which returns number of times a character appear in string

{ public static int CountCharacter(String userString, char character)

   {  

       int counter = 0;   //stores the no of times character appears in string

for (int i=0; i<userString.length(); i++) //loop moves through entire string

       { // if character matches the character in the string

           if (userString.charAt(i) == character)

           counter++;  // adds one to the character counter variable

       }  

       return counter;     } // returns no of time character is found in string

public static void main(String[] args) {

    Scanner scanner = new Scanner(System. in);//takes input from user

    String string;

    char character;

       System.out.println("Enter a string"); //prompts user to enter a string

       string = scanner.nextLine(); //scans the input string

       System.out.println("Enter a character");

       // prompts user to enter character

       character = scanner.next().charAt(0); //scans and reads the character

       System.out.println("number of times character "+character + " appears in the string " + string +": " + CountCharacter(string, character));  }  }

// calls CountCharacter method to output no of times a character appears in the input string

Explanation:

The method CountCharacter() has a loop in which the variable i works as an index which moves through each character of the string and if the character of the string in i matches the character which is to be found in the string then the value of counter variable increments by 1. This means that counter variable keeps counting the number of time the character appears in the string. The loop breaks when value of i exceeds the length of the string which means that the i reaches the end of the string and has searched the entire string for the specified character. In the end the count variable returns the number of time character appeared in the string. The main() function takes as input from the user, a string and a character to be searched for in the string. It then calls CountCharacter() function to find the number of times that input character appears in the input string and displays it on the output screen.

The program along with its output is attached as a screenshot.

You might be interested in
Critical thinking questions Giving 30 points if you answer correctly!!!!
krek1111 [17]

Answer:

1. The reason hunting seasons are displayed all over the world in cave paintings is because of the necessity to hunt. Ancient people everywhere were nomads that relied on wild animals for the majority of their calories. The paintings likely served as an expression of the importance of hunting to their culture. It could have also been used as educational material for very young children to learn about hunting as well as its importance to their community.

Explanation:

I can't do 2nd because the image was cut off, sorry.

8 0
3 years ago
If I wanted to add code so that the costume of my sprite would change, what menu would I look under to find the "change costume"
Damm [24]

Answer:

D.) Looks

the costume is technically the look of your sprite, so looks is the correct answer

7 0
3 years ago
You must create an instructional video for your company and are required to ensure it is compatible with your company's windows
enyata [817]
The best video format which should be chosen when creating an instructional video is WMV format. At first, you are asked to make video which will <span>compatible with windows systems and since WMV format is a</span><span> part of the </span>Windows Media<span> framework it is a sensible choice. WMV can be also put into the AVI container format which is quite common among the users.</span>
7 0
4 years ago
Explain different types of secondary memory of computer system<br>​
Westkost [7]

<em>Answer:</em>

<em>There are three main types of secondary storage in a computer system: solid state storage devices, such as USB memory sticks. optical storage devices, such as CD, DVD and Blu-ray discs. magnetic storage devices, such as hard disk drives.</em>

<em>Explanation:</em>

6 0
2 years ago
The top element in the hierarchy is the html element , which contains all of the other elements within a Hypertext Markup Langua
Anna [14]

the answer is true

The top element in the hierarchy is the html element

6 0
4 years ago
Other questions:
  • Which Boolean operator enables you to exclude a search term?
    9·2 answers
  • Tanya wants to create an sql query to fetch records from a table. which is the first word she has to type to write the query?
    7·1 answer
  • Wanda works for a large company. She and her team maintain a set of servers that support their office, and their office is conne
    12·1 answer
  • What did Aristotle teach?
    6·2 answers
  • The process of auditing the source code for an application to verify that the proper security controls are present, that they wo
    14·1 answer
  • what are the main technologies that have contributed to the growth and commercialization of the internet .​
    5·1 answer
  • Tipos de estructuras A. Expresiones y de variables B. Selectivas y de control C. Instrucciones y parámetros
    5·1 answer
  • Before a new email application could be released to the public, it was released for a few days to some account holders of a webs
    14·2 answers
  • Which of the following describes why graphical interfaces quickly became popular after their introduction to the mass market?
    5·1 answer
  • What does error code 18 indicate?
    11·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!