Austin
Austin

Reputation: 11

Why won't my code run second time?

I'm having a problem with some code that I'm doing for homework where it won't repeat when I enter the word for the second time. I'm fairly new to Java so any help would be appreciated. Thanks

class Main
{
    public static void main( String args[] )
    {
        System.out.print( "#Please enter a word : " );
        String word = BIO.getString();

        int i, length, vowels = 0;
        String j;
        length = word.length();
        for (i = 0; i < length; i++)
        {

            j = word.substring(i, i + 1);

            if (j.equalsIgnoreCase("a") == true)
                vowels++;
            else if (j.equalsIgnoreCase("e") == true)
                vowels++;
            else if (j.equalsIgnoreCase("i") == true)
                vowels++;
            else if (j.equalsIgnoreCase("o") == true)
                vowels++;
            else if (j.equalsIgnoreCase("u") == true)
                vowels++;
        }
        System.out.print("[  " + vowels + "] vowels in " + "\""+word+"\"");

        System.out.print("\n");

        System.out.print("#Please enter a word : ");
        word = BIO.getString();

    }
}

Upvotes: 0

Views: 543

Answers (3)

Kirty Bhushan
Kirty Bhushan

Reputation: 147

Do while loop is the best way to achieve what you want to here ! As mentioned above, you have not included your

System.out.print("#Please enter a word : "); word = BIO.getString();

part in any type of loop. SO you can't expect the code to run otherwise. Include it in a do-while and you should be doing well. A well modified example is already answered above.

Upvotes: 0

LGenzelis
LGenzelis

Reputation: 834

To get the result you expect, you should nest your code inside a loop. Like this:

class Main
{
public static void main( String args[] )
{
    System.out.print( "#Please enter a word : " );
    String word = BIO.getString();

    while(!word.equals("QUIT")){

        int i, length, vowels = 0;
        String j;
        length = word.length();
        for (i = 0; i < length; i++)
        {

            j = word.substring(i, i + 1);

            if (j.equalsIgnoreCase("a") == true)
                vowels++;
            else if (j.equalsIgnoreCase("e") == true)
                vowels++;
            else if (j.equalsIgnoreCase("i") == true)
                vowels++;
            else if (j.equalsIgnoreCase("o") == true)
                vowels++;
            else if (j.equalsIgnoreCase("u") == true)
                vowels++;
        }
        System.out.print("[  " + vowels + "] vowels in " + "\""+word+"\"");

        System.out.print("\n");

        System.out.print("#Please enter a word : ");
        word = BIO.getString();
    }

}

That way, your code will loop until you enter the word QUIT.

Upvotes: 2

D Stanley
D Stanley

Reputation: 152556

After the second input executes the program terminates. Use a while(true) block with some sort of special input (like "quit" or an empty string) to break the loop.

Upvotes: 0

Related Questions