Users Pricing

forum

home / developersection / forums / easy way to find vowels and print them in java

easy way to find vowels and print them in JAVA

Jayden Bell 2548 05 Nov 2014
Hey this is my first time posting! I got my program to print out the vowels from an input from user but I feel like I have repeated myself a lot in the for loop. Is there a quicker way to do this? Also is this code readable and in the correct format?

import java.util.Scanner;
ublic class Task09 {
public static void main(String[] args)
{
 
   Scanner input = new Scanner(System.in);
   String vowels ="";
   String answer= input.next()
   for(int i = 0 ;i<answer.length();i++)
   {
       char answerPosition = answer.charAt(i);
       if (answerPosition =='a'
           ||answerPosition  =='e'
           ||answerPosition  =='i'
           ||answerPosition =='o'
           ||answerPosition =='u'
           ||answerPosition =='A'
           ||answerPosition =='I'
           ||answerPosition =='O'
           ||answerPosition =='U')
        {
              vowels += answerPosition + " ";
        }
 
    }
 }

import java.util.Scanner;
ublic class Task09 {
public static void main(String[] args)
{
 
   Scanner input = new Scanner(System.in);
   String vowels ="";
   String answer= input.next()
   for(int i = 0 ;i<answer.length();i++)
   {
       char answerPosition = answer.charAt(i);
       if (answerPosition =='a'
           ||answerPosition  =='e'
           ||answerPosition  =='i'
           ||answerPosition =='o'
           ||answerPosition =='u'
           ||answerPosition =='A'
           ||answerPosition =='I'
           ||answerPosition =='O'
           ||answerPosition =='U')
        {
              vowels += answerPosition + " ";
        }
 
    }
System.out.println("The vowels are:" + vowels);
input.close();
 }
}


Jayden Bell

Other


1 Answers