Question

In: Computer Science

Write a complete Java program called CharCounter that gets two Strings called inputWord and inputCharacter from...

Write a complete Java program called CharCounter that gets two Strings called inputWord and inputCharacter from the user at the command line. Check that the character has a length of 1.

If it doesn't, provide the user with suitable feedback and conclude the program.

If the character length is valid (i.e., it has a length of 1), use a while loop to check each position in the inputWord variable and return the number of times the character occurs. For example, if the inputWord is "test" and the inputCharacter is "e" you would output: "There is 1 occurrence of 'e' in test."

Solutions

Expert Solution

CharacterCounter.java

import java.util.Scanner;

public class CharacterCounter {

   public static void main(String[] args) {
       //Declaruing variables
       String inputWord,inputCharacter;
       int count=0,i=0;
      
       //Scanner object is used to get the inputs entered by the user
       Scanner sc=new Scanner(System.in);
      
       //Getting the word entered by the user
       System.out.print("Enter the Input Word :");
       inputWord=sc.next();
      
       //Getting the character entered by the user
       System.out.print("Enter the charcater :");
       inputCharacter=sc.next();
      
       //if the length of the character is greater than 1 the display error message
       if(inputCharacter.length()>1)
       {
           System.out.println("** Invalid.Length of the character should not be more than 1 **");
       }
       else
       {
           i=0;
           //This while loop will count the number of occurrences of that character
       while(i<inputWord.length())
       {
           if(inputCharacter.charAt(0)==inputWord.charAt(i))
           {
               count++;
                 
           }
           i++;
       }
       }
       //Displaying the result
       System.out.println("There are "+count+" occurance of '"+inputCharacter+"' in "+inputWord);

   }

}

_______________

Output:

Enter the Input Word :test
Enter the charcater :e
There are 1 occurance of 'e' in test

______________Thank You


Related Solutions

Write a JAVA program that compares two strings input to see if they are the same?
Write a JAVA program that compares two strings input to see if they are the same?
Using Java, write a program that takes in two integers from the keyboard called m and...
Using Java, write a program that takes in two integers from the keyboard called m and n, where m > n. Your program should print the first m natural numbers (m..1) downwards in n rows.
DO THIS IN JAVA Write a complete Java program. the program has two threads. One thread...
DO THIS IN JAVA Write a complete Java program. the program has two threads. One thread prints all capital letters 'A' to'Z'. The other thread prints all odd numbers from 1 to 21.
IN JAVA Write a complete program that asks the user to enter two real numbers from...
IN JAVA Write a complete program that asks the user to enter two real numbers from the console. If both numbers are positive print the product, if both numbers are negative print the quotient, otherwise print INVALID INPUT. Use a nested if; output should be to a dialog and console; use printf to format the console output (for real numbers specify the width and number of digits after the decimal). The output must be labeled. Follow Java conventions, indent your...
Write a complete Java program to solve the following problem. Read two positive integers from the...
Write a complete Java program to solve the following problem. Read two positive integers from the user and print all the multiple of five in between them. You can assume the second number is bigger than the first. For example if the first number is 1 and the second number is 10, then your program should output 5 10 Java must be grade 11 work easy to understand and not complicated code
Write a complete program in java that will do the following:
Write a complete program in java that will do the following:Sports:             Baseball, Basketball, Football, Hockey, Volleyball, WaterpoloPlayers:           9, 5, 11, 6, 6, 7Store the data in appropriate arraysProvide an output of sports and player numbers. See below:Baseball          9 players.Basketball       5 players.Football           11 players.Hockey            6 players.Volleyball        6 players.Waterpolo       7 players.Use Scanner to provide the number of friends you have for a team sport.Provide an output of suggested sports for your group of friends. If your...
Write a Java program called RevenueAdvanced to calculate the revenue from a sale based on the...
Write a Java program called RevenueAdvanced to calculate the revenue from a sale based on the unit price and quantity of a product input by the user. (use if and if-else statements) • The discount rate is o 0% for the quantity purchased between 1 and 49 units. o 10% for the quantity purchased between 50 and 99 units. o 15% for the quantity purchased between 100 and 149 units. o 25% for the quantity purchased greater than or equal150...
DO THIS PROGRAM IN JAVA Write a complete Java console based program following these steps: 1....
DO THIS PROGRAM IN JAVA Write a complete Java console based program following these steps: 1. Write an abstract Java class called Shape which has only one abstract method named getArea(); 2. Write a Java class called Rectangle which extends Shape and has two data membersnamed width and height.The Rectangle should have all get/set methods, the toString method, and implement the abstract method getArea()it gets from class Shape. 3. Write the driver code tat tests the classes and methods you...
URGENT!!! DO THIS PROGRAM IN JAVA Write a complete Java console based program following these steps:...
URGENT!!! DO THIS PROGRAM IN JAVA Write a complete Java console based program following these steps: 1. Write an abstract Java class called Shape which has only one abstract method named getArea(); 2. Write a Java class called Rectangle which extends Shape and has two data membersnamed width and height.The Rectangle should have all get/set methods, the toString method, and implement the abstract method getArea()it gets from class Shape. 3. Write the driver code tat tests the classes and methods...
In Java: Write a program called F2C that allows the user to convert from degrees Fahrenheit...
In Java: Write a program called F2C that allows the user to convert from degrees Fahrenheit to degrees Celsius. The program should prompt for a temperature in Fahrenheit and output a temperature in Celsius. All calculations should be done in in ints, so be careful of truncation.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT