Mailing List Archive

Support open source code!


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

Re: java question



On Thu, Jun 14, 2001 at 04:27:00PM +0900, B0Ti wrote:
> Thomas O'Dowd wrote:
> 
> > use a loop with an exit condition, or an infinite loop with a break, or ...
> >
> > int someInt;
> > while()
> > {
> >         try
> >         {
> >                 someInt = Integer.parseInt(readUserInput());
> >                 if(someInt >= 1 && someInt <= 10)
> >                         break;
> >         }
> >         catch (NumberFormatException)
> >         {
> >                 ;
> >         }
> > }
> >
> 
> This is how I started doing it initially, but if I used somInt after the catch
> statement javac said: cannot resolve symbol. The "finally" statement solved
> it.

If you declare someInt within the while block, its scope will be that block 
and you won't be able to use it outside the while loop. If you declare it
outside as above, it should work, although java will probably complain that
the variable someInt may not be initialised if you use it after the while
loop (this is because javac just sees that an exception may be thrown
during parseInt() and someInt may never be defined, although we are looping to
prevent that anyway). You can solve this by initialising it to some default
value (0) when you declare it although it should never be uninitialised in
this case. There is no real need for a finally statement in this case that
I can see. finally is just used to perform tidyup at the end of a try block
no matter what path execution takes through the try. The while loop should
read, "while(true)" and not "while()". I guess it was late when I wrote it :)

Tom.
-- 
Thomas O'Dowd. - Nooping - http://nooper.com
tom@example.com - Testing - http://nooper.co.jp/labs


Home | Main Index | Thread Index

Home Page Mailing List Linux and Japan TLUG Members Links