Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

calling functions

 
Daniel Hall
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just started programming and needed a little help. I am writing a program that reads in a number from a user then converts that number into a string(ex. 1 to one). I pass in the integer x now how do i return a string into a string variable called strx.
public static String wordNumber(int x)
if(x == 1)
return "one";

 
Shivaji Marathe
Ranch Hand
Posts: 203
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You will nned to create a top-level class, declare this method in that class, and in the main method of that class, call this method.


HTH
Originally posted by Daniel Hall:
I just started programming and needed a little help. I am writing a program that reads in a number from a user then converts that number into a string(ex. 1 to one). I pass in the integer x now how do i return a string into a string variable called strx.
public static String wordNumber(int x)
if(x == 1)
return "one";

 
Cindy Glass
"The Hood"
Sheriff
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It would be better to declare a local variable to be the string to return.
String returnString = "Some default verbage";
Then if you set up a Switch, in each case statement you can assign the correct word to the variable. At the end of the Switch you just return returnString. Having only one return is much easier for the next guy to follow, and prevents errors on your part.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic