Simple Unit Converter help

• 06-23-2009, 06:08 PM
Black.Ice.
Simple Unit Converter help
Hello All. Im doing a very simple unit converter for my Comp Sci class and I need a little help with a bug. This is the code that I have so far:

Code:

``` import java.util.Scanner; public class UnitConverter {           public static void main(String[] args)     {         int initialAmount;         double convertedAmount;                  System.out.println("Welcome to Ryan's Unit Converter");         System.out.println();         System.out.println("Which unit would you like to convert from:");         System.out.println("centimeter feet inch kilometer league meter microinch mile millimeter yard?");         Scanner kboard = new Scanner(System.in);         String fromUnit = kboard.nextLine();         System.out.println();         System.out.println("Which unit would you like to convert to?");         System.out.println("centimeter feet inch kilometer league meter microinch mile millimeter yard?");         String toUnit = kboard.nextLine();           System.out.println();           System.out.println("You are converting from " + fromUnit + " to " + toUnit + "?");         System.out.println("How many " + fromUnit + " would you like to convert to " + toUnit + "?");           initialAmount = kboard.nextInt();                               if(fromUnit.equals("centimeter") && toUnit.equals("feet"))               convertedAmount = initialAmount * 0.033;                                     System.out.println(convertedAmount);               }```

It's only halfway done, so Im just testing out converting centimeters to feet for now. However, if I try to run, it gives me the error "variable convertedAmount might not have been initialized
System.out.println(convertedAmount);"

I don't understand because I did initialize convertedAmount at the top. I just think that there's an error having to do with doubles and ints.

Sorry if I seem like such a java newb but Im just trying to get on my feet. Any help?

Thanks again.
• 06-23-2009, 08:40 PM
emceenugget
your initialization occurs within an conditional statement. that's why it "might" not be initalized. you can solve this by putting an 'else' to your 'if' and set the variable to another value, or by only printing if the variable is set.
• 06-23-2009, 09:28 PM
hardwired
I don't understand because I did initialize convertedAmount at the top
There is a difference between declaring and inititializing.
Code:

```public static void main(String[] args) {     // Local variable declaration, not initialized.     int initialAmount;     // Local variable dclaration and initialization.     double convertedAmount = 0.0;     ...     // Local variable initialization.     initialAmount = 25;```
Code:

```public class Initialization {     // Member variable declarations can be uninitialized.     // If this is the case java gives them a default value,     // zero for type [i]int[/i]     int memberInt;     // null for type [i]String[/i]     String memberStr;     // Member variable declared and initialized.     String anotherStr = "hello world";     private void test() {         System.out.println("memberInt = " + memberInt);         System.out.println("memberStr = " + memberStr);         System.out.println("anotherStr = " + anotherStr);         // Local variables are not assigned default values         // and must be initialized, ie, assigned a value         // before they are used.         // So this uninitialized local variable         // is okay so far.         int localInt;         // As soon as I try to do something with [i]localInt[/i]         // there will be a compile–time error.         //System.out.println("localInt = " + localInt);     }     public static void main(String[] args) {         new Initialization().test();     } }```
Initializing Fields
• 06-23-2009, 09:30 PM
porchrat
IMO it is always a good idea to initialise your variables in the beginning and not just declare them (as you have done).

Code:

```int initialAmount; double convertedAmount;```
use:
Code:

```int initialAmount = 0; double convertedAmount = 0.0;```
You can't always count on default values.

That will solve the error, but that means that whenever this condition is not met:
Code:

`if(fromUnit.equals("centimeter") && toUnit.equals("feet"))`
Your program will then print out the 0.0 value, which is obviously not desirable. To prevent that you would need to complete the class and ensure that you have an 'else' statement at the end.
• 06-24-2009, 05:46 AM
pbrockway2
> IMO it is always a good idea to initialise your variables in the beginning

Compiler messages are good things and should be embraced.

Assigning values to variables "just in case" is only going to frustrate the compiler in its analysis of your code, and stop you getting helpful messages.

Code:

```Foo foo = null; /* null just in case... */ if(someCondition) {     foo = createFoo(); } else {     // whoops! forgot to initialise foo in this case     // This is a logic bug... } foo.doStuff(); // ...but the compiler won't pick it up```
• 06-24-2009, 09:57 PM
porchrat
Quote:

Originally Posted by pbrockway2
> IMO it is always a good idea to initialise your variables in the beginning

Compiler messages are good things and should be embraced.

Assigning values to variables "just in case" is only going to frustrate the compiler in its analysis of your code, and stop you getting helpful messages.

Code:

```Foo foo = null; /* null just in case... */ if(someCondition) {     foo = createFoo(); } else {     // whoops! forgot to initialise foo in this case     // This is a logic bug... } foo.doStuff(); // ...but the compiler won't pick it up```

Good point :P.