Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Calling for methods

  1. #1
    Junior Member
    Join Date
    Feb 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Calling for methods

    As some of you saw my other thread creating methods. Well now I have created methods but I am having problems calling for it on the main method.

    These where the instructions from my instructor on how to create the method.
    Write a method called convertInchesToFeet

    * one parameter: inches
    * returns nothing
    * the method converts the number of inches to feet and inches
    * the method displays the result to the user
    o For example, if the method call sends 66 as the value of inches, say "Your are 5 feet and 6 inches tall"
    * notice you are outputing the information to the user from inside the method, not returning the value to main
    The code is
    public static void convertinchestofeet(int inches)
    	{
    		int feet = inches / 12;
    		int pInches = inches % 12;
    		System.out.println("You are" + feet +"feet and" + pInches + "inches tall.");
    	}// end ConvertInchesToFeet Method

    Then the instructions for my main method are:
    says "Let's convert inches to feet" then

    * prints a blank line
    * Asks the user to enter the total number of inches tall he is
    * calls the convertInchesToFeet method
    my code is
    public static void main(String[] args)
    	{
    Scanner scanner = new Scanner(System.in);
              System.out.println("Let's convert inches to feet\n");
    		System.out.print("Enter the total number of inches tall you are:");
    			int height = scanner.next();

    Now i have having trouble linking both methods and performing the actions my teachers asks for. Can anybody help me?


  2. #2
    Junior Member
    Join Date
    Feb 2010
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Smile Re: Calling for methods

    import java.util.Scanner;
    public class SRT {
    public static void convertinchestofeet(double inches)
    	{
    		double feet = inches / 12;
    		double pInches = inches % 12;
    		System.out.println("You are " + feet +"feet and " + pInches + "inches tall.");
    }
    public static void main(String[] args)
    	{
    Scanner scanner = new Scanner(System.in);
              System.out.println("Let's convert inches to feet\n");
    		System.out.print("Enter the total number of inches tall you are:");
    			double height = scanner.nextDouble();
                SRT.convertinchestofeet(height);
     
    }
    }


    i suggest you to use double instead of int.
    there is no function named next().
    since function is static u can call it directly using <class>.<static function>
    Last edited by Json; March 1st, 2010 at 02:12 AM. Reason: Please use code tags.

Similar Threads

  1. calling c code from java
    By sara in forum Java Native Interface
    Replies: 3
    Last Post: April 6th, 2013, 09:53 PM
  2. Calling method from .class file
    By alexx_88 in forum Java Theory & Questions
    Replies: 6
    Last Post: April 24th, 2012, 02:14 AM
  3. Help Calling Method From Another Class
    By CheekySpoon in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 15th, 2010, 09:24 AM
  4. Java Code Help - Calling Method
    By KevinGreen in forum Object Oriented Programming
    Replies: 5
    Last Post: September 18th, 2009, 12:55 AM
  5. [SOLVED] How to call string in another class in java?
    By tazjaime in forum What's Wrong With My Code?
    Replies: 4
    Last Post: April 23rd, 2009, 09:31 AM