Have a problem on line 128 : out.println("Location \tAveragePrice"); Not recognize it.And the program not compiling.
Have a txt file(cars.txt) :

CARREG      MAKE    MODEL   COLOUR  LOCATIOn    PRICE       OWNERS  MILEAGE
09-L-1258   OPEL    ASTRA   SILVER  LIMERICK    12999       0   35000
01-D-7845   FORD    FOCUS   BLACK   LIMERICK    1500        3   85000
11-D-7425   NISSAN  ALMERA  WHITE   DUBLIN      21000       0   0
06-C-1758   PEUGEOT A306    SILVER  CORK        9999        2   58500
10-CE-63    HYUNDAI ACCENT  RED DUBLIN      18000       1   12250
11-D-8956   AUDI    A6  BLACK   DUBLIN      75000       0   0
08-W-1373   FORD    FOCUS   BLACK   CORK        7900        2   45989
04-DL-31    OPEL    CORSA   GREEN   CORK        1900        3   67900
10-LK-123   BMW X1  SILVER  DUBLIN      28700       1   14223
98-C-6661   FIAT    BRAVO   YELLOW  CORK        750     5   43900
01-LK-98    RENALT  LAGUNA  GREEN   LIMERICK    995     2   159000
00-KK-1211  SEAT    IBIZA   RED DUBLIN      975     1   110000
11-D-1233   TOYOYA  AVENSIS GREY    DUBLIN      26999       0   0
11-D-1234   TOYOTA  AVENSIS BLACK   DUBLIN      26999       0   0
01-D-31     OPEL    CORSA   BLACK   CORK        1900        3   987
07-D-7845   FORD    FOCUS   BLACK   LIMERICK    6500        1   55000

package cars;

import java.io.*;

public class Cars {

    public static void main(String[] args) {

        // Declare file reader and writer streams
        FileReader frs = null;
        FileWriter fws = null;

        // Declare streamTokenizer
        StreamTokenizer in = null;

        // Declare a print stream
        PrintWriter out = null;

        // temp location of the car
        String location = "";

        // temp vars for avg computation
        double limerickTotal = 0;
        double corkTotal = 0;
        double dublinTotal = 0;
        int limerickCount = 0;
        int dublinCount = 0;
        int corkCount = 0;

        // to hold the avg price of cars in various locations
        double limerickAvg = 0;
        double dublinAvg = 0;
        double corkAvg = 0;

        try {
            // Create file input and output streams
            frs = new FileReader("cars.txt");
            fws = new FileWriter("projections.txt");
            // Create a stream tokenizer wrapping file input stream
            in = new StreamTokenizer(frs);
            out = new PrintWriter(fws);
            //range of white space characters
            in.whitespaceChars('-', '-');

            // Read past col headings.
            in.nextToken(); //CARREG
            in.nextToken(); //MAKE
            in.nextToken(); //MODEL
            in.nextToken(); //COLOUR
            in.nextToken(); //LOCATION
            in.nextToken(); //PRICE
            in.nextToken(); //OWNERS
            in.nextToken(); //MILEAGE

            //read first token of first row (YEAR OF CAR - FROM CARREG)
            in.nextToken();

            // Process a record
            while (in.ttype != StreamTokenizer.TT_EOF) {
                //read second token (COUNTY OF CAR - FROM CARREG)
                in.nextToken();
                //read THIRD token (REG NUMBER OF CAR - FROM CARREG)
                in.nextToken();
                //read FOURTH token (MAKE OF CAR)
                in.nextToken();
                //read FIFTH token (MODEL OF CAR)
                in.nextToken();
                //read SIXTH token (COLOUR OF CAR)
                in.nextToken();
                //read SEVENTH token (LOCATION OF CAR)
                in.nextToken();
                if (in.ttype == StreamTokenizer.TT_WORD) {
                    location = in.sval;
                } else {
                    System.out.println("Bad file structure");
                }
                //read EIGHT token (PRICE OF CAR)
                in.nextToken();
                if (in.ttype != StreamTokenizer.TT_NUMBER) {
                    System.out.println("Bad file structure");
                }
                if (location.equalsIgnoreCase("Limerick")) {
                    limerickCount++;
                    limerickTotal += in.nval;
                } else if (location.equalsIgnoreCase("Dublin")) {
                    dublinCount++;
                    dublinTotal += in.nval;
                } else {
                    corkCount++;
                    corkTotal += in.nval;
                }
                //read NINTH token (OWNERS OF CAR)
                in.nextToken();
                //read TENTH token (MILEAGE OF CAR)
                in.nextToken();
                //read next token of the next line in the file
                in.nextToken();
            }
        } catch (FileNotFoundException ex) {
            System.out.println("File not found: cars.txt");
        } catch (IOException ex) {
            System.out.println(ex.getMessage());
        } finally {
            //compute avg prices in the three locations
            limerickAvg = limerickTotal / limerickCount;
            dublinAvg = dublinTotal / dublinCount;
            corkAvg = corkTotal / corkCount;

            //print info to second file
            out.println("Location    \tAveragePrice");
            out.println("Dublin    " + "\t " + dublinAvg);
            out.println("Cork      " + "\t " + corkAvg);
            out.println("Limerick  " + "\t " + limerickAvg);

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

Recommended Answers

All 2 Replies

A syntax error has nothing to do with the data. If the println() function on line 128 is failing, then you did something wrong in the code before that line. You need to run this in the debugger and single step through the function until you find the root cause. Please show the compiler error.

This is very similar to OP's other post, and contains the same potential null pointer, and uses finally in same bad way.

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.