Mailing List Archive

Support open source code!


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

java question



    B0Ti> Maybe it's trivial, but I cannot find a clean solution to
    B0Ti> this.  I need to read in an integer and it must be within a
    B0Ti> limit. Something like this:

try {
    int someInt = Integer.parseInt(readUserInput());
    if (someInt > 10 || someInt < 0) throw new NumberFormatException;
}
catch (NumberFormatException) { /* goto begin :( ...*/ }

    B0Ti> How should this be done cleanly and effectively?

Recursively.

This is why functional programming rules.  In Lisp it looks like this:

(defun integer-mung ()
   (let ((i (read)))
      (if (and (> i 10) (< i 0)) ; check range
          (integer-mung)
        (throw 'value i))))

(catch 'value (integer-mung))

(In Scheme this would be detected as tail-recursive, and executed as a
loop as efficient as could be hand-coded within the restrictions of
Scheme, using no extra stack space.)

The same strategy should work (I don't know Java syntax anymore,
sorry) but it may not be possible to do this efficiently in Java
without a loop.  In Lisp exceptions are not inefficient because of the
need to maintain closures all the time anyway.  OTOH, no matter how
inefficient Java exceptions are, a human user will never notice the
time lag.


-- 
University of Tsukuba                Tennodai 1-1-1 Tsukuba 305-8573 JAPAN
Institute of Policy and Planning Sciences       Tel/fax: +81 (298) 53-5091
_________________  _________________  _________________  _________________
What are those straight lines for?  "XEmacs rules."


Home | Main Index | Thread Index

Home Page Mailing List Linux and Japan TLUG Members Links