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 / First Aid / December 2004

Tip: Looking for answers? Try searching our database.

scope of a variable

Thread view: 
Kenneth - 06 Dec 2004 05:53 GMT
I want to know why the variable 'i' is not recognized within the 'try'
function?  The code is below:

for(int i =1; i<stock.length;i++)
   {

       System.out.print("Input the price: ");
       System.out.flush();
       try {
            if ((line = stndin.readLine()) != null)
              stock[1].price = Double.valueOf(line).intValue();
        } catch (IOException e) {
        }
       System.out.print("Input the quantity: ");
       System.out.flush();
       try {
            if ((line = stndin.readLine()) != null)
              stock[i].quantity = Integer.valueOf(line).intValue();
        } catch (IOException e) {
        }
   
   
       System.out.println("buy " + stock[i].quantity + " at $" +
stock[i].price + " each.");
    }
Brett Foster - 06 Dec 2004 06:55 GMT
> I want to know why the variable 'i' is not recognized within the 'try'
> function?  The code is below:

It's hard enough to debug code... It's next to impossible when you don't
even declare all your variables in the fragment.

**snip**

Think again:

for(int i =1; i<100;i++){
    try {i=i;} catch (Exception ex) {};
}

You need to ISOLATE the problem. That is, take out as much code as
possible and begin to add it back in slowly and then see where you're
having trouble. I think you'll find it's not i.

Brett
Ryan Stewart - 06 Dec 2004 12:21 GMT
>> I want to know why the variable 'i' is not recognized within the 'try'
>> function?  The code is below:
>
> It's hard enough to debug code... It's next to impossible when you don't
> even declare all your variables in the fragment.

It's also much easier when you follow code standards:
http://java.sun.com/docs/codeconv/html/CodeConvTOC.doc.html

It makes your code much easier to read.
Kenneth - 06 Dec 2004 23:11 GMT
> I want to know why the variable 'i' is not recognized within the 'try'
> function?  The code is below:
[quoted text clipped - 21 lines]
> stock[i].price + " each.");
>     }

brett, i don't understand what you're trying to say.   i want to replace
all 'stock[1]' with 'stock[i]'].   i need to use the 'i' that i declared
in the 'for' loop.

how do i reference that 'i'?
Eric Sosman - 06 Dec 2004 23:24 GMT
> brett, i don't understand what you're trying to say.   i want to replace
> all 'stock[1]' with 'stock[i]'].   i need to use the 'i' that i declared
> in the 'for' loop.
>
> how do i reference that 'i'?

   Exactly as you're doing in the "stock[i].quantity="
line and in the final println() call.  Just change the
"stock[1].price=" to "stock[i].price=" and you're set.

   If that doesn't cure your problems, you'll have to
explain what problems you're having.  So far, you have
not done so, nor shown enough code for anyone to guess
what they might be.  Please see

    http://www.physci.org/codes/sscce.jsp

Signature

Eric.Sosman@sun.com

Kenneth - 07 Dec 2004 00:15 GMT
>>brett, i don't understand what you're trying to say.   i want to replace
>>all 'stock[1]' with 'stock[i]'].   i need to use the 'i' that i declared
[quoted text clipped - 12 lines]
>
>     http://www.physci.org/codes/sscce.jsp

the compiler error i recieve is:   'variable 'i' not initialized'

here is the code again;

for(int i =1; i<stock.length;i++)
   {
        System.out.print("Input the price: ");
       System.out.flush();
       try {
            if ((line = stndin.readLine()) != null)
              stock[i].price = Double.valueOf(line).intValue();
        } catch (IOException e) {
        }
       System.out.print("Input the quantity: ");
       System.out.flush();
       try {
            if ((line = stndin.readLine()) != null)
              stock[i].quantity = Integer.valueOf(line).intValue();
        } catch (IOException e) {
        }
   
   
       System.out.println("buy " + stock[i].quantity + " at $" +
stock[i].price + " each.");
    }

is it because the 'i' variable is in the try() statement?  if so how
would i reference the declared 'i' in the for loop in the try()
statement?

since i delcared 'i' to be 1 in the for() statement, when 'i' goes
through the try() statements, 'i' should be 1, right?
Andrew Thompson - 07 Dec 2004 00:29 GMT
>>     http://www.physci.org/codes/sscce.jsp
..
> the compiler error i recieve is:   'variable 'i' not initialized'
...
> here is the code again;
>
> for(int i =1; i<stock.length;i++)

A quiz for you Kenneth.  What do the second S and first C
os SSCCE mean?  Why are they important?

Signature

Andrew Thompson
http://www.PhySci.org/codes/  Web & IT Help
http://www.PhySci.org/  Open-source software suite
http://www.1point1C.org/  Science & Technology
http://www.LensEscapes.com/  Images that escape the mundane

Kenneth - 07 Dec 2004 20:31 GMT
>>> brett, i don't understand what you're trying to say.   i want to
>>> replace all 'stock[1]' with 'stock[i]'].   i need to use the 'i' that
[quoted text clipped - 44 lines]
> since i delcared 'i' to be 1 in the for() statement, when 'i' goes
> through the try() statements, 'i' should be 1, right?

for some reason, the error message is gone.   now it goes through the
loop once and the second time around it just stops at
"System.out.print("Input the quantity: ");"

any ideas what might be going on?


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



©2008 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.