w3resource

Java Method Exercises: Check all the characters in a string are vowels or not

Java Method: Exercise-23 with Solution

From Wikipedia-
A vowel is a syllabic speech sound pronounced without any stricture in the vocal tract. Vowels are one of the two principal classes of speech sounds, the other being the consonant. Vowels vary in quality, in loudness and also in quantity (length). They are usually voiced and are closely involved in prosodic variation such as tone, intonation and stress.

Write a Java method that checks whether all the characters in a given string are vowels (a, e,i,o,u) or not. Return true if each character of the string is a vowel, otherwise return false

Sample Data:
AIEEE ->true
IAO -> true
Java -> false
Python -> false

Pictorial Presentation:

Java Method Exercises: Check all the characters in a string are vowels or not
Java Method Exercises: Check all the characters in a string are vowels or not

Sample Solution:

Java Code:

import java.util.Scanner;
public class Main {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    System.out.print("Input a string: ");
    String str = sc.nextLine();
    System.out.print("Check all the characters of the said string are vowels or not!\n");
    System.out.print(test(str));
  }

  public static boolean test(String input) {
    String str_vowels = "aeiou";
    String phrase = input.toLowerCase();
    for (int i = 0; i < phrase.length(); i++) {
      if (str_vowels.indexOf(phrase.charAt(i)) == -1)
        return false;
    }
    return true;
  }
}

Sample Output:

Input a string:  AIEEE
Check all the characters of the said string are vowels or not!
true

Flowchart :

Flowchart: Check all the characters in a string are vowels or not

Java Code Editor:

Contribute your code and comments through Disqus.

Previous Java Exercise: Check whether every digit of a given integer is even.
Next Java Exercise: Java Number Exercises

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Share this Tutorial / Exercise on : Facebook and Twitter

Java: Tips of the Day

PDF Export:

Export a table to PDF is a common requirement in Java programs. Using itextpdf, it becomes really easy to export PDF.

import java.io.FileOutputStream;
import com.itextpdf.text.Document;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
 
public class DrawPdf {
 
      public static void main(String[] args) throws Exception {
        Document document = new Document();
        PdfWriter.getInstance(document, new FileOutputStream("Employee.pdf"));
        document.open();
         
        Paragraph para = new Paragraph("Employee Table");
        para.setSpacingAfter(20);
        document.add(para);
         
        PdfPTable table = new PdfPTable(3);
        PdfPCell cell = new PdfPCell(new Paragraph("First Name"));
 
        table.addCell(cell);
        table.addCell("Last Name");
        table.addCell("Gender");
        table.addCell("Ram");
        table.addCell("Kumar");
        table.addCell("Male");
        table.addCell("Lakshmi");
        table.addCell("Devi");
        table.addCell("Female");
 
        document.add(table);
         
        document.close();
      }
    }

Ref: https://bit.ly/3DwKJ1h