goal: try to print an error message if a ticker is not in the file
"Tickers". Only 1 error message per ticker, even if there are say, 10,
attempts to search for the same ticker.
e.g. Ticker file has:
ABC
DEFG
HI
J
search for:
EF
ABC
EF
H
HI
EF
we would have log error messages (i.e. logError) for:
EF
H
problem:
only EF is logged in the error log. (i.e. only the tickers that are
searched repeatedly (and not found) are logged; tickers that are
searched once, but not found, are not logged).
i.e.
we have only logged error messages for:
EF
code:
private String checkTicker(Object ticker, Object exch, String
pName)
{
String inputLine = null;
String returnString = null;
boolean found = false;
Vector failedTickers = this.getFailedTickers();
try
{
FileReader in = new FileReader(fileDir + "/" + "Tickers");
BufferedReader reader = new BufferedReader(in);
while (((inputLine = (reader.readLine())) != null) &&
(found == false))
{
inputLine.trim();
if
(inputLine.compareToIgnoreCase(((String)ticker).trim()) == 0)
{
found = true;
// (Really just a dummy value, since the
// ticker is never used in processing)
returnString = "OK";
}
}
in.close();
reader.close();
if (found == false)
{
// check to see if ticker is already in failed tickers
// if so, then do nothing
// if not found, add this ticker to the list of failed
tickers, and output error message
if ( false == failedTickers.contains( ticker ) ) {
failedTickers.add( ticker );
this.setFailedTickers( failedTickers );
logTickerError("failed, ticker: " +
((String)ticker.trim()), 3);
}
errFlag = true;
returnString = "ERROR";
}
}
catch (Exception e)
{
}
return returnString;
}
Question:
where is my logic error?
tom fredriksen - 21 Feb 2006 22:28 GMT
> goal: try to print an error message if a ticker is not in the file
> "Tickers". Only 1 error message per ticker, even if there are say, 10,
[quoted text clipped - 7 lines]
> Question:
> where is my logic error?
So basically what you want is, a list of containing tickers and an error
message if a search for the ticker fails 2 or more times? Did I get it
right?
If it is then I think you might be doing it a bit more complicated than
necessary.
Use a HashMap (or a HashSet) where they key is the ticker and the value
is a object (Integer or a more complex object which could include other
info) that has a field signaling the status of any historic searches.
To make it easy and clear you could say that the status value behaves
like this:
object is not in the hash = no search yet
1 = one search and miss
1> = still a search but several misses
--> print error message
You only want one error message, at a miss, so the question is whether
you want to report an error when the signal has value 1 or 2.
An optimisation is that you dont need to keep increasing the signal
value after 1, because you know when you are setting it to one or if its
already 1.
Additionally, there are some points of code clarity I would suggest.
1) return a 0 or 1 from the code, if you need it. F.ex to signal that an
error message should be printed. Otherwise use a void instead, it makes
the code clearer to understand.
2) move the file reading to a private method, this makes the the code
clearer and the logic easier to understand. Either have a the private as
a getNext() method or have it return a ArrayList containing all the
parsed data from the file. (you shouldnt use vectors they are deprecated)
/tom
Roedy Green - 24 Feb 2006 20:29 GMT
>only EF is logged in the error log. (i.e. only the tickers that are
>searched repeatedly (and not found) are logged; tickers that are
>searched once, but not found, are not logged).
the usual ways:
1. sort the searches and remove dups before you do them..
2. maintain a HashSet of things you have already searched for.
3. maintain a sorted list of what you have previously searched for. Do
a binary search. This would not work well if you were constantly
adding to the list.

Signature
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.