Ian Lundberg
Ian Lundberg

Reputation: 1883

String line limit?

I am writing a method that will read from a text file and tell how many people in that text file are born within the years 1920 to 1984 inclusive, but for some reason it will only ever store a maximum of 45 lines.

private Buffin itsFile = new Buffin("workers.txt");
private static final int TIME_SPAN = 65;
private static final int YEAR = 1920;

public String countBirthYears()
{
    try
    {
        int[] count = new int[TIME_SPAN];

        //== READ THE WORKER DATA AND UPDATE THE COUNTS
        Worker data = new Worker(itsFile.readLine());
        while(data.getName() != null)
        {
            if((data.getBirthYear() - YEAR) >= 0 && (data.getBirthYear() - YEAR) <= 1984){
                int lastDigit = data.getBirthYear() % 10;
                count[lastDigit]++;
            }

            data = new Worker(itsFile.readLine());
        }

        //== CONSTRUCT THE STRING OF ANSWERS
        String s = "";
        for(int k = 0; k < TIME_SPAN; k++)
            s += (YEAR + k) + " : " + count[k] + " workers\n";
        return s;
    }catch(Exception e){JOptionPane.showMessageDialog(null, e); return "";}
}

I would appreciate if someone could explain to me as to why this is happening and how to fix it. thanks :)

Upvotes: 0

Views: 509

Answers (3)

Dario
Dario

Reputation: 548

Well, this is not a real answer actually but I would like to address a couple of things in your code and I hope it can help you:

1.

if((data.getBirthYear() - YEAR) >= 0 && (data.getBirthYear() - YEAR) <= 1984)

Does not actually check for birthYears within 1920 and 1984, but it includes all the years between 1920 and 3904

I think you meant:

if((data.getBirthYear() - YEAR) >= 0 && (data.getBirthYear() - YEAR) <= TIME_SPAN)

2.

int lastDigit = data.getBirthYear() % 10;
count[lastDigit]++;

Actually adds up people born in 1973 and those born in 1983 (for instance)... is this what you really want?

Maybe the problem is not in String limits but there might be something in the logic...

Upvotes: 2

Dropout
Dropout

Reputation: 13876

The size of the String is limited in Java. The amount of chars the String object can hold is actually equal to Integer.MAX_VALUE. If you are exceeding this lenght you should consider using StringBuffer or StringBuilder to create your output based on if you work with it in different threads (StringBuffer is synchronized).

You can use

StringBuffer sb = new StringBuffer();
for(int k = 0; k < TIME_SPAN; k++)
        s.append(YEAR + k).append(" : ").append(count[k]).append(" workers\n");

return sb; //sb.toString();

Note: If you have a small heap size you may be even more limited. The String maximum length is either Integer.MAX_VALUE or half your heap size, whichever is smaller.

Upvotes: 0

AlexR
AlexR

Reputation: 115418

Yes, string is limited. As far as I remember the limit depends on JVM and is something like 64K.

But it even does not matter. You do not have to read whole file into one string to detect how many people match your criteria. Even if there is no limitation on string size. What if your file contains list of all 7 billion people? Do you want to hold in memory the whole list just to return a simple counter in the end of your run?

You have to read your file line-by-line, parse each line and increment your counter if it is needed. No need to store the whole information in RAM.

Upvotes: -1

Related Questions