LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-27-2003, 09:54 AM   #1
Huddlebum
Member
 
Registered: Nov 2003
Distribution: RedHat 9.0
Posts: 45

Rep: Reputation: 15
Odd Java Error; Need Help Debugging (Nevermind)


Code:
/*
1 meter is 39.37 inches.
This program outputs a table of twelve feet's worth of inch-to-meter
conversions, and outputs a blank line every 12 inches.
*/
class InchtoMeter {
        public static void main(String[] args) {
                int inches, counter;
                for (inches = 0; inches <= 144; inches++) {
                        counter++;
                        if (counter == 12) {
                                System.out.println(inches/12+" feet is " +
                                        inches/39.37D + " meters.");
                                System.out.println();
                                counter = 0;
                        }
                        else {
                                System.out.println(inches+" inches is " +
                                        inches/39.37D + " meters.");
                        }
                }
        }
}
and this is the error I get from Sun JDK:
Quote:
InchtoMeter.java:10: variable counter might not have been initialized
counter++;
^
1 error
"Might not have been initialized"??? WTF?

I solved the problem by realizing the difference between "declared" and initialized, and assigned counter the value of zero outside the scope of for. I also initialized inches to 1 instead of to 0, because it was saying every 11 inches was a foot!

Last edited by Huddlebum; 11-27-2003 at 10:18 AM.
 
Old 11-27-2003, 02:34 PM   #2
coolman0stress
Member
 
Registered: Jun 2003
Location: Toronto, Ontario, Canada
Posts: 288

Rep: Reputation: 30
That's right, Java is very picky about that kind of stuff. It's actually sort of good, since it could be a hard to find bug.
 
Old 11-27-2003, 06:38 PM   #3
eric.r.turner
Member
 
Registered: Aug 2003
Location: Planet Earth
Distribution: Linux Mint
Posts: 216

Rep: Reputation: 31
Class fields are automatically initialized (0, null, false, etc.) Variables within a method are NOT initialized, so the compiler alerts you about this. Arrays are the only exception. Array elements are initialized (0, null, false etc...) no matter where they are declared.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Odd error at the end of my C++ program myrrdyn Programming 5 04-07-2005 01:28 PM
odd kdevelop compile error shishimo Linux - Software 0 01-27-2005 01:44 PM
odd apt-get error in debian Lleb_KCir Linux - Software 2 10-08-2004 10:40 PM
Odd error installing ffmpeg af_dave Linux - Software 1 07-12-2004 02:01 AM
Odd error during boot up, never seen this before [42]Sanf0rd Linux - General 2 06-26-2004 05:42 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:36 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration