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

  • Home
  • SEARCH
  • 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 9014985
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T03:38:24+00:00 2026-06-16T03:38:24+00:00

Could you please point out where is the bug in my code? I have

  • 0

Could you please point out where is the bug in my code?

I have a simple text file with the following data structure:

something1
something2
something3
...

It results a String[] where every element is the last element of the file. I can’t find the mistake, but it goes wrong somewhere around the line.setLength(0);

Any ideas?

public String[] readText() throws IOException {
    InputStream file = getClass().getResourceAsStream("/questions.txt");
    DataInputStream in = new DataInputStream(file);

    StringBuffer line = new StringBuffer();
    Vector lines = new Vector();

    int c;
    try {
        while( ( c = in.read()) != -1 ) {
            if ((char)c == '\n') {
                if (line.length() > 0) {
                    // debug
                    //System.out.println(line.toString());
                    lines.addElement(line);
                    line.setLength(0);
                }
            }
            else{
                line.append((char)c);
            }
        }
        if(line.length() > 0){
            lines.addElement(line);
            line.setLength(0);
        }

        String[] splitArray = new String[lines.size()];
        for (int i = 0; i < splitArray.length; i++) {
            splitArray[i] = lines.elementAt(i).toString();
        }
        return splitArray;

    } catch(Exception e) {
        System.out.println(e.getMessage());
        return null;
    } finally {
        in.close();
    }
}
  • 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-06-16T03:38:25+00:00Added an answer on June 16, 2026 at 3:38 am

    I see one obvious error – you’re storing the same StringBuffer instance multiple times in the Vector, and you clear the same StringBuffer instance with setLength(0). I’m guesing you want to do something like this

     StringBuffer s = new StringBuffer();
     Vector v = new Vector();
    
     ...
     String bufferContents = s.toString();
     v.addElement(bufferContents);
     s.setLength(0);
     // now it's ok to reuse s
     ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Could someone please point out a site where I can find an algorithm to
Could someone please point me in the right direction with respect to OHLC data
The question is self explanatory. Could you please point out a way to put
Could you please point me to an algorithm that takes a (binary) parse tree
Could someone who is familiar with webkit please explain or point me in the
Could someone please suggest why this is happening... I’ve got some code to pretty
Can someone please confirm that the following is a bug with PHP 5.2.13: (thank
I am wondering if you could please help with generating .cpp/.h file from the
very new to Ruby, I've got the following situation. I have a file with
Did you ever had a bug in your code, you could not resolve? I

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.