Question

Write a java program that accepts a string from the user. List all characters in the...

Write a java program that accepts a string from the user. List all characters in the string from lowest ASCII value to highest and output the number of occurrences for each of those characters.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

If any doubt please comment back and kindly rate.

Summary : Firstly took string input. Than convert that string to character array and sort that character array. Now, create a hash map to take each character and it's count. Now iterate a loop and check one by one each character from character array if it is already present in hasp map than increase it's occurrence by one and if it not present than add it in hash map and put occurrence to one. Finally. print all values of hash map.

import java.io.*;
import java.util.*;

class Main {
public static void main(String[] args) {
System.out.println("Enter String");
Scanner sc = new Scanner(System.in);//Making sc an object of scanner class
String s = sc.nextLine();//Taking input
char[] ch = s.toCharArray(); //converting string to char array
Arrays.sort(ch);// sorting char array so they can be printed from smaller to biggest in ASCII value

HashMap<Character, Integer> countchar = new HashMap<Character, Integer>();/*creating a hashmap of
char and int to count occurence of each character*/
for (char c : ch) {
if (countchar.containsKey(c)) {//checking if hash map already contains character c
countchar.put(c,countchar.get(c) + 1); //if yes than increase it's count by 1
}
else {
countchar.put(c, 1); //if no than out it in hash map
}
}
//print the output
for (Map.Entry e : countchar.entrySet()) {
System.out.println(e.getKey() + " " + e.getValue());// char and it's number of occurence is printed
}

sc.close();//closing sc
}
}

Add a comment
Know the answer?
Add Answer to:
Write a java program that accepts a string from the user. List all characters in the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a C++ program that accepts a string from the user and then replaces all occurrences...

    Write a C++ program that accepts a string from the user and then replaces all occurrences of the letter e with the letter x.

  • Write a complete java program that accepts input from the user, strips any spaces or punctuation...

    Write a complete java program that accepts input from the user, strips any spaces or punctuation then sorts the characters of the input and outputs the sorted characters. For example, input is "I Love Java!" would output "aaeIJLovv"

  • Need Java help: 1. Create a Java program that accepts input String input from a user...

    Need Java help: 1. Create a Java program that accepts input String input from a user of first name. 2. Create a Java program that accepts input String input from a user of last name. 3. Concatenate the Strings in a full name variable and print to the console.

  • In Java #2 JAVA Write a program named salaryCalculator that accepts input from the user that...

    In Java #2 JAVA Write a program named salaryCalculator that accepts input from the user that represents an annual salary for employees. This program should all be written in the main driver program no additional classes are needed. Your program should do the following: Prompt the user for a salary continually until the user enters a sentinel value, a negative number. Keep two running totals the first is the total of all original salaries and the second is a total...

  • In the first task, you will write a Java program that accepts a string of the...

    In the first task, you will write a Java program that accepts a string of the format specified next and calculate the answer based on the user input. The input string should be of the format dddxxdddxx*, where d represents a digit and x represents any character and asterisk at the end represents that the string can have any number of characters at the end. 1. Prompt the user to enter a string with the specific format (dddxxdddxx*) 2. Read...

  • Write a Java program called Histogram.java that displays a list of distinct characters in an input...

    Write a Java program called Histogram.java that displays a list of distinct characters in an input tile and the occurrence of each eharacte. Your iogram should 1ead an input file name from a use. After that, your program should read characters in the file and display a list of distinct characters and their occurTeces. Finally, your program should draw a veril bafo the occuences For the assignment, your program has.to display the result exactly as the sample run. For instance,...

  • in C++ Write a program that will take a sentence from the user and uses a...

    in C++ Write a program that will take a sentence from the user and uses a ​for loop​ to convert all lowercase characters to uppercase characters. Then output the converted sentence. Example, if the user enters “Hello World”, your program should output “HELLO WORLD”.​ Recall that the ASCII value of ‘A’ is 65 and the ASCII value of ‘a’ is 97. The ASCII value of ‘Z’ is 90 and the ASCII value of ‘z’ is 122. in c++… we can...

  • Design and implement a Java program (name it PasswordTest) that accepts a string from the user...

    Design and implement a Java program (name it PasswordTest) that accepts a string from the user and evaluates it as a password, giving it a valid or invalid verdict A good password will be at least 8 characters and includes at least one lower-case letter, at least one upper-case letter, at least one digit, and at least one character that is neither a letter nor a digit. Your program will need to check each character in the string in order...

  • In Java, write a program that prompts the user to input a sequence of characters and...

    In Java, write a program that prompts the user to input a sequence of characters and outputs the number of vowels. You will need to write a method to return a value called isAVowel which will return true if a given character is a vowel and returns false if the character is not a vowel. A second method, called isAConstant, should return true if a given character is a constant and return false if the character is not a constant....

  • Write a program that accepts a String value from the user and displays the reverse of...

    Write a program that accepts a String value from the user and displays the reverse of that value. For additional challenge, determine if the String and its reverse are equal and display a message explaining the result.

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT