Page 1 of 2 12 LastLast
Results 1 to 20 of 34
  1. #1
    zhen1337 is offline Member
    Join Date
    Feb 2011
    Posts
    16
    Rep Power
    0

    Default String to double HELP please!

    Hello,
    Thank you for taking time to read this and help me out.

    deleted
    Last edited by zhen1337; 02-08-2011 at 05:49 AM. Reason: Moderator Edit: Code tags added

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

    Default

    For help, you need to ask a coherent question, and it usually ends with a question mark, "?". So what specifically is your question?

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

    Default

    Obviously we MUST fix the code in red.

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

    Default

    I'm new to java, and my java class is asking me to do this assignment. I spend days googling and trying my best to figure this out but i failed. so i would like to ask for help with the code in red.

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

    Default

    Quote Originally Posted by zhen1337 View Post
    I'm new to java, and my java class is asking me to do this assignment. I spend days googling and trying my best to figure this out but i failed. so i would like to ask for help with the code in red.
    But what help do you need? What is not working? What is your question? We're not going to write it for you, but we can help clarify any concepts you're stuck on. If you can't tell us what is working, what's not working, what your question is, then we can't help you. Period. Again, please put some effort into forming a coherent question.

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

    Default

    Quote Originally Posted by Junky View Post
    Obviously we MUST fix the code in red.
    Obviously. I'm trying to get some flippin' explanation from him but it's like pulling teeth.

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

    Default

    My reply was sarcastically aimed at the OP not you ;)

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

    Default

    ok, How can I put the String "entree" in to double so i can use it in my equation.
    "mealcost = (entree + sides) * tax * tip;"
    I tired converting the string to double but it is not working. I would like to know what i m doing wrong.
    Thanks

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

    Default

    Quote Originally Posted by zhen1337 View Post
    ok, How can I put the String "entree" in to double so i can use it in my equation.
    "mealcost = (entree + sides) * tax * tip;"
    I tired converting the string to double but it is not working. I would like to know what i m doing wrong.
    Thanks
    OK, now we're getting somewhere. Now what do you mean when you say "it is not working"? Does it not compile? If not, what error occurs? Does it compile and run but throw an exception? If so, what exception is thrown? Does it tell you you look fat and need a haircut? What happens?

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

    Default

    Quote Originally Posted by Fubarable View Post
    you you look fat and need a haircut
    Are you spying on me?

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

    Default

    Eclipse is telling me catch and nfe are Syntax error.
    } catch (NumberFormatException nfe) {

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

    Default

    When you get a compiler error most intelligent people would actually copy and paste teh EXACT error message. Catch statements are associated withe a try statement. Where in your code is the try statement?

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

    Default

    OK now you're giving us information we can work with, you're missing the key word "try". Look up try/catch blocks in the Java tutorials: try/catch blocks

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

    Default

    I was messing with the codes when i had the try statement, the conversion did not effect



    entree became "Incompatible operand types String and double" again.

    now i added the try statement back, the conversion is not effecting entree.
    Last edited by zhen1337; 02-08-2011 at 05:50 AM.

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

    Default

    entree is a String variable and can only reference a String. The compiler is telling you that you can't assign a double to entree, and of course, the compiler is right. Don't do this. If you need to hold on to double information, put it in a double variable. You do that with the d variable, but since it was declared inside of your try block, it is only visible inside of the block. So declare it outside of the block in the method or the class.

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

    Default

    then how can i put the entree in the equation?
    "mealcost = (entree + sides) * tax * tip;"

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

    Default

    This is the code i have right now, it can compile but it's giving an error.
    Last edited by zhen1337; 02-08-2011 at 05:50 AM.

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

    Default

    Quote Originally Posted by zhen1337 View Post
    then how can i put the entree in the equation?
    "mealcost = (entree + sides) * tax * tip;"
    As written, you can't. It is a String and you can't calculate with Strings. Again, convert it to a double like you're doing, and use that double in the equation, not a String variable.

    Perhaps the best solution is for entree to not be a String at all, but rather a double variable, that you should read the user's input into a local variable, not entree, convert the String to a double and then put that into your double variable entree. Then you can use entree in your equation above.

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

    Default

    hmm i think i might got it, hold on...
    Last edited by zhen1337; 02-08-2011 at 02:51 AM.

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

    Default

    first i would like to ask, m i converting it right?
    Last edited by zhen1337; 02-08-2011 at 05:50 AM.

Page 1 of 2 12 LastLast

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
  •