-3

I am trying to run the following code:

import textio.TextIO;

public class Main{
    public static void main(String[] args){
        String str; // Line of text entered by the user.
        System.out.println("Please type in a line of text.");
        str = TextIO.getln();
        int vcount; 
        int ccount; 
        char y[] = str.toCharArray();
        int size = y.length;
        
        int i = 0;
        while(i != size)
        {
            if(y[i]>='A' && y[i]<='Z')
            {
                if(y[i]=='A'||y[i]=='E'||y[i]=='I'||y[i]=='O'||y[i]=='U')
                {
                    ++vcount;
                }
                else
                {
                    ++ccount;
                }
                ++i;
            }
            int ratio = vcount/ccount;
            System.out.println("The vowel/consonant ratio of arithmetic is" + ratio);
        }
    }
}

This code is not compiling because the compiler says the variables vcount and ccount are not initialized. I think I have already initialized both these variables at the beginning of the code. Where am I going wrong?

PM 77-1
  • 12,933
  • 21
  • 68
  • 111
Ricky_Nelson
  • 133
  • 1
  • 5
  • @PM77-1 Sorry, I am very new to Java, and I don't see where I am going wrong in my particular code. – Ricky_Nelson Feb 20 '22 at 22:41
  • 1
    You ***declared*** your variables but did not ***initialize*** them. Local variables are not assigned default values. You needed something like `int v=0;`. – PM 77-1 Feb 20 '22 at 22:45

1 Answers1

0

While declaring variables, compiler knows that somewhere in memory this variable exists and you can call this variable later.

Initializing variable means that you need to assign value before using this variable

Frendom
  • 508
  • 6
  • 24