Java Tutorial
Reads the string and search character in the string and finds the frequency of search character occurrence in string.
This java program uses Scanner from java.util package to read the user input character and string.
To convert string to character array uses toCharArray method.
Important thing is here, this program uses for each loop like available in python and this code works only if compilation level should be greater than or equal 1.5
import java.util.Scanner; class CharacterFrequencyTest{ public static void main(String[] args){ int frequencyCount = 0; System.out.print("Enter String: "); Scanner sc=new Scanner(System.in); String sentence = sc.nextLine(); System.out.println(""); System.out.print("Enter character to search: "); char searchChar = sc.next().charAt(0); sc.close(); char[] characters = sentence.toCharArray(); // for each in java and works from java compilation level 1.5 and wont work in before 1.5 for(char ch : characters){ if (ch == searchChar){ frequencyCount++; } } System.out.println("Character '"+searchChar+"' Frequency Count in String '"+sentence+"': "+frequencyCount); } }Output:
D:\Java_Programs>javac CharacterFrequencyTest.java D:\Java_Programs>java CharacterFrequencyTest Enter String: Welcome to learn java! Enter character to search: e Character 'e' Frequency Count in String 'Welcome to learn java!': 3 D:\Java_Programs>javac CharacterFrequencyTest.java D:\Java_Programs>java CharacterFrequencyTest Enter String: Welcome to learn java! Enter character to search: Y Character 'Y' Frequency Count in String 'Welcome to learn java!': 0
If character is occurred in the string, returns the frequency count otherwise returns 0.
Reads the string and search character in the string and finds the frequency of search character occurrence in string.
This java program uses only for loop to compute the search character frequency and works compatible in all java compilation level and no minimum compilation level is expected.
import java.util.Scanner; class CharacterFrequencyTest{ public static void main(String[] args){ int frequencyCount = 0; System.out.print("Enter String: "); Scanner sc=new Scanner(System.in); String sentence = sc.nextLine(); System.out.println(""); System.out.print("Enter character to search: "); char searchChar = sc.next().charAt(0); sc.close(); char[] characters = sentence.toCharArray(); for(int i=0;i<characters.length;i++){ if (characters[i] == searchChar){ frequencyCount++; } } System.out.println("Character '"+searchChar+"' Frequency Count in String '"+sentence+"': "+frequencyCount); } }Output:
D:\Java_Programs>javac CharacterFrequencyTest.java D:\Java_Programs>java CharacterFrequencyTest Enter String: Welcome to learn java! Enter character to search: e Character 'e' Frequency Count in String 'Welcome to learn java!': 3 D:\Java_Programs>javac CharacterFrequencyTest.java D:\Java_Programs>java CharacterFrequencyTest Enter String: Welcome to learn java! Enter character to search: Y Character 'Y' Frequency Count in String 'Welcome to learn java!': 0
Java Tutorial
Privacy Policy | Copyright2020 - All Rights Reserved. | Contact us | Report website issues in Github | Facebook page | Google+ page