Results 1 to 6 of 6
Thread: Simple Unit Converter help
- 06-23-2009, 05:08 PM #1
Member
- Join Date
- Dec 2008
- Posts
- 4
- Rep Power
- 0
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.
- 06-23-2009, 07:40 PM #2
Senior Member
- Join Date
- Sep 2008
- Posts
- 564
- Rep Power
- 5
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, 08:28 PM #3
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;Initializing FieldsJava 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(); } }
- 06-23-2009, 08:30 PM #4
Senior Member
- Join Date
- Mar 2009
- Posts
- 105
- Rep Power
- 0
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:
use:Java Code:int initialAmount; double convertedAmount;
You can't always count on default values.Java Code:int initialAmount = 0; double convertedAmount = 0.0;
That will solve the error, but that means that whenever this condition is not met:
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.Java Code:if(fromUnit.equals("centimeter") && toUnit.equals("feet"))
- 06-24-2009, 04:46 AM #5
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
> 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
- 06-24-2009, 08:57 PM #6
Senior Member
- Join Date
- Mar 2009
- Posts
- 105
- Rep Power
- 0
Similar Threads
-
XML to JSP Unit Testing
By Abder-Rahman in forum Advanced JavaReplies: 2Last Post: 02-15-2009, 12:35 AM -
p-unit 0.12
By Jamie in forum Java SoftwareReplies: 0Last Post: 06-16-2007, 09:33 AM -
p-unit 0.11
By levent in forum Java SoftwareReplies: 0Last Post: 06-04-2007, 08:07 AM -
p-unit 0.10-release
By levent in forum Java SoftwareReplies: 0Last Post: 05-28-2007, 08:23 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks