Page 2 of 2 FirstFirst 12
Results 21 to 34 of 34
  1. #21
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

  2. #22
    zhen1337 is offline Member
    Join Date
    Feb 2011
    Posts
    16
    Rep Power
    0

    Default

    Quote Originally Posted by Junky View Post
    What does the compiler say?
    Eclipse have no error on those lines but it wont let me compile because now it says my double entree "The local variable entree may not have been initialized"

    i think i didn't link the converted entree1 to entree right, or i converted it wrong. which why i asked.
    Last edited by zhen1337; 02-08-2011 at 03:28 AM.

  3. #23
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    So initialize it then. Give it a starting value, perhaps 0.0. A key to learning Java is to try to understand what the error message is telling you, and this one isn't cryptic at all -- it's saying that you're using a variable that might not yet have been assigned a value (because you assign it in a try block and the try block might fail.

  4. #24
    zhen1337 is offline Member
    Join Date
    Feb 2011
    Posts
    16
    Rep Power
    0

    Default

    i tired that then the whole thing just skips the conversion and user input for entree.
    let says i make double entree = 0;
    it will just do mealcost = (0 + sides) * tax * tip;
    so i believe i did the conversion wrong or linked them wrong or both. not sure what to do at this point
    Last edited by zhen1337; 02-08-2011 at 03:43 AM.

  5. #25
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    The first thing to do would be to understand what you are doing. It seems you are making the same mistake as a lot of n00bs: guessing.

  6. #26
    zhen1337 is offline Member
    Join Date
    Feb 2011
    Posts
    16
    Rep Power
    0

    Default

    I understand that you guys are trying to help us by making us realize our own mistake and where the problem lies but when we don't understands it and because we are noobs at this point, trying to do it for the 1st time. so i m googling trying to understand, but it's still confusing. i still do not know what is wrong with this code.
    Last edited by zhen1337; 02-08-2011 at 04:00 AM.

  7. #27
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    Java Code:
    int number = "hello";
    Surely you understand that this is wrong. Well, you are doing something similar in your code.

    Stop coding and grab a piece of paper. Write down exactly what you want to do on the paper. Each and every tiny step. Once you have done that it will become more obvious how to write the code.

  8. #28
    zhen1337 is offline Member
    Join Date
    Feb 2011
    Posts
    16
    Rep Power
    0

    Default

    In the step where i ask the user to enter entree1 in string.
    reads the string, converts it in to double, match it with a meal(chicken beef,fish) double. set it to = entree. then use it in the equation.

    right now i m unsure of if i converted it into double wrong
    or match it with a meal double wrong,
    or even setting it to = entree wrong.

  9. #29
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    So break it down step by step.

    Get the user to input a value. Print that input back to the screen.
    Move on...convert that to a double and print to a screen. As a sanity check, print 2 * user input. If user inputs 5 then 10 should be displayed.
    Move on....etc

  10. #30
    zhen1337 is offline Member
    Join Date
    Feb 2011
    Posts
    16
    Rep Power
    0

    Default

    i'm converting it wrong? because "The local variable entree may not have been initialized"
    Last edited by zhen1337; 02-08-2011 at 05:48 AM.

  11. #31
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    Jeebus!

    As was pointed out earlier you must give local variables an intial value.
    Java Code:
    int value; // wrong, no initial value
    int value = 0; // correct, has initial value

  12. #32
    zhen1337 is offline Member
    Join Date
    Feb 2011
    Posts
    16
    Rep Power
    0

    Default

    now it runs fine, but it keeps entree with initial value. So the if statements r not working. but what is wrong with it?
    Last edited by zhen1337; 02-08-2011 at 05:48 AM.

  13. #33
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    If you look carefully and it would be more obvoius if you used consistent indentation, you have the if statement inside the catch statement. It will only get executed if an exception occurs.

    Also, the more standard way of converting a String to double is to use the parse method, the same as when you parse an int.

  14. #34
    zhen1337 is offline Member
    Join Date
    Feb 2011
    Posts
    16
    Rep Power
    0

    Default

    i was giving up but i couldn't sleep. so i sit there and figured it out.
    1st i did not need to convert entree to double.
    I could just use an if statement with entreeStr.equals("chicken, beef, fish") == true ) {(set entree= to a value)}

    I truly appreciate your help.
    Thank you.
    Last edited by zhen1337; 02-08-2011 at 09:37 AM.

Page 2 of 2 FirstFirst 12

Similar Threads

  1. Convert from string to double
    By Lord ice in forum New To Java
    Replies: 4
    Last Post: 12-12-2010, 05:27 PM
  2. String to double errors
    By bigvanilla in forum New To Java
    Replies: 9
    Last Post: 12-05-2010, 08:42 AM
  3. convert String to Double
    By azurovyhrosik in forum CLDC and MIDP
    Replies: 5
    Last Post: 10-22-2008, 02:46 AM
  4. Converting String to Double
    By srini in forum New To Java
    Replies: 1
    Last Post: 12-24-2007, 08:03 PM
  5. convert string to a double?
    By javaMike in forum Advanced Java
    Replies: 2
    Last Post: 11-27-2007, 03:10 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
  •