8 Replies - 380 Views - Last Post: 04 August 2017 - 08:36 AM Rate Topic: -----

#1 Jupiturd  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 16
  • Joined: 24-January 14

Vowel checker

Posted 04 August 2017 - 07:58 AM

I made a simple little program where it checks if there is a vowel in a string that you type but I was curious on how I could make this shorter or more efficient possibly.

package yo;
import java.util.Scanner;
import java.util.Arrays;

public class yo {

	public static void main(String[] args) {
			Scanner scan = new Scanner(System.in);
			char[] vowels = {'a','e','i','o','u'};
			System.out.println("Type an word you want");
			String word = scan.nextLine();
			char[] wordtyped = word.toCharArray();
			for(int cnt = 0; cnt < wordtyped.length; cnt++) {
					if(wordtyped[cnt] == 'a') {
						System.out.println("Vowel found!");
					} else if(wordtyped[cnt] == 'e') {
						System.out.println("Vowel found!");
					} else if(wordtyped[cnt] == 'i') {
						System.out.println("Vowel found!");
					} else if(wordtyped[cnt] == 'o') {
						System.out.println("Vowel found!");
					} else if(wordtyped[cnt] == 'u') {
						System.out.println("Vowel found!");
					} else if(wordtyped[cnt] == 'A') {
						System.out.println("Vowel found!");
					} else if(wordtyped[cnt] == 'E') {
						System.out.println("Vowel found!");
					} else if(wordtyped[cnt] == 'I') {
						System.out.println("Vowel found!");
					} else if(wordtyped[cnt] == 'O') {
						System.out.println("Vowel found!");
					} else if(wordtyped[cnt] == 'U') {
						System.out.println("Vowel found!");
					}
			}
			}
		}



Is This A Good Question/Topic? 0
  • +

Replies To: Vowel checker

#2 ndc85430  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 539
  • View blog
  • Posts: 2,192
  • Joined: 13-June 14

Re: Vowel checker

Posted 04 August 2017 - 08:00 AM

You don't really need all those else ifs do you? You know that the character is a vowel if it's an a or it's an e or ... . Also, you could of course turn the character to it's lower or uppercase form so you only have to check it against either upper or lowercase letters, rather than both.
Was This Post Helpful? 0
  • +
  • -

#3 Jupiturd  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 16
  • Joined: 24-January 14

Re: Vowel checker

Posted 04 August 2017 - 08:02 AM

View Postndc85430, on 04 August 2017 - 08:00 AM, said:

You don't really need all those else ifs do you? You know that the character is a vowel if it's an a or it's an e or ... . Also, you could of course turn the character to it's lower or uppercase form so you only have to check it against either upper or lowercase letters, rather than both.


Yea I tried to do that but I wasn't entirely sure how to do it or set it up I guess you could say without all the "else if" or have it as lower and upper
Was This Post Helpful? 0
  • +
  • -

#4 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 3416
  • View blog
  • Posts: 15,400
  • Joined: 20-September 08

Re: Vowel checker

Posted 04 August 2017 - 08:02 AM

Quote

how I could make this shorter


boolean containsVowel = word.matches("(?i).*[aeiou].*");

Was This Post Helpful? 1
  • +
  • -

#5 NormR  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 635
  • View blog
  • Posts: 4,815
  • Joined: 25-December 13

Re: Vowel checker

Posted 04 August 2017 - 08:03 AM

Another idea: use the String class's indexOf method to see if the character was in a String of all vowels: "aeiouAEIOU"
Was This Post Helpful? 1
  • +
  • -

#6 Jupiturd  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 16
  • Joined: 24-January 14

Re: Vowel checker

Posted 04 August 2017 - 08:11 AM

I did g00se method and it worked thank you guys for the help :bigsmile:/> here is what it looks like now:


package yo;
import java.util.Scanner;
import java.util.Arrays;

public class yo {

	public static void main(String[] args) {
			Scanner scan = new Scanner(System.in);
			char[] vowels = {'a','e','i','o','u'};
			System.out.println("Type an word you want");
			String word = scan.nextLine();
			char[] wordtyped = word.toCharArray();
			boolean containsVowel = word.matches("(?i).*[aeiouAEIOU].*");
			for(int cnt = 0; cnt < wordtyped.length; cnt++) {
					if(containsVowel == true) {
						System.out.println("vowel found!");
						break;
					}
			}
		}
}


Was This Post Helpful? 0
  • +
  • -

#7 ndc85430  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 539
  • View blog
  • Posts: 2,192
  • Joined: 13-June 14

Re: Vowel checker

Posted 04 August 2017 - 08:13 AM

Do you actually understand how that works, though?
Was This Post Helpful? 0
  • +
  • -

#8 Jupiturd  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 16
  • Joined: 24-January 14

Re: Vowel checker

Posted 04 August 2017 - 08:17 AM

Woops forgot to take out the char[] vowels lol ignore that part please

View Postndc85430, on 04 August 2017 - 08:13 AM, said:

Do you actually understand how that works, though?

Yes, I understand why it works but what I have not seen before is the " ?i "

This post has been edited by Jupiturd: 04 August 2017 - 08:18 AM

Was This Post Helpful? 0
  • +
  • -

#9 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 3416
  • View blog
  • Posts: 15,400
  • Joined: 20-September 08

Re: Vowel checker

Posted 04 August 2017 - 08:36 AM

Case-insensitive. You can remove the upper case chars from the pattern
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1