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