Question:
How to read string and print out all vowels contained in the string.
Code:
import java.util.Scanner; public class GetVowels { public static void main(String[] args) { String vowels = "AEIOUaeiou"; System.out.print("Enter a string: "); Scanner in = new Scanner(System.in); String str = in.nextLine(); for(int i=0;i<str.length();i++){ String onechar = str.substring(i,i+1); if( vowels.indexOf(onechar) != -1){ System.out.print(onechar); } } System.out.println(); } }
Output:
$ java GetVowels Enter a string: abcDEFghi aEi