-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?

2
  • @PM77-1 Sorry, I am very new to Java, and I don't see where I am going wrong in my particular code. Commented Feb 20, 2022 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;. Commented Feb 20, 2022 at 22:45

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.