简体   繁体   中英

Why is my for loop exiting after the first element when checking vowels? Java

I am new to for loops and enhanced for loops so perhaps someone can help clarify why exactly my enhanced for loop for checking vowels is only checking the first element before exiting the loop?

I put a println(vowel) underneath the for loop to test its output before checking against the input and it is only pulling 'A'. The consonants all work just fine, so I'm more than a little confused at this point.

Anything that can point me in the right direction to figuring or this out or help me understand would be greatly appreciated.

Thanks!

import java.util.Scanner;

public class WordStart {

public static void main(String[] args) {

    Scanner in=new Scanner(System.in);

    char[] consonants = {'B','C','D','F','G','H','J','K','L','M','N','P','Q','R','S','T','V','W','X','Y','Z'};
    char[] vowels = {'A','E','I','O','U'};
    System.out.println("Please enter a word: ");
    String word=in.nextLine();
    char firstLetter=(Character.toUpperCase(word.charAt(0)));
    int found = -1;




    for (char vowel: vowels)
{//System.out.println(vowel);
        if (firstLetter == vowel)
    {
        found = 1;
            if (found==1)
            {
                System.out.print(firstLetter+" is a vowel.\n");
                System.exit(0);
            }

    }
     for (char consonant: consonants)   
        {
            if (firstLetter == consonant)
            {
            found = 2;
                {
                    if (found==2)
                    {
                        System.out.print(firstLetter+" is a consonant.\n");
                        System.exit(0);

                    }

                }
            }    
        }
     if (found<=0)
     {
         System.out.println(firstLetter+" is not a vowel or consonant.\n");
         System.exit(0);
     }

}

This is the correct code:

import java.util.Scanner; public class WordStart {

public static void main(String[] args) {
    Scanner in=new Scanner(System.in);

    char[] consonants = {'B','C','D','F','G','H','J','K','L','M','N','P','Q','R','S','T','V','W','X','Y','Z'};
    char[] vowels = {'A','E','I','O','U'};
    System.out.println("Please enter a word: ");
    String word=in.nextLine();
    char firstLetter=(Character.toUpperCase(word.charAt(0)));
    int found=0;

    for (char vowel:vowels)
    {
        if(firstLetter==vowel)
        {
            found=1;
            System.out.println(firstLetter+" is a vowel.");
            System.exit(0);

        }
    }

     for (char consonant: consonants)   
        {
            if (firstLetter == consonant)
            {
            found = 2;
            System.out.print(firstLetter+" is a consonant.\n");
            System.exit(0);
            }    
        }
     if (found<=0)
     {
         System.out.println(firstLetter+" is not a vowel or consonant.\n");
         System.exit(0);
     }       
}

}

The formatting was simply incorrect. After cleaning up the code I found that the problem was with my own carelessness.

Thank you everyone!

You need to remove System.exit(0); statements if you want your program to continue execution.

Try replacing

System.exit(0);

with

break;

It will stop the execution of current for loop and start with next loop.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM