BufferedReader == reader.mark() ERROR

This is a basice i/o program. It reads data, stores it in an array and then writes a file based on a filtering system.

The code:

import java.io.*;

import java.util.StringTokenizer;

import java.util.NoSuchElementException;

import java.util.ArrayList;

import java.lang.String;

import java.lang.IndexOutOfBoundsException;

publicclass bigbook{

public bigbook()

{

Day = 0;

Open = 0;

High = 0;

Low = 0;

Close = 0;

Vol = 0;

Trade = 0;

Shares = 0;

TradingDay = 0;

PL = 0;

ExitDay = 0;

}

publicvoid enterDay(double date)

{

Day = date;

}

publicvoid enterOpen(double open)

{

Open = open;

}

publicvoid enterHigh(double high)

{

High = high;

}

publicvoid enterLow(double low)

{

Low = low;

}

publicvoid enterClose(double close)

{

Close = close;

}

publicvoid enterTradingDay(int day)

{

TradingDay = day;

}

publicvoid enterPL(double pandl)

{

PL = pandl;

}

publicvoid enterVol(double vol)

{

Vol = vol;

}

publicvoid enterExit(double dayfive)

{

ExitDay = dayfive;

}

publicvoid enterExitPrice(double exitspot)

{

ExitPrice = exitspot;

}

publicdouble getExitPrice()

{

return ExitPrice;

}

publicdouble getExit()

{

return ExitDay;

}

publicdouble getHigh()

{

return High;

}

publicdouble getPL()

{

return PL;

}

publicdouble getClose()

{

return Close;

}

publicdouble getShares()

{

return Shares;

}

publicdouble getDate()

{

return Day;

}

publicdouble getPosition()

{

return Trade;

}

publicint getTradingDay()

{

return TradingDay;

}

publicdouble getVolume()

{

return Vol;

}

publicvoid numShares()

{

Shares = 100000/Close;

}

publicvoid goLong()

{

Trade = 1;

}

publicvoid sellShort()

{

Trade = -1;

}

privatedouble Day;

privatedouble Open;

privatedouble High;

privatedouble Low;

privatedouble Close;

privatedouble Vol;

privatedouble Trade;

privatedouble Shares;

privateint TradingDay;

privatedouble PL;

privatedouble ExitDay;

privatedouble ExitPrice;

privatestaticvoid ReadFile(BufferedReader reader)throws IOException{

String nextLine = reader.readLine();// read first line from br

int runCount = 0;

int closeCount = 0;

ArrayList theDayList =new ArrayList();

ArrayList theResultsList =new ArrayList();

StringTokenizer td;

do

{

while ( runCount<2500 && nextLine !=null){

int roc = 10;

// roc == run overlap count

runCount++;

bigbook Zbigbook =new bigbook();

td =new StringTokenizer(nextLine,",");

while (td.hasMoreTokens()){

Zbigbook.enterDay(Double.parseDouble((String)td.nextToken()));

Zbigbook.enterOpen(Double.parseDouble((String)td.nextToken()));

Zbigbook.enterHigh(Double.parseDouble((String)td.nextToken()));

Zbigbook.enterLow(Double.parseDouble((String)td.nextToken()));

td.nextToken();

Zbigbook.enterVol(Double.parseDouble((String)td.nextToken()));

Zbigbook.enterClose(Double.parseDouble((String)td.nextToken()));

Zbigbook.enterTradingDay(closeCount);

}

theDayList.add(Zbigbook);

nextLine = reader.readLine();

if(runCount == 2000)

{

reader.mark(roc);

}

if(runCount == 2500 || nextLine ==null)

{

reader.reset();

closeCount += runCount;

ArrayList theInterimList = (ArrayList)Test(theDayList);

for (int i=0; i<theInterimList.size(); i++){

try{

bigbook Xbigbook = (bigbook)theInterimList.get(i);

theResultsList.add(Xbigbook);

}

catch (IndexOutOfBoundsException e){

break;

}

}

if(nextLine ==null){

PrintWriter pw;

System.out.print("Send output where? ");

String dest = (new BufferedReader (new InputStreamReader(System.in))).readLine();

if (dest.equals("System.out"))

pw =new PrintWriter(System.out);

else

pw =new PrintWriter(new FileWriter(dest));

pw.println("Runs " + closeCount +" closes.");

for (int i=0; i><theResultsList.size(); i++){

try{

bigbook Ybigbook = (bigbook)theResultsList.get(i);

pw.println(Ybigbook.getDate()+"," + Ybigbook.getVolume()+","

+Ybigbook.getClose());

}

catch (IndexOutOfBoundsException e){

break;

}

}

pw.close();

}

runCount = 0;

theDayList =new ArrayList();

}

}

}

while(nextLine !=null);

}

privatestatic ArrayList Test(ArrayList list)throws IOException{

ArrayList theRelayedList = list;

ArrayList theVolList =new ArrayList();

for (int i = 0; i><theRelayedList.size(); i++)

{

try{

bigbook Abigbook = (bigbook)theRelayedList.get(i);

double volume = Abigbook.getVolume();

if(volume > 1000)

{

theVolList.add(Abigbook);

}

}

catch (IndexOutOfBoundsException e){

break;

}

}

return theVolList;

}

publicstaticvoid main(String[] args){

String filename;

// Prompt user for file name

System.out.print("Enter file name: " );

// Read user's input from System.in

InputStreamReader isr =new InputStreamReader(System.in);

BufferedReader br =new BufferedReader(isr);

FileReader fr;

// Iteratively prompt user until file found

try{

while (true){

filename = br.readLine();

try{

fr =new FileReader(filename);

break;

}

catch (FileNotFoundException e){

System.out.print("Re-enter file name: ");

}

}

ReadFile(new BufferedReader (fr));

}

catch (IOException e){

System.out.println(e);

return;

}

}

}

