Results 1 to 13 of 13
  1. #1
    Adomini is offline Member
    Join Date
    Aug 2010
    Posts
    70
    Rep Power
    0

    Default return question for method

    Ok, I am a little confused here as to what I can do to return an int called num.

    I have to return it to make the program work to the professors specifications, but it is confusing. here is the header:

    Java Code:
    public static int solve(double [] eqn, double [] roots)
    I cant return this "int num" because all equations are in a double.

    I keep getting the error "Cant change result from int to double/int[]"

    How do you guys switch from a double to an int? I assume you use variables, right?

  2. #2
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    760
    Rep Power
    8

    Default

    Hi,

    You can convert from double value to int value using the type cast. If you look at the Double class you can also find a method that return an int value from the Double instance. But you should be aware that converting from double to int value you will lost the fractional value of your original double value.
    Last edited by wsaryada; 09-02-2010 at 06:17 AM.

  3. #3
    Adomini is offline Member
    Join Date
    Aug 2010
    Posts
    70
    Rep Power
    0

    Default

    I dont mean to be facetious, but is there another way?

    My code uses two double arrays, but I need to return an int.

    If i use cast, most likely the professor will not approve of it, since he wants me to "program it all out". I am essentially not able to use Java utilities like comparing arrays and such - I have to do the manual work only. Does that make any sense? thanks for your response though. I wish I could use it.

  4. #4
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    I don't think your professor can disprove of it, honestly. If your numbers are all doubles, somewhere you're going to lose fractional data if you're required to return an int. Whether that's through .intValue() or through (int) casting, you need some way to return it as an int. I mean, in theory, you could put the double into a string and capture everything to the left of the decimal... then convert that to an int using parseInt, but that's just stupid to be honest.

    PS: You can also use Math.floor and Math.ceil, but those still return doubles.

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default

    My guess is that the method is supposed to return the number of roots and the roots need to be placed in the array passed to the method. The number of roots is an integer of course.

    kind regards,

    Jos

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default

    Yes, asking us how to turn the inputs into an output without telling us what the method is supposed to actually do is unlikely to result in a correct answer.

    So, can you confirm Jos' suspicions about what this method is supposed to actually do?

  7. #7
    Adomini is offline Member
    Join Date
    Aug 2010
    Posts
    70
    Rep Power
    0

    Default

    yeah, Jos said it.

    I apologize again for my lack for explanation. I was trying to ask for some direction without placing homework on the forum. I do not want anyone to think I am cheating vs. asking for help online. next time though, I will just post some of what it is asking for.

    Should I use a variable to do what Jos said?

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default

    Should I use a variable to do what Jos said
    A variable is much easier to use than hardcoded literals.

  9. #9
    Adomini is offline Member
    Join Date
    Aug 2010
    Posts
    70
    Rep Power
    0

    Default

    I had some extra help and got it solved. Thanks for telling me about the cast - I did not know that. Java has some pretty cool packages.

  10. #10
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Adomini View Post
    I had some extra help and got it solved. Thanks for telling me about the cast - I did not know that. Java has some pretty cool packages.
    What cast?

    kind regards,

    Jos

  11. #11
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default

    Post #2.
    Which is slightly worrying since we now know casting has nothing to do with this.

  12. #12
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Tolls View Post
    Post #2.
    Which is slightly worrying since we now know casting has nothing to do with this.
    The OP's last reply also contradicts the OP's reply #7. I don't understand this entire thread anymore ;-)

    kind regards,

    Jos

  13. #13
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default

    Imagine how complex it would be if it wasn't a SOLVED thread?

Similar Threads

  1. Not able to return the method value
    By dmakshay2002 in forum Advanced Java
    Replies: 11
    Last Post: 05-28-2010, 03:07 PM
  2. Method won't return value
    By footyvino in forum New To Java
    Replies: 2
    Last Post: 03-26-2010, 11:49 AM
  3. Replies: 3
    Last Post: 10-15-2009, 02:02 PM
  4. Return question in a method.
    By MetalGear in forum New To Java
    Replies: 1
    Last Post: 01-13-2008, 05:45 AM
  5. Return value of method
    By cachi in forum New To Java
    Replies: 1
    Last Post: 08-01-2007, 09: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
  •