Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 06-23-2009, 06:08 PM
Member
 
Join Date: Dec 2008
Posts: 4
Rep Power: 0
Black.Ice. is on a distinguished road
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:

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.
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 06-23-2009, 08:40 PM
Senior Member
 
Join Date: Sep 2008
Posts: 564
Rep Power: 2
emceenugget is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 06-23-2009, 09:28 PM
hardwired's Avatar
Senior Member
 
Join Date: Jul 2007
Posts: 1,561
Rep Power: 4
hardwired is on a distinguished road
Default
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 int
    int memberInt;
    // null for type String
    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 localInt
        // there will be a compile–time error.
        //System.out.println("localInt = " + localInt);
    }

    public static void main(String[] args) {
        new Initialization().test();
    }
}
Initializing Fields
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 06-23-2009, 09:30 PM
Member
 
Join Date: Mar 2009
Posts: 40
Rep Power: 0
porchrat is on a distinguished road
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:
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.
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 06-24-2009, 05:46 AM
Senior Member
 
Join Date: Feb 2009
Posts: 589
Rep Power: 1
pbrockway2 is on a distinguished road
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.

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
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 06-24-2009, 09:57 PM
Member
 
Join Date: Mar 2009
Posts: 40
Rep Power: 0
porchrat is on a distinguished road
Default
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.

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 10:05 PM.
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
XML to JSP Unit Testing Abder-Rahman Advanced Java 2 02-15-2009 01:35 AM
costs of unit testing JavaForums Java Blogs 0 10-17-2008 02:50 PM
p-unit 0.12 Jamie Java Announcements 0 06-16-2007 10:33 AM
p-unit 0.11 levent Java Announcements 0 06-04-2007 09:07 AM
p-unit 0.10-release levent Java Announcements 0 05-28-2007 09:23 AM


All times are GMT +2. The time now is 04:36 PM.



VBulletin, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org