The problem is that the mark() method is becoming invalidated.

This is the first time I've tried to implement the mark method. I know that the reset() method is supposed to start the reader back at the place where I called mark().

Does it have something to do with the if statement where the mark() call is?

[15221 byte] By [Imamica] at [2007-10-3 2:43:18]
# 1

Your call to mark, if you make it, only preserves 10 characters (if you set that variable to 10). But you seem to do 50 things between calling mark() and reset(). To me that doesn't compute.

You may guess I didn't look too closely at your code. Most of it scrolled off the right side of my screen and there was way too much that had nothing to do with the problem at hand.

DrClapa at 2007-7-14 20:31:39 > top of Java-index,Java Essentials,Java Programming...
# 2

private static void ReadFile(BufferedReader reader) throws IOException {

String nextLine = reader.readLine(); // read first line from br

int runCount = 0;

int closeCount = 0;

ArrayList theDayList = new ArrayList();

ArrayList theResultsList = new ArrayList();

StringTokenizer td;

do

{

while ( runCount<2500 && nextLine != null) {

int roc = 1000;

// roc == run overlap count

runCount++;

bigbook Zbigbook = new bigbook();

td = new StringTokenizer(nextLine, ",");

while (td.hasMoreTokens()){

Zbigbook.enterDay(Double.parseDouble((String)td.nextToken()));

Zbigbook.enterOpen(Double.parseDouble((String)td.nextToken()));

Zbigbook.enterHigh(Double.parseDouble((String)td.nextToken()));

Zbigbook.enterLow(Double.parseDouble((String)td.nextToken()));

td.nextToken();

Zbigbook.enterVol(Double.parseDouble((String)td.nextToken()));

Zbigbook.enterClose(Double.parseDouble((String)td.nextToken()));

Zbigbook.enterTradingDay(closeCount);

}

theDayList.add(Zbigbook);

nextLine = reader.readLine();

if(runCount == 2000)

{

reader.mark(roc);

}

if(runCount == 2500 || nextLine == null)

{

reader.reset();

This is where the problem occurs.

In the previous post the readAheadLimit was set at 10, now I have it at 1000.

All that's happening between the mark() and reset() really is tokenizing each line and storing it to the the DayList ArrayList.

The way I understand it is that - each time the reader reads a line, that counts as 1 .................................

OH! this is defined it terms of Characters, not lines from the reader!

This may make a difference.

Imamica at 2007-7-14 20:31:39 > top of Java-index,Java Essentials,Java Programming...
# 3

I was also unable to look at your code for the following reasons:

1) .NET style capitalization

2) deep statement block nesting

>This is the first time I've tried to implement the mark method.

You mean the first time you've tried to use the mark method.

>I know that the reset() method is supposed to start the reader back at the

>place where I called mark().

Unless, as you've discovered, the mark is invalidated.

Use a larger readAheadLimit.

IanSchneidera at 2007-7-14 20:31:39 > top of Java-index,Java Essentials,Java Programming...
# 4
>>1) .NET style capitalizationI don't get this. Usually when people code in .NET, the use caps like DayList?Also implement and use sorta mean the same thing, no?Thanks for the help. I didn't realize it was characters and not lines from the reader.readLine().
Imamica at 2007-7-14 20:31:39 > top of Java-index,Java Essentials,Java Programming...
# 5

>I don't get this.

http://java.sun.com/docs/codeconv/html/CodeConventions.doc8.html#367

>Also implement and use sorta mean the same thing, no?

No.

Implement:

To accomplish; to fulfill.

Use:

The act of employing anything, or of applying it to one's service;

Example:

My implementation uses a BufferedReader.

IanSchneidera at 2007-7-14 20:31:39 > top of Java-index,Java Essentials,Java Programming...