Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / February 2006

Tip: Looking for answers? Try searching our database.

Repeated search - logic error

Thread view: 
John Smith - 21 Feb 2006 20:57 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,
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.



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.