d3xtile-
d3xtile-

Reputation: 1

Problem with Java code that will print TRUE when a word has no vowels in an IF statement

I am trying to write a code the will print TRUE when i enter a word with no vowels and will print FALSE if I enter a word with vowels. I am having trouble when it comes to writing the string. I give one of the strings the value that holds all the values. String A = "aeiou"; The other 2 strings hold the rest of the alphabet. When I type in a word that has vocals it prints FALSE however when I type in a word with no vowels it prints FALSE. I wanna know if there is a way to make the program read the values of the string as separate entities and not as a whole line.

package lab3;

import java.util.Scanner;

public class Lab3 {

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

        /* A and B, but not C */
        String A = "bcdfghjklm";
        String B = "npqrstvwxyz";
        String C = "aeiou";

        if (input.contains(A) && input.contains(B) || input.contains(C)) {
            System.out.println("TRUE");
        } else {
            System.out.println("FALSE");
        }
    }
}

Upvotes: 0

Views: 759

Answers (3)

mhd
mhd

Reputation: 103

You can do it with a short function

import java.util.Scanner;

public class Test {
    public static void main(String[] args) {
        Scanner reader = new Scanner(System.in);
        String input = reader.next();
        System.out.println(chk(input));
    }

    static public boolean chk(String s) {
        for (int i = 0; i < s.length(); i++) {
            if ("aeiou".indexOf(s.charAt(i)) != -1) {
                return false;
            }
        }
        return true;
    }
}

Upvotes: 2

Arvind Kumar Avinash
Arvind Kumar Avinash

Reputation: 79035

You can choose to use any of the below two methods, containsVowel and containsVowel1 as both of them do the same thing:

import java.util.Arrays;
import java.util.Scanner;

public class Test {
    public static void main(String args[]) {
        Scanner reader = new Scanner(System.in);
        System.out.println("Enter a text: ");
        String input = reader.nextLine();
        String[] vowels = { "a", "e", "i", "o", "u" };
        System.out.println(!containsVowel(input, vowels));
        System.out.println(!containsVowel1(input, vowels));
    }

    public static boolean containsVowel(String input, String[] vowels) {
        return Arrays.stream(vowels).parallel().anyMatch(input::contains);
    }

    public static boolean containsVowel1(String input, String[] vowels) {
        for (int i = 0; i < vowels.length; i++) {
            if (input.contains(vowels[i])) {
                return true;
            }
        }
        return false;
    }
}

Upvotes: 1

I love coding
I love coding

Reputation: 1191

This is a working correct for your purpose:

public static void main(String[] args) {
    Scanner reader = new Scanner(System.in);
    String input = reader.next();
    String C = "aeiou";
    for (int i = 0; i < input.length(); i++) {
        char inputA = input.charAt(i);
        for (int j = 0; j < C.length(); j++) {
            char c = C.charAt(j);
            if (inputA == c) {
                System.out.println("TRUE");
                return;
            }
        }
    }
    System.out.println("FALSE");
}

The key point is to use the method charAt(int index); so you can check each char in the string.

Upvotes: 1

Related Questions