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.
Module 1 Program Write a complete Java program in a file called Module1Program.java that reads all...
Module 1 Program Write a complete Java program in a file called Module1Program.java that reads all the lyrics from a file named lyrics.txt that is to be found in the same directory as the running program. The program should read the lyrics for each line and treat each word as a token. If the line contains a double (an integer is also treated as a double) it should use the first double it finds in line as the timestamp for...
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
in java Write an application that gets two numbers from the user and prints the sum,...
in java Write an application that gets two numbers from the user and prints the sum, product, difference and quotient of the two numbers in a GUI.
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...
Write a program that reads two strings from an input file (The first line is X,...
Write a program that reads two strings from an input file (The first line is X, the second line is Y), compute the longest common subsequence length AND the resulting string. You will need to write 2 methods 1) return LCS length in iterative function // return the length of LCS. L is the 2D matrix, X, Y are the input strings, m=|X|, n=|Y| int lcs_it(int **C, string X, string Y, int m, int n ) 2) return LCS resulting...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT