0
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

public class DataStreamExample {

    public static void main(String[] args) throws IOException {
        File f = new File("seminar.txt");

        if (f.exists()) {
            BufferedReader br = new BufferedReader(new FileReader(f));

            String dat;
            StringBuilder text = new StringBuilder();
            while ((dat = br.readLine()) != null) {
                text.append(dat);
            }
            String strArray[] = text.toString().replace("January", "").replace("February","").replace("March", "").replace("Week","").trim().split(" ");

            boolean flag=false;
            Integer sum=0;
            for(String s :strArray) {
                if(flag) {
                    try {
                        sum= sum + Integer.parseInt(s);
                    }catch(Exception e) {}
                    flag=false;
                }else
                    flag=true;
            }
            System.out.println("Total Number of Student : "+sum);

        } else
            System.out.println("File Not Found...");

    }

}
4
Contributors
4
Replies
58
Views
1 Month
Discussion Span
Last Post by JamesCherrill
2

What is it about that code that you don't understand?

ps: although it's legal Java, it's not very good code - obviously written by a beginner. Don't use it as an example to follow.

Edited by JamesCherrill

0

Hi,

It's a parsing code, and as Venkat Subramaniam said : "don't show your parsing code to any one, it is always ugly" :)

So, start by looking in "seminar.txt".

0

On the surface the code is reading the contents of seminar.txt file, although we don't know what is in that file other than possibly an integer per month that represents a student tally? While we don't know the contents and format of the input file, I suspect the parsing could be coded a little cleaner and easier to read. The purpose of if(flag) is a bit puzzling!

1

The purpose of if(flag) is a bit puzzling!

AFAICS it toggles on each pass of the loop so that the code on line 26 etc is executed on every other string. And yes, there are better ways to do that!

Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.