2 Replies - 325 Views - Last Post: 28 September 2010 - 09:40 PM Rate Topic: -----

#1 nacholibre  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 76
  • Joined: 19-September 09

am i stupid or missing some stupid thing?

Posted 28 September 2010 - 09:19 PM

First Thank you for taking a look at this at this time of the night.

Dont mind saying you are stupid....

but here is my code and the error i get

  
new ThreadOne();
        new ThreadTwo();
        int calc;
        try{
            for(int j = 0; j<=5; j++ )
                calc = j *(500 - j);
                System.out.println("in Main Thread Multiplying :" + calc);     //its saying "variable calc might not have been initialized"
                Thread.sleep(200);
        }
        catch(InterruptedException e){
            System.out.println("Main thread interupted");
        }


why does it let me assign value to it if it is not initialized? or am i missing something when i say calc?
Help please

Is This A Good Question/Topic? 0
  • +

Replies To: am i stupid or missing some stupid thing?

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8325
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: am i stupid or missing some stupid thing?

Posted 28 September 2010 - 09:35 PM

View Postnacholibre, on 28 September 2010 - 10:19 PM, said:

First Thank you for taking a look at this at this time of the night.

Dont mind saying you are stupid....

but here is my code and the error i get

  
new ThreadOne();
        new ThreadTwo();
        int calc;
        try{
            for(int j = 0; j<=5; j++ )
                calc = j *(500 - j);
                System.out.println("in Main Thread Multiplying :" + calc);     //its saying "variable calc might not have been initialized"
                Thread.sleep(200);
        }
        catch(InterruptedException e){
            System.out.println("Main thread interupted");
        }


why does it let me assign value to it if it is not initialized? or am i missing something when i say calc?
Help please


:code:
One thing for sure... the Java compiler is not stupid :)
Indent your code correctly and it will become obvious

      try {
         for(int j = 0; j<=5; j++ )
            calc = j *(500 - j);
         System.out.println("in Main Thread Multiplying :" + calc);


OK you probably have { missing for your for() loop that will include the calculation of calc and its printout.
You can argue that the for() will be executed at least once even if an exception is throwed the compiler is not that smart (contrary to you :))
Was This Post Helpful? 1
  • +
  • -

#3 nacholibre  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 76
  • Joined: 19-September 09

Re: am i stupid or missing some stupid thing?

Posted 28 September 2010 - 09:40 PM

pardon me too much python ...
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1