Question:
Calling Methods in Java?
Ana
2010-02-28 20:05:29 UTC
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
[code]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
[/code]

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 [code]
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();
[/code]

Now i have having trouble linking both methods and performing the actions my teachers asks for. Can anybody help me?
Three answers:
?
2016-04-15 02:55:31 UTC
What you are wanting to do is to write a parser program in Java, which reads individual words from a text file, parses them (essentially compares the word against words in a dictionary, either from a separate file or held in the program), picks up any parameters and calls the appropriate methods. It's an interesting exercise, and it will certainly keep you occupied for a while. If you do an internet search for "parser program java" you will find lots of ideas to start you off.
Dr.Hack007
2010-02-28 21:32:39 UTC
first when you get the user input you should get next int not next as in



int height = scanner.nextInt();

scanner.nextLine();

/**this eats the end of line input so the next time you try to get something it doesn't just give a space or what ever is on the end of the line */



when you call a method you just say "methodName(input)"



so in this case:



convertinchestofeet(height);

/** since it returns nothing just call it */



ps. as a side note, when writing in java all variables and methods are case sensitive so where ever there are capitals you must have the capitals in the same place when calling the method or variable

...also standard java naming convention is to capitalize the first letter of every word in the name, except the first word



Hope this helps
AniDev
2010-02-28 20:18:59 UTC
non-static method Scanner.next() will return a String; you want an int, so do:

Scanner.nextInt();



int height = scanner.nextInt();

convertInchesToFeet(height);


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...