Question

In: Computer Science

3. Consider the string:”34456754”. Please find the frequency of each number. With a java program.

3. Consider the string:”34456754”. Please find the frequency of each number. With a java program.

Solutions

Expert Solution

In below code, we need to find frequency of each digit in the string:

for this we have declared one variable called digit , which will iterate fro 0 to 9 using while loop

Inside while loop, for loop is defined , which will run number of times of string's length , and number of digit frequency will be stored in variable flag

Further, if flag is greater than 0. that means if number will be in string for atleast one time, the result will be displayed in console using If statement

____________________________________________

Please find the code in JAVA:

import java.util.*;
public class Main
{
   public static void main(String[] args) {
   Scanner scr=new Scanner(System.in);
       System.out.println("Enter the string of digits");
       String str=scr.nextLine();
       int flag=0,digit=0;
       char value='0';
       while(digit<=9){
       flag=0;
       for(int i=0;i<str.length();i++){
       if(String.valueOf(digit).equals(String.valueOf(str.charAt(i)))){
       flag+=1;
       value=str.charAt(i);
       }
       }
       if(flag>0){
       System.out.println(value+" in string appears "+ flag +" times.");
       }
       digit++;
      
       }
   }
}

Please find the OUTPUT screenshot:


Related Solutions

Write a Java program that takes in a string and a number and prints back the...
Write a Java program that takes in a string and a number and prints back the string from the number repeatedly until the first character... for example Pasadena and 4 will print PasaPasPaP. Ask the user for the string and a number Print back the string from the number repeatedly until the first character For both programs please utilize: methods arrays loops Turn in screenshots
A Java program that deciphers each message by building the string. Each line of the text...
A Java program that deciphers each message by building the string. Each line of the text file contains either a word or a command followed by a forward slash and the requirements of the command. A Stack Implementation is needed as. As you read the data from the text file, you need to know if it is a new phrase or command. The commands are: i – an insert followed by the letters, numbers, or symbols that need to be...
Write a java program to read a string from the keyboard, and count number of digits,...
Write a java program to read a string from the keyboard, and count number of digits, letters, and whitespaces on the entered string. You should name this project as Lab5B. This program asks user to enter string which contains following characters: letters or digits, or whitespaces. The length of the string should be more than 8. You should use nextLine() method to read string from keyboard. You need to extract each character, and check whether the character is a letter...
Write a java program with 3 recursive methods that reverse the order of a string. The...
Write a java program with 3 recursive methods that reverse the order of a string. The first recursive method should reverse the order starting from the left selecting the leftmost character as the head and the remaining part of the string as its tail., the second starting from the right selecting the rightmost character as the head and the remaining part of the string on its left as the tail, and the last a recursive method starting from the middle...
Use Java (Find the number of days in a month) Write a program that prompts the...
Use Java (Find the number of days in a month) Write a program that prompts the user to enter the month and year and displays the number of days in the month. For example, If the user entered month 2 and year 2012, the program should display that February 2012 has 29 days. If the user entered month 3 and year 2015, the program should display that March 2015 has 31 days. Sample Run 1 Enter a month in the...
Please give me an example of this java code. Vehicle - number: int - color: String...
Please give me an example of this java code. Vehicle - number: int - color: String - price: double - horsepower: double + Vehicle ( ) + Vehicle (int number) + Vehicle (int number, String color) + Vehicle (int number, String color, double price) + Vehicle (int number, String color, double price, double horsepower) +getnumber( ):int +setnumber(int number):void +getcolor( ):String +setcolor(String color):void +getprice( ):double +setprice(double price):void +gethorsePower( ):double +sethorsePower(double horsePower):void toString( ):String public String toString( ) { return "\n\n number...
3. Write a Java program that generates a set of random strings from a given string...
3. Write a Java program that generates a set of random strings from a given string of same length where no character is ever repeated and characters belong to the original string. Examples Input: “Hello World” Output: “World oHlel”
Java Program 1. Write a program that asks the user: “Please enter a number (0 to...
Java Program 1. Write a program that asks the user: “Please enter a number (0 to exit)”. Your program shall accept integers from the user (positive or negative), however, if the user enters 0 then your program shall terminate immediately. After the loop is terminated, return the total sum of all the previous numbers the user entered. a. What is considered to be the body of the loop? b. What is considered the control variable? c. What is considered to...
Write a Java program that creates a three-dimensional array. Populate each element with a string that...
Write a Java program that creates a three-dimensional array. Populate each element with a string that states each coordinate position in the array.
In this program: ================================================================== /* Program to count number of occurrences of a given string in...
In this program: ================================================================== /* Program to count number of occurrences of a given string in original string*/ #include <iostream> #include <cstring> #include <stdio.h> #include <iostream> using namespace std; int main() { const int SIZE = 40; char str[SIZE]; char str1[SIZE]; char searchString[SIZE]; int n; int l1, l2; int count = 0; printf("Enter a sentence: \n"); fgets(str,SIZE,stdin); printf("Enter a search word: \n"); fgets(searchString,SIZE,stdin); if (str1[strlen(str1) - 1] == '\n') { str1[strlen(str1)-1] = '\0'; } if (str[strlen(str) - 1] == '\n')...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT