Home > Software engineering >  Count all Vowels in a String
Count all Vowels in a String

Time:11-05

I am working on a program that counts the number of vowels in a string but I am getting an error:

import java.util.Scanner;
public class Main
{
    public static void main(String[] args) {
        int vowels = 0;
        
        
        Scanner input = new Scanner(System.in);
        System.out.println("Enter String: ");
        String string = input.nextLine(); 

        string=string.toLowerCase();
        System.out.println("Vowels: "  vowels);
    }
    public static void countVowels(String string)
    {
        int i;
        for (i=0;i<string.length();i  )
        {
            if (string.charAt(i) = "a" || string.charAt(i) = "e" || string.charAt(i) = "i" || string.charAt(i) = "o" || string.charAt(v) = "u")
            {
                vowels  ; 
            }
        }
    }
}

 

Oh and I want to ask what it means to have methods without return value/with return value. Not sure if the code above has return value.

CodePudding user response:

A lot of wrong things in your code.

In Java you represent a char with single quotes example 'a' 'b', etc.

String is double quotes and is an Object "a", "b".

You can't compare a Char with a String.

You need to comapre char with char.

string.charAt(i) == 'a'

and missing the double equals.

CodePudding user response:

You need to fix following issues,

  1. The scope of variable vowels is set to method level so it is not available outside main method
  2. The method countVowels is never invoked
  3. Assignment operator = is used in place of comparison ==
  4. String literal is used " " in place of char ' '

CodePudding user response:

My code above has many problems.

First, the main class not even calling the method class (countVowels)

Second, I used double quote " " and calling it char. String uses " " and char uses ' '

Third, I used single = instead of ==

Below is the refined code

import java.util.Scanner;
public class Main
{
    public static void main(String[] args) {
     
        Scanner input = new Scanner(System.in);
        System.out.println("Enter String: ");
        String string = input.nextLine(); 

        string=string.toLowerCase();
        countVowels(string);
        
    }
    public static void countVowels(String string) //method class, dito yung parang procedure nung pagbibilang ng vowels
    {
        int v;
        int vowels = 0;
        for (v=0;v<string.length();v  )
        {
            if (string.charAt(v) == 'a' || string.charAt(v) == 'e' || string.charAt(v) == 'i' || string.charAt(v) == 'o' || string.charAt(v) == 'u')
            {
                vowels  ; 
            }
            
        }
        System.out.println("Vowels: "  vowels);
        
        
    }
}

CodePudding user response:

Most of the issues have been answered by others, remaining few left.
Which are, you are not calling your function countVowels() and you have declare your variable vowels in main method, not in your function countVowels() .
Methods with return value:

import java.util.Scanner;
public class Main
{
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.println("Enter String: ");
        String string = input.nextLine(); 
        string=string.toLowerCase();
        int vowels = countVowels(string); // calling function which returns a value.
        System.out.println("Vowels: "  vowels);
    }
    public static int countVowels(String string)
    {
        int i,vowels=0;
        for (i=0;i<string.length();i  )
        {
            if (string.charAt(i) == 'a' || string.charAt(i) == 'e' || string.charAt(i) == 'i' || string.charAt(i) == 'o' || string.charAt(i) == 'u')
            {
                vowels  ; 
            }
        }
        return vowels;
    }
}

Methods without return value:

import java.util.Scanner;
public class Main
{
    public static void main(String[] args) {
        .... // same code as above
        countVowels(string);  // calling funtion, this doesnt return value      
    }
    public static void countVowels(String string)
    {
        int i,vowels=0;
        for (i=0;i<string.length();i  )
        {
            .... // same code as above
        }
        System.out.println("Vowels: "  vowels);
    }
}
  • Related