Daniel O A
Daniel O A

Reputation: 145

My counter does no increase and stays as 1 in java

I have a feeling I may have missed something in my code that is preventing my counter from increasing. Can someone check to see what I may have miss, my counter's values stays as 1.

Code:

public static void count_vowels(String in_file)
{
    {
        File temp = new File(in_file);
        Scanner input_file;
        try
        {
            input_file = new Scanner(temp);
        }
        catch (Exception e)
        {
            System.out.printf("Failed to open file %s\n", in_file );
            return;
        }
        while(input_file.hasNextLine())
        {
            String line = input_file.nextLine();
            System.out.printf("%s\n", line);
            for(int i = 0; i<line.length(); i++)
            {
                char aA = line.charAt(i);
                int counta = 0;
                if(aA == 'a' || aA == 'A')
                {
                    counta++;
                    System.out.println(counta);
                }


            }

        }
        input_file.close();

    }
}

The program calls a file given with 3 lines of text. I added 2 print statements to show the lines and underneath each line the value of the counter is printed. The result is:

The CAT is jumping over an airplane.
1
1
1
1
TODAY IS WEDNESDAY.
1
1
UUUu

It seems to count but does not increment.

Upvotes: 1

Views: 997

Answers (7)

Felix Gerber
Felix Gerber

Reputation: 1651

Your Problem is, that your right here:

 for(int i = 0; i<line.length(); i++)
            {
                char aA = line.charAt(i);
                int counta = 0;
                ...

Your loop is as often executed as you have character in your line. So for every character your programm is declares counta and initializes it as 0. For every loop your creating a "new" variable, always with the value 0.

If you hit the character you want(A and a in your case), your increment counta and print it out. For the first time it's okay, but for every further loop your porgramm is declaring counta and initalzing it with 0.

The solution is simple:

Get the declartion and initialzation out of your loop:

...
while(input_file.hasNextLine())
        {
            String line = input_file.nextLine();
            System.out.printf("%s\n", line);
            //Counta need to be placed right here:
            int counta = 0;
            for(int i = 0; i<line.length(); i++)
            {
                char aA = line.charAt(i);
                //Counta was here before but thats wrong
                if(aA == 'a' || aA == 'A')
                {
                    counta++;
                    System.out.println(counta);
                }


            }

        }
        ...

I hope i was able to help and your understanding your problem better now.

Upvotes: 0

Daniel O A
Daniel O A

Reputation: 145

I moved it outside and got the counter to increase. From here how could I have the counter to continue from line to line instead of having it restart after each line. After the first line it counts the x amount of "a" but it reverts back to 0 for the next line.

Upvotes: 0

SomeJavaGuy
SomeJavaGuy

Reputation: 7357

This is your problem: Your counta variable gets reinitialized after each loop call, since it's only existing in the scope of the loop.

while(input_file.hasNextLine())  {
    String line = input_file.nextLine();
    System.out.printf("%s\n", line);
    int counta = 0; // Move this here if you want to count every a in each line
    for(int i = 0; i<line.length(); i++) {
        char aA = line.charAt(i);
        if(aA == 'a' || aA == 'A') {
           counta++;
           System.out.println(counta);
        }
    }
}

Upvotes: 0

Ankur Singhal
Ankur Singhal

Reputation: 26077

int counta = 0; reset to 0 every time it process line.

move outside the for-loop

Modified code

while(input_file.hasNextLine())
    {
        String line = input_file.nextLine();
        System.out.printf("%s\n", line);
        int counta = 0; // moved outside
        for(int i = 0; i<line.length(); i++)
        {
            char aA = line.charAt(i);

            if(aA == 'a' || aA == 'A')
            {
                counta++;
                System.out.println(counta);
            }


        }

    }

Upvotes: 0

newbieee
newbieee

Reputation: 450

while(input_file.hasNextLine())
    {
        String line = input_file.nextLine();
        System.out.printf("%s\n", line);
        int counta = 0;
        for(int i = 0; i<line.length(); i++)
        {
            char aA = line.charAt(i);

            if(aA == 'a' || aA == 'A')
            {
                counta++;
                System.out.println(counta);
            }


        }

    }

take counta decleration out of for loop

Upvotes: 0

Bhuwan Prasad Upadhyay
Bhuwan Prasad Upadhyay

Reputation: 3056

int counta = 0; 

declare this line outside the for loop

Upvotes: 0

Bruce
Bruce

Reputation: 8849

Move this line out of loop

int counta = 0;

Upvotes: 1

Related Questions