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...");

    }

}

Recommended Answers

All 4 Replies

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.

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".

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!

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!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.