Results 1 to 6 of 6
  1. #1
    Black.Ice. is offline Member
    Join Date
    Dec 2008
    Posts
    4
    Rep Power
    0

    Default 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:

    Java 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.

  2. #2
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Posts
    564
    Rep Power
    6

    Default

    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.

  3. #3
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    I don't understand because I did initialize convertedAmount at the top
    There is a difference between declaring and inititializing.
    Java 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;
    Java 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

  4. #4
    porchrat is offline Senior Member
    Join Date
    Mar 2009
    Posts
    105
    Rep Power
    0

    Default

    IMO it is always a good idea to initialise your variables in the beginning and not just declare them (as you have done).

    instead of:
    Java Code:
    int initialAmount;
    double convertedAmount;
    use:
    Java 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:
    Java 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.

  5. #5
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

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

    Java 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

  6. #6
    porchrat is offline Senior Member
    Join Date
    Mar 2009
    Posts
    105
    Rep Power
    0

    Default

    Quote Originally Posted by pbrockway2 View Post
    > 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.

    Java 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.
    Last edited by porchrat; 06-24-2009 at 09:05 PM.

Similar Threads

  1. XML to JSP Unit Testing
    By Abder-Rahman in forum Advanced Java
    Replies: 2
    Last Post: 02-15-2009, 12:35 AM
  2. p-unit 0.12
    By Jamie in forum Java Software
    Replies: 0
    Last Post: 06-16-2007, 09:33 AM
  3. p-unit 0.11
    By levent in forum Java Software
    Replies: 0
    Last Post: 06-04-2007, 08:07 AM
  4. p-unit 0.10-release
    By levent in forum Java Software
    Replies: 0
    Last Post: 05-28-2007, 08:23 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •