First, launch NetBeans and close any previous projects that may be open (at the
ID: 3592631 • Letter: F
Question
First, launch NetBeans and close any previous projects that may be open (at the top menu go to File ==> Close All Projects).
Then create a new Java application called "CharCounter" (without the quotation marks) that gets two Strings called inputEntry and inputCharacter from the user at the command line. Allow for the inputEntry String to be one or more words of input. Check that the inputCharacter String has a length of 1 and note these validations:
If it doesn't, provide the user with suitable feedback and conclude the program.
If the inputCharacter length is valid (i.e., it has a length of 1), use a while loop to check each position in the inputEntry variable and return the number of times the character occurs. For example, if the inputEntry is "test" and the inputCharacter is "e" you would output: "There is 1 occurrence(s) of 'e' in test."
***Note: include commenting in your code***
Explanation / Answer
the code is in java
please give thumbs up
import java.util.Scanner; // importing scanner class for taking input
public class MyClass {
public static void main(String args[]) {
Scanner sc=new Scanner(System.in); // defining scanner class object
String name=sc.next(); // taking string as input
char x=sc.next().charAt(0); // taking the character as input
int counter = 0; // initialing a counter
for( int i=0; i<name.length(); i++ ) {
if( name.charAt(i) == x ) { // checking the char in string
counter++; // incrementing counter
}
}
String str = String.format("%s%d%s%c", "There is ",counter, " of ", x); // formating string
System.out.println(str); // printing counter
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.