Page 1 of 2 12 LastLast
Results 1 to 20 of 23
  1. #1
    jkhamler is offline Member
    Join Date
    Nov 2009
    Posts
    24
    Rep Power
    0

    Default Help with Methods Please :-)

    Hi!

    I am trying to write a very simple program using methods to convert between units, like from inches to cm.

    Below I am trying to use the method 'inchtoCm' to convert whatever is input from inches to cm. The method call works, but it won't accept any input from the user... I know this is probably something to do with the arguments, but I'm stuck... help would be much appreciated!

    Thanks, Jon




    public class Q2 {

    KeyboardInput in = new KeyboardInput () ;

    public static void main (final String[] args)
    {

    Q2 anObject = new Q2 () ;

    anObject.inchtoCm(); // This calls the method 'inchtoCm'

    }

    public double inchtoCm() // Method to convert inches to cm
    {

    System.out.println("Please enter the value in inches you wish to convert to cm: ") ;
    double cm = in.readDouble()*2.54 ; // THIS WON'T WORK!
    return cm ;
    }

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,560
    Blog Entries
    7
    Rep Power
    21

    Default

    Define 'WON'T WORK'. Does it make the penguin on the television explode?

    kind regards,

    Jos

  3. #3
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default

    // THIS WON'T WORK!

    What does that mean? Compiler Error? Exception? No output?

    Yep, what Jos asked.
    Last edited by PhHein; 11-10-2009 at 04:31 PM. Reason: Being super slow.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  4. #4
    jkhamler is offline Member
    Join Date
    Nov 2009
    Posts
    24
    Rep Power
    0

    Default

    Hi Jos,

    The program just halts - it won't accept any input from me.

  5. #5
    jkhamler is offline Member
    Join Date
    Nov 2009
    Posts
    24
    Rep Power
    0

    Default

    Is it something to do with arguments?

  6. #6
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,560
    Blog Entries
    7
    Rep Power
    21

    Default

    Maybe it has something to do with the KeyboardInput type; it is not a class in the core Java distribution ...

    kind regards,

    Jos

  7. #7
    jkhamler is offline Member
    Join Date
    Nov 2009
    Posts
    24
    Rep Power
    0

    Default

    The KeyboardInput type is ok - I have a class for that. It's wierd, this kinda thing works fine outside of methods, but when I put it in a method I can't get it to read in a value...

  8. #8
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,560
    Blog Entries
    7
    Rep Power
    21

    Default

    How do you know that it doesn't work? You're not printing out anything ...

    kind regards,

    Jos

  9. #9
    jkhamler is offline Member
    Join Date
    Nov 2009
    Posts
    24
    Rep Power
    0

    Default

    It just waits like it's expecting me to type something but it won't accept an input.. This is a little confusing to explain on the forum I guess.

    It's something to do with telling it I want it to accept a value from KeyboardInput within the Method...

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

    Default

    Quote Originally Posted by jkhamler View Post
    It just waits like it's expecting me to type something but it won't accept an input.. This is a little confusing to explain on the forum I guess.

    It's something to do with telling it I want it to accept a value from KeyboardInput within the Method...
    Yes I know what you expect; after you have typed something does your program terminate immediately? Does it hang? Does it throw an Exception?

    kind regards,

    Jos

  11. #11
    jkhamler is offline Member
    Join Date
    Nov 2009
    Posts
    24
    Rep Power
    0

    Default

    It just won't let me type anything.

  12. #12
    jkhamler is offline Member
    Join Date
    Nov 2009
    Posts
    24
    Rep Power
    0

    Default

    I just want a method that accepts an input from the user, multiplies it by 2.54 and returns it.

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

    Default

    Quote Originally Posted by jkhamler View Post
    It just won't let me type anything.
    You have to be more exact: what happens if you type, say, a '0'? Do you see the echo on the screen? What happens when you press the enter key afterwards?

    kind regards,

    Jos

  14. #14
    Arnold is offline Member
    Join Date
    Oct 2009
    Location
    Rotterdam
    Posts
    52
    Rep Power
    0

    Default

    "Q2 anObject = new Q2 () ;" ?
    You don't have a constructor method. You need one if you want to use the "new" statement.
    But There are other ways beside constructors to do these things. Stick to static methods for this one.

  15. #15
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default

    Quote Originally Posted by Arnold View Post
    "Q2 anObject = new Q2 () ;" ?
    You don't have a constructor method. You need one if you want to use the "new" statement.
    But There are other ways beside constructors to do these things. Stick to static methods for this one.
    Rubbish! Read about default constructors.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  16. #16
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,560
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Arnold View Post
    "Q2 anObject = new Q2 () ;" ?
    You don't have a constructor method. You need one if you want to use the "new" statement.
    But There are other ways beside constructors to do these things. Stick to static methods for this one.
    You are obfuscating this thread: using a no-arg constructor when there are no constructors (explicitly) defined is fine, the compiler has generated one for you. Using static methods is a point of attention; in this example it doesn't matter much but non-static methods are preferable in most cases.

    kind regards,

    Jos

  17. #17
    jkhamler is offline Member
    Join Date
    Nov 2009
    Posts
    24
    Rep Power
    0

    Default

    I've got it to work by putting in a line - System.out.println("cm") ;

    - I assumed (wrongly I guess) that return prints the value?

  18. #18
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,560
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by jkhamler View Post
    I've got it to work by putting in a line - System.out.println("cm") ;

    - I assumed (wrongly I guess) that return prints the value?
    Java is not Lisp and assumptions are (almost) always incorrect. Better put a System.out.println(cm+" cm"); before your return statement in your method if you want to see what has been calculated.

    kind regards,

    Jos

  19. #19
    jkhamler is offline Member
    Join Date
    Nov 2009
    Posts
    24
    Rep Power
    0

    Default

    Exactly, that is what I have just learned - many thanks anyway!

  20. #20
    Arnold is offline Member
    Join Date
    Oct 2009
    Location
    Rotterdam
    Posts
    52
    Rep Power
    0

    Default

    Quote Originally Posted by JosAH View Post
    You are obfuscating this thread: using a no-arg constructor when there are no constructors (explicitly) defined is fine, the compiler has generated one for you. Using static methods is a point of attention; in this example it doesn't matter much but non-static methods are preferable in most cases.

    kind regards,

    Jos
    Non-static is preferable? I made an entire console program with only static methods and it works fine.

Page 1 of 2 12 LastLast

Similar Threads

  1. Replies: 8
    Last Post: 04-16-2012, 11:00 PM
  2. methods
    By lilac87 in forum New To Java
    Replies: 7
    Last Post: 07-22-2009, 06:37 PM
  3. Replies: 4
    Last Post: 04-03-2009, 06:36 AM
  4. methods
    By Zensai in forum New To Java
    Replies: 10
    Last Post: 12-03-2007, 05:31 AM
  5. Methods
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-01-2007, 08:49 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •