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 7558071
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T12:17:03+00:00 2026-05-30T12:17:03+00:00

I need some help writing a class that can go through other java files

  • 0

I need some help writing a class that can go through other java files and show the class name, int variable names, and comments.

I have a test class that I’m attempting to parse through here.

public class Test {
    private int x;
    private int y;
    private String s;
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        // more comments
        int l; //local variable
        l = 0;
    }
}

The output I’m looking to obtain:

The Class name is : Test
There is an int variable named: x
There is an int variable named: y
Comment contains:  TODO Auto-generated method stub
Comment contains:  more comments
There is an int variable named: l
Comment contains: local variable

Here is the code for the class I have now:

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.InputStreamReader;

class ExtractJavaParts {

    public static void main(String args[]){

        try{
            // Open the file that is the first 
            // command line parameter
            FileInputStream fstream = new FileInputStream("src/Test.Java");

            // Get the object of DataInputStream
            DataInputStream in = new DataInputStream(fstream);
            BufferedReader br = new BufferedReader(new InputStreamReader(in));
            String strLine;

            //Read File Line By Line            
            while ((strLine = br.readLine()) != null){
                // Print the content on the console
                if (strLine.contains("class")){
                    System.out.println ("The class name is: " + strLine.substring(strLine.indexOf("class ") + 6, strLine.indexOf("{")));
                }
                else if (strLine.contains("int")){
                    System.out.println("There is an int variable named: " + strLine.substring(strLine.indexOf("int ") + 4, strLine.indexOf(";")));
                }
                else if (strLine.contains("//")){
                    System.out.println("Comment contains: " + strLine.substring(strLine.indexOf("//") + 2));
                }
            }

            //Close the input stream
            in.close();
        }

        catch (Exception e){
            //Catch exception if any
            System.err.println("Error: " + e.getMessage());
        }
    }
}

This is the output currently:

The class name is: Test 
There is an int variable named: x
There is an int variable named: y
Comment contains:  TODO Auto-generated method stub
Comment contains:  more comments
There is an int variable named: l

The program as of now will not pick up on comments that occur after code. Any help you can provide to get the desired output is greatly appreciated. Thanks so much!

  • 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-30T12:17:04+00:00Added an answer on May 30, 2026 at 12:17 pm

    The problem is that in your code you have an int followed by a comment.

    When that line is read, it goes into the first “else if” statement and then goes to the next line.

    Try using 3 if statements instead of one “if” and two “else if”s

    The problem is that for any line, it can go through ONLY ONE conditional statement the way you have it coded. Which means if you have a comment AND an int on the same line, it will only find the int, and then continue to the next iteration of the loop

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

Sidebar

Related Questions

I need some help with a program I'm writing for my Programming II class
I need some help writing an http client. The trouble comes when I try
I'm new to web programming, so I need some help. I am writing a
Need some help about with Memcache. I have created a class and want to
Need some help assigning a mouseover event to display some icons that start out
I am writing a WPF application using C# and I need some help with
I need some help with a rails development that I'm working on, using rails
I'm writing a Java component that will be doig pretty heavy-duty work on some
Need some help, please. I have a line of horizontal thumbnails loaded as ONE
Need some help to solve this. I have a gridview and inside the gridview

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.