Hi,please help.Stack with streamtokens.I want to figure out how to print the table :

County          Max          Min    Condition
Clare           4            0  Cloudy
Cork            3            0  Rain
Kerry           4            1  Showers
Limerick    4            1  Cloudy
Tipperary   3           -1  Clear
Waterford   2           -2  Clear

in format :

County  Average Daily Temp  Freeze Risk
Clare   2   NO
Cork    1.5 NO
Kerry   2.5 NO
Limerick    2.5 NO
Tipperary   1   NO
Waterford   0   YES

My code is :

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package weather;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StreamTokenizer;

/**
 *
 * @author Arthur
 */
public class Weather {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        FileReader flr = null;
        FileWriter fwr = null;
        StreamTokenizer streamIn = null;
        PrintWriter printOut = null;
        String county ="";
        double clareTotal =0;
        double clareAvg =0;
        double corkTotal =0;
        double corkAvg =0;
        double kerryTotal =0;
        double kerryAvg =0;
        double limerickTotal =0;
        double limerickAvg =0;
        double tipperaryTotal=0;
        double tipperaryAvg =0;
        double waterfordTotal =0;
        double waterfordAvg =0;
        int clareCount =0;
        int corkCount =0;
        int kerryCount =0;
        int limerickCount =0;
        int tipperaryCount =0;
        int waterfordCount =0;

        try {

                    flr = new FileReader("weather.txt");
                    fwr = new FileWriter("averagetemps.txt");

                    streamIn = new StreamTokenizer(flr);
                    printOut = new PrintWriter(fwr);

                    streamIn.nextToken();//County
                    streamIn.nextToken();//Max
                    streamIn.nextToken();//min
                    streamIn.nextToken();//condition
                 //   streamIn.nextToken();//read first county

                    while (streamIn.ttype != StreamTokenizer.TT_EOF){
                    streamIn.nextToken();

                        if (streamIn.ttype != StreamTokenizer.TT_NUMBER)
                        {
                          //  System.out.println("Bad file structure");
                                    if (county.equalsIgnoreCase("Clare")) {
                                       clareCount++;
                                       clareTotal += streamIn.nval;
                                   }
                                    if (county.equalsIgnoreCase("Cork")) {
                                       corkCount++;
                                       corkTotal+= streamIn.nval;
                                    }
                                   if (county.equalsIgnoreCase("Kerry")) {
                                       kerryCount++;
                                       kerryTotal+= streamIn.nval;
                                   }
                                    if (county.equalsIgnoreCase("Limerick")) {
                                       limerickCount++;
                                       limerickTotal+= streamIn.nval;
                                    }
                                   if (county.equalsIgnoreCase("Tipperery")) {
                                       tipperaryCount++;
                                       tipperaryTotal+= streamIn.nval;
                                   }
                                           else {
                                       waterfordCount++;
                                       waterfordTotal+=streamIn.nval;
                                   }
                        }
                                       if (streamIn.ttype == StreamTokenizer.TT_WORD)
                                      county = streamIn.sval;
                                 else
                                       System.out.println("Bad file structure");
                                  streamIn.nextToken(); 

                }

                            }

         catch (FileNotFoundException ex)   {
            System.out.println("File not found: weather.txt");
        }
        catch (IOException ex)   {
            System.out.println(ex.getMessage());
        }
         finally   {
            //compute avg prices in the three locations
        clareAvg = clareTotal/clareCount;
        corkAvg = corkTotal/corkCount;
        kerryAvg = kerryTotal/kerryCount;
        limerickAvg = limerickTotal/limerickCount;
        tipperaryAvg = tipperaryTotal/tipperaryCount;
        waterfordAvg = waterfordTotal/waterfordCount;

        String freezrisk ="";
        if (clareAvg <=0 || corkAvg <=0 || kerryAvg<=0 || limerickAvg<=0 ||tipperaryAvg <=0 ||waterfordAvg<=0)
        {
           freezrisk = "YES";
        }
        else{
        freezrisk ="NO";
        }

            //print info to second file
            printOut.println("County    \tAverageDailyTemp     \tFreeze risk");
            printOut.println("Clare    " + "\t " + clareAvg  + "\t " + freezrisk);
            printOut.println("Cork    " + "\t " + corkAvg  + "\t " + freezrisk);
            printOut.println("Kerry   " + "\t " + kerryAvg + "\t " + freezrisk);
            printOut.println("Limerick    " + "\t " + limerickAvg  + "\t " + freezrisk);
            printOut.println("Tipperery    " + "\t " + tipperaryAvg + "\t " + freezrisk);
            printOut.println("Waterford   " + "\t " + waterfordAvg  + "\t " + freezrisk);

            try     {
                if (flr != null) flr.close();
                if (fwr != null) fwr.close();
            }
            catch (IOException ex)  {
                System.out.println(ex);
            }
        }

    }

}

I assume i do something wrong in try block

"I assume i do something wrong" tells us nothing. If you have a compiler or runtime error message post the complete message plus the actual code it refers to. If your code is giving an incorrect result explain what result you get and what the correct result should be.

Also, if you don't get errors, a copy of your output would be helpful. You say at the bottom of your code that you assume you have a problem in the try/catch block. Does it throw an exception? Are you catching it? Also, you have 2 try/catch blocks. Which is the problem one?

Compiling with all the diagnostics on (which you should always do) just shows that printOut on line 129 etc may be uninitialised. That's right - if lines 52 thru 55 throw an exception then the finally block will be excuted with printOut still null

This code uses finally in bad way - finally is intended for cleaning up whatever needs to be cleaned up from the try or catch, not for executing the rest of the method even after an exception.

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.