Hi i have written a program to identify all the vowels in a sentance and need to output the number as Stars. at the moment it is outputting the number how would i tell the program to output 5 stars instead of the number 5
this is what i have written so far.

// filename VowelCounter.java
// Created Fri 5th nov 2004
// Counts vowels in a sentance
// I really dont understand this stuff

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;

public class VowelCounter

{
    public static void main(final String[] pArgs) throws IOException

    {

        final InputStreamReader tInputStreamReader = new InputStreamReader(System.in);
        final BufferedReader tKeyboard = new BufferedReader(tInputStreamReader);
    // get user to input a sentance
        System.out.println(" this will count how many vowels are in a sentance");
        System.out.println(" Please enter a Short sentance");

        String tString = tKeyboard.readLine();
        String tNewString = tString.toLowerCase();

    // counts characters in sentance
        int tStringLength = tNewString.length();

        int tVowA = 0;
        int tVowE = 0;
        int tVowI = 0;
        int tVowO = 0;
        int tVowU = 0;
        int tOtherChar = 0;

    //begin for loop
        for (int tCounter = 0; tCounter < tStringLength; tCounter ++)
        {
        char tVow = tString.charAt(tCounter);
    //end for loop begin if statement
        if (tVow =='a')
            {
                tVowA ++;
            }
        else if (tVow =='e')
            {
                tVowE ++;
            }
        else if (tVow =='i')
            {
                tVowI ++;
            }
        else if (tVow =='o')
            {
                tVowO ++;
            }
        else if (tVow =='u')
            {
                tVowU ++;
            }
        else if (tVow !=' ')
                {
                    tOtherChar ++;
                }
        }
    // end if statement


        System.out.println ("A: " +tVowA);
        System.out.println ("E: " +tVowE);
        System.out.println ("I: " +tVowI);
        System.out.println ("O: " +tVowO);
        System.out.println ("U: " +tVowU);
        System.out.println ("tOther Characters: " +tOtherChar);
    }
}

any help and or advice would be much appreciated

Dani AI

Generated

A concise, practical fix and a couple of safe options.

The goal is to print a line of asterisks whose length equals the vowel count instead of printing the numeric value. 's single-loop approach works for printing stars directly. A shorter, modern option (Java 11+) is to build the star string with String.repeat:

System.out.println("A: " + "*".repeat(tVowA));

For environments older than Java 11, avoid reprinting the exact loop from the thread by creating a char array and filling it, then converting to a String:

char[] stars = new char[tVowA];
Arrays.fill(stars, '*');
System.out.println("A: " + new String(stars));

Two quick correctness suggestions specific to 's posted code: the lowercase conversion is stored in tNewString but the loop reads characters from tString — switch to tNewString.charAt(tCounter) so the lowercase test actually applies. Also the current test for "other" characters treats anything that's not a space as "other"; consider using Character.isLetter and Character.isWhitespace to separate letters, whitespace and punctuation more reliably (increment tOtherChar only for characters that are neither letters nor whitespace).

String.repeat is documented in the Java API for Java 11 and later: String.repeat. Replacing the numeric prints with any of the shown star-string constructions yields output like A: ***** for a count of five.

Recommended Answers

All 2 Replies

>how would i tell the program to output 5 stars instead of the number 5

for ( int i = 0; i < 5; i++ )
  System.out.print ( "*" );

:) hi Naru thanks for the code i figured it in class and didn't have to use the code you sent but thanks anyway

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.