Subject: Re: HW 1 question

From: Chee Yap <[email-masked]>
Date: Mon, 20 Sep 1999 20:03:42 -0400 (EDT)

Dear Doris,

I did not understand your earlier message but
in discussing with the T.A., he figured out what
you were saying!

The problem is this: in case there is an exception,
the integer n would have NO value (i.e., not initialized).

Java compiler catches this and complains... It is really
being more clever than us.

To fix this problem, you need to declare n as follows:

	int n = 0;

(or, you can put "n = 0" in the catch block).

--Chee

> > From [email-masked] Mon Sep 20 15:14 EDT 1999
> > Date: Mon, 20 Sep 1999 15:13:57 -0400 (EDT)
> > From: Doris L Eng <[email-masked]>
> > To: [email-masked]
> > Subject: HW 1 question
> > 
> > Hi Chee, 
> > 
> > I'm getting an error for my program. Here's the code and I wrote in caps
> > where the error was shown:
> > 
> >   public static void main(String[] args) { // main function 
> >       int n;
> >       if (args.length == 0) {
> > 		System.out.println("Error. No argument listed.");
> >       } else if (args.length > 1) {
> > 		System.out.println("Error. Only one argument allowed.");
> >  	}  
> > 	try {n = Integer.parseInt(args[0]);
> >       } catch(Exception e){System.out.println("Error. Argument was not an
> > integer");
> >       }
> > 
> > 
> > IN THE FOLLOWING LINE, I GET AN ERROR THAT SAYS VARIABLE N MAY NOT HAVE
> > BEEN INITIALIZED. WASN'T IT INITIALIZED WITH THE FIRST LINE IN THE MAIN
> > PART, INT N? I JUST BASICALLY DON'T UNDERSTAND WHY I'M GETTING AN ERROR
> > HERE. (In the if(n <= 0 ...) part)
> > 
> >       if (n <= 0 || n >= 13) {
> > 	System.out.println("Error. Input must be between 0 and 13.");
> >       } else {for (int i = 0; i <= n; i++) 
> >                 System.out.println 
> >                   (i + " factorial is " + computeFactorial(i)); 
> > } }// main 
> > 
> > 
> > Thanks,
> > 
> > Doris
> > 
> > ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> > 
> >          (())(())    ( Y )   
> >          (o':'o)    (>':'<)        
> >         (" )_( ")  (" )_( ")
> > 
> > We're the bunnies and we've come to take over your planet!!
> > 
> > 
> > ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> > 
> > 
>