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]
3 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]3 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
NEED FIVE QUESTIONS ANSWERED!!!
jeka94

Answers:

A. Create a spreadsheet

The MS Excel software is a type of software that allows users to create spreadsheets that can contain a large number of data. These spreadsheets are mainly used to store information of businesses or simply used to organize different information in a table like form.

C. The rectangular box where a column and row intersect

A cell is a rectangular box where a column and row intersect in a spreadsheet. These cells can store one piece of data at a time and have unique coordinates to separate them from each other.

D. AutoComplete only works when the first character is a text.

The AutoComplete feature in MS Excel is a feature that can give a suggestion of what the user is trying to input into a cell. This feature will only work when a user inputs a text (e.g. A to Z and 0 - 9). If a user inputs a special character, the AutoComplete feature will not be able to recognize it.

B. Home

The Home tab contains the main formatting tools in a spreadsheet. The home tab is one of the most used tabs in MS Excel as most of the inputs in a spreadsheets will most likely need formatting to make it more understandable to the users.

B. Hide the row and unhide it when the new season begins.  

The hide feature in MS excel allows users to select a row(s) or column(s) of the spreadsheet to temporarily remove the selected rows or columns and prevent users from editing information in them. The unhide feature will simply reveal the hidden selection to allow the user to edit the information again.

3 0
3 years ago
propose,two new ,proudly South African ways,which you can visualize that the IoT,can be used at work to make life better.
grin007 [14]
1. Internet should be used as a medium of learning.

Through internet, such African people could obtain a lot of knowledge from various fields that provided by more developed countries, and use that knowledge to help the development in south africa.

2. It should be used for utilizing business.

There are a lot of new potential business that could be done because of internet, such as blogging, affiliate marketing, public endorsement through social media followers, etc.
3 0
4 years ago
As long as users refer to files in the ____ directory, they can access the files without entering the absolute filename.
algol13
Your answer to the question would be "Root"
4 0
4 years ago
What are the pros and cons of using the Internet for researching career information?
VikaD [51]

Answer:

pros: a lot of info

cons: a lot of it can be fake!

Explanation:

4 0
3 years ago
Read 2 more answers
A researcher wants to do a web-based survey of college students to collect information about their sexual behavior and drug use.
Brilliant_brown [7]

Answer:

True.

Explanation:

An investigator would like to do an internet-based college student survey to gather data regarding their behavior as well as drug use. Even so, specific identifications are still not gathered and IP addresses might be available in the set of data. Damage threat must be assessed both by the magnitude (or severity) and the likelihood (or likelihood) of the hurt.

7 0
4 years ago
Other questions:
  • The function takes two string parameters; the first is the name of a file and the second is text. Complete the function to appen
    7·1 answer
  • Which is the largest unit of measurement for computer data?
    14·1 answer
  • Describe a way in which a badly-designed or badly-constructed STRUCTURAL subsystem might impact the PROPULSION subsystem.
    5·1 answer
  • A series of four or five comparisons and calculations that together determine an employee's withholding tax value might be group
    6·1 answer
  • What mistake do you think you might make related to changing the data in a cell that's used in a formula?
    14·1 answer
  • Can someone help me please and thank u
    13·2 answers
  • Some in the security community argue that a lack of diversity is security vulnerability. For example, Firefox and Internet Explo
    6·1 answer
  • Draw a flowchart diagram for a program that display a person's name x times​
    5·1 answer
  • To generate a series of first ten counting number of algorithm​
    9·1 answer
  • What is the definition of shape
    14·2 answers
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!