java - String line limit? -


i writing method read text file , tell how many people in text file born within years 1920 1984 inclusive, reason ever store 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 worker data , update 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 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 appreciate if explain me why happening , how fix it. :)

well, not real answer address couple of things in code , hope can you:

1.

if((data.getbirthyear() - year) >= 0 && (data.getbirthyear() - year) <= 1984) 

does not check birthyears within 1920 , 1984, includes years between 1920 , 3904

i think meant:

if((data.getbirthyear() - year) >= 0 && (data.getbirthyear() - year) <= time_span) 

2.

int lastdigit = data.getbirthyear() % 10; count[lastdigit]++; 

actually adds people born in 1973 , born in 1983 (for instance)... want?

maybe problem not in string limits there might in logic...


Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

android - Keyboard hides my half of edit-text and button below it even in scroll view -

css - Make div keyboard-scrollable in jQuery Mobile? -