Re: Missing return statement
remember that all class names should be capitalized!
change the class name to Marc1
for the last method, the method signature clearly indicates that you intend for the method to return a String. yet the method only prints the numbers and does not return anything. there are 2 solutions, the first is just to change the return type to void. the second solution is to return
number + ""; which automatically turns it into a string. you can then print this in another method.
Re: Missing return statement
Quote:
Originally Posted by
tomejuan
remember that all class names should be capitalized!
change the class name to Marc1
for the last method, the method signature clearly indicates that you intend for the method to return a String. yet the method only prints the numbers and does not return anything. there are 2 solutions, the first is just to change the return type to void. the second solution is to return
number + ""; which automatically turns it into a string. you can then print this in another method.
Class names DON'T have to be capitalized. Its just a good coding convention that you do. I was corrected on this fact earlier this week. Also, tomejuan is correct in saying that the reason you have a "missing return error" is due to the fact that your final method is setup to return a String type but you are only printing to the screen. So to correct this, change "String" to "void" in the method's header.
Re: Missing return statement
Thanks for helping me get the return statement taken care of. All errors went away. Now when I went to run the code in my terminal. No matter what I enter, it continuously loops my system.out.print.
Re: Missing return statement
I modified your main code a bit to reflect what I think you wanted to do. Firstly you seem to exit the program if the user enters a zero but there is no notice to the user about this. Secondly I'm presuming since you have the main program in a while loop, you want the program to run so long as zero is not entered. Finally, your validateLength(), I am taking it that you are looking for input of no more than one (ie the number entered is to be a single digit) based on the "You're number is too long" message. So I changed the conditional bracket in the method to check if the length was "Greater Than" 1 and not "Less Than" as you have it.
Code java:
System.out.println("What is your single digit integer?");
System.out.print("Type 0 to exit: "); //Tell the user how to exit program
int number= input.nextInt();
while (number > 0) { //While the number entered is not 0
if (validateLength(number)){ //Validate number length
convertIntegerToWords(number); //if true, convert to word
}
System.out.print("Enter another single digit integer?: "); //request another integer
number = input.nextInt(); //store the new number and return to start of loop
}
System.out.print( "Thank you for playing! Good bye!"); //User has decided to exit so print goodbye message before program ends
}
Code java:
if (userNumber.length() > 1) { //change made here!
System.out.print("The number you entered is too long!");
return false;
}
else
return true;
}
Hope this helps a bit. If I have mistaken your code's functionality just make the necessary amends to facilitate larger numbers.