-3

This is what I have

import java.util.Scanner;
public class A9 {

  public static String getPhrase () {
Scanner input = new Scanner(System.in);
System.out.print ("Enter a phrase: ");
String phrase = input.nextLine();
return phrase;
 }

public static void reportPhrase (String phrase) {
System.out.println (phrase);
}

public static void printHistogram (String phrase) {  
    for(int i=0;i <phrase.length();i++){
        if((phrase.charAt(i) == 'a') || 
            (phrase.charAt(i) == 'e')  ||
            (phrase.charAt(i) == 'i') || 
            (phrase.charAt(i) == 'o') ||
            (phrase.charAt(i) == 'u')) {
  System.out.println(phrase.charAt(i) + ": ");
 for (int j = 0; j < phrase.length(); j++) {
            System.out.print("*");
} 

public static void main(String args[]) {
String phrase;  
phrase = getPhrase ();
reportPhrase (phrase);
printHistogram (phrase);
// Call the method printHistogram here    

    }
}

and the output is this

Enter a phrase: frederator
frederator
e: 
**********e: 
**********a: 
**********o: 
**********

Can someone rewrite it to make an output more like this?

ex " Alphabet soup is my favorite soup in the whole world."
    a: * * *
    e: * * * *
    i: * * *
    o: * * * * *
    u: * *
4castle
  • 32,613
  • 11
  • 69
  • 106
  • This is very unclear. None of the example replaces vowels with asterisks, contrary to the title. What exactly are you trying to do, and what is going wrong? – Tunaki Nov 13 '16 at 23:12
  • Use String replaceAll(regex, replacement) , go through the string api. You have many ways to do it. – PKR Nov 13 '16 at 23:18
  • 1
    Possible duplicate of [Java: How do I count the number of occurrences of a char in a String?](http://stackoverflow.com/questions/275944/java-how-do-i-count-the-number-of-occurrences-of-a-char-in-a-string) – 4castle Nov 13 '16 at 23:27

3 Answers3

0
import java.util.Scanner;

public class A9 {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.println("Enter a phrase: ");
        String str = in.nextLine();
        printVowels(str);

    }

    public static void printVowels(String str) {
        String a = "";
        String e = "";
        String i = "";
        String o = "";
        String u = "";
        for (int ind = 0; ind < str.length(); ind++) {
            switch (str.charAt(ind)) {
                case 'a':
                case 'A':
                    a += "* ";
                    break;
                case 'e':
                case 'E':
                    e += "* ";
                    break;
                case 'i':
                case 'I':
                    i += "* ";
                    break;
                case 'o':
                case 'O':
                    o += "* ";
                    break;
                case 'u':
                case 'U':
                    u += "* ";
                    break;
            }
        }
        System.out.println("a: " + a);
        System.out.println("e: " + e);
        System.out.println("i: " + i);
        System.out.println("o: " + o);
        System.out.println("u: " + u);
    }
}
noko
  • 1,129
  • 2
  • 14
  • 25
Puar
  • 16
  • 3
0

Hope this helps. I created a string with the input, however you can join it with your method.

public class Te {

public static void main(String[] args) {

    String s="Alphabet soup is my favorite soup in the whole world.";
    int arr[] = new int[5];

    String S = s.toUpperCase ();

    arr[0]=S.length() - S.replace("A", "").length();
    arr[1]=S.length() - S.replace("E", "").length();
    arr[2]=S.length() - S.replace("I", "").length();
    arr[3]=S.length() - S.replace("O", "").length();
    arr[4]=S.length() - S.replace("U", "").length();


    System.out.print("a: ");printStar(arr[0]);
    System.out.print("e: ");printStar(arr[1]);
    System.out.print("i: ");printStar(arr[2]);
    System.out.print("o: ");printStar(arr[3]);
    System.out.print("u: ");printStar(arr[4]);

}

public static void printStar(int n){
    if(n > 0){
        System.out.print("*");
        printStar(n-1);
    }
if (n==0)
{System.out.println("");}
}
}
0

Using Hammad2506's code and the base code provided by OP I formulated exactly what OP was looking for using the existing strings in the other methods. Please excuse my terrible whitespace.

/**
 * A simple program that prompts the user
 * for a line of text.
 */

import java.util.Scanner;
public class Loops {

  public static String getPhrase () {
    Scanner input = new Scanner(System.in);
    System.out.print ("Enter a phrase: ");
    String phrase = input.nextLine();
    return phrase;
  }

  public static void reportPhrase (String phrase) {
    System.out.println (phrase);
  }

  public static void printHistogram (String phrase) {
    // Your code here
    int arr[] = new int[5];

    String P = phrase.toUpperCase ();

    arr[0]=P.length() - P.replace("A", "").length();
    arr[1]=P.length() - P.replace("E", "").length();
    arr[2]=P.length() - P.replace("I", "").length();
    arr[3]=P.length() - P.replace("O", "").length();
    arr[4]=P.length() - P.replace("U", "").length();


    System.out.print("a: ");printStar(arr[0]);
    System.out.print("e: ");printStar(arr[1]);
    System.out.print("i: ");printStar(arr[2]);
    System.out.print("o: ");printStar(arr[3]);
    System.out.print("u: ");printStar(arr[4]);

  }

  public static void printStar(int n){
    if(n > 0){
        System.out.print("*");
        printStar(n-1);
       }
    if (n==0)
      {System.out.println("");
        }
}
  public static void main(String args[]) {
    String phrase;  
    phrase = getPhrase ();
    reportPhrase (phrase);
    // Call the method printHistogram here
    printHistogram (phrase);

  }
}