Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6570501
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T14:45:52+00:00 2026-05-25T14:45:52+00:00

I have written some code that count the number of if statements from unknown

  • 0

I have written some code that count the number of “if” statements from unknown number of files. How can i keep a count for each file separate and a total of “if” from all files?

code:

import java.io.*;

public class ifCounter4 
{
    public static void main(String[] args) throws IOException
    {
        // variable to keep track of number of if's
        int ifCount = 0;

        for (int c = 0; c < args.length; c++)
        {
            // parameter the TA will pass in
            String fileName = args[c];

            // create a new BufferReader
            BufferedReader reader = new BufferedReader( new FileReader (fileName));
            String line  = null;
            StringBuilder stringBuilder = new StringBuilder();
            String ls = System.getProperty("line.separator");

            // read from the text file
            while (( line = reader.readLine()) != null) 
            {
                stringBuilder.append(line);
                stringBuilder.append(ls);
            }

            // create a new string with stringBuilder data
            String tempString = stringBuilder.toString();

            // create one last string to look for our valid if(s) in
            // with ALL whitespace removed
            String compareString = tempString.replaceAll("\\s","");

            // check for valid if(s)
            for (int i = 0; i < compareString.length(); i++)
            {
                if (compareString.charAt(i) == ';' || compareString.charAt(i) == '}' || compareString.charAt(i) == '{') // added opening "{" for nested ifs :)
                {
                    i++;

                    if (compareString.charAt(i) == 'i')
                    {
                        i++;

                        if (compareString.charAt(i) == 'f')
                        {
                            i++;

                            if (compareString.charAt(i) == '(')
                                ifCount++;
                        } // end if
                    } // end if
                } // end if

            } // end for

         // print the number of valid "if(s) with a new line after"
         System.out.println(ifCount + " " + args[c]);  // <-- this keeps running total
                                                       // but not count for each file
        }

        System.out.println(); 

    } // end main
} // end class 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T14:45:52+00:00Added an answer on May 25, 2026 at 2:45 pm

    Using an array would solve this problem.

    int[] ifCount = new int[args.length];
    and then in your loop ifCount[c]++;

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have written some code that pulls info from a plist file and does
I have written some code in java that will guess a number based on
I have written some code to ensure that items on an order are all
I have an application written in C# that invokes some C code as well.
I have a c++ source code that was written in linux/unix environment by some
I have some old code written in C for 16-bit using Borland C++ that
Hey everyone, I have written a script that downloads a zip file from a
I have written the following code to perform some simultaneous HTTP posting and file
I have written some code in C to read a binary file containing complex
I have some code written that is attempting to connect to a mysql db.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.