Numbers to Words
Collapse Content
Show Content
Given a number from 1 to 4 (inclusive), return a word representation of the number. For example, given 2
, return two
. If the number is greater than 4, return the phrase too large
. If the number is less than 1, return the phrase too small
.
Input/Output Format
Boilerplate code is provided for Java. For other languages, read in the numbers from the standard input in the following format.
- The input starts with N, the number of cases.
- N lines follow, which each contains 1 integer.
Print out the correct phrase for each case on its own line.
Click on "View Raw I/O" below to view sample input and output.
Challenge
'Convert' each Number to a Word, as described above.
Please sign in or sign up to submit answers.
Alternatively, you can try out Learneroo before signing up.
Comments
Victoria Holland
Jan 22, 7:29 AMAn interesting thing I discovered in this task is that you can't use return statements within a switch-block otherwise the compiler will say the break statements are unreachable. I had to assign
the word representations of the number to a String variable and then return that at the end of the method.
Learneroo
Jan 22, 9:28 AM@Victoria, thanks. If you use a return statement, you should remove the
break
after it, since return exits the block of code. To help avoid errors, the Java compiler doesn't accept code it recognizes as unreachable (even if just abreak
statement).Dino Šišić
Feb 21, 6:14 AMYou can make an String array and a new String after which you can do the following if for example num == 1 then assign the value of array[0] which is "one" in which case the new String equals "one". For too large and too small assign the "too large" or "too small" string from array if num < 1 and if num > 4 assign those strings from array to new String. Close all if and else if statements and return the new String.
Hope it helps
alan
Mar 29, 10:38 PMIs there an easier way to do this? For the numbers 1-4 and other conditions, i made an individual if statement to save the string value to a variable. It seems kind of inefficient to write 6 if statements.
Learneroo
Mar 30, 11:48 AM@alan, you can use a switch case instead. Click on "Featured Answers" above the Hint.
Bill
Apr 22, 1:03 AMNeed help
import java.util.Scanner;
public class Main {
}
when i run this code, the compiler shows one error.
Main.java:21: error: missing return statement
}
^
1 error
how should do to solve this problem?
Budafai Gergely
Jul 20, 5:27 AMMy solution is:
String mes = "";
String[] onetofour = {"one", "two", "three", "four"};
for (int i =1; i<onetofour.length+1; i++) {
if (num== i) {
Macro
Aug 18, 10:52 PMflowra
Feb 13, 5:55 PMMy solution and it is correct :
String result="";
String [] test = {"one","two","three","four"};
if (num 4) result= "too large";
Bernard Mitchell
May 14, 4:39 PMThis continuation of "if" and "else" problems doesn't explain how to convert numbers to text. Can you please explain?
Learneroo
May 14, 4:44 PMYou need to return a String.
Bernard Mitchell
May 15, 1:12 PMCan I get an explanation on how to return a string? Like I said this wasn't covered in previous lessons.
Learneroo
May 15, 5:38 PMInstead of
return 5;
, you doreturn "hello";
See String Class for more info.Bernard Mitchell
May 20, 12:01 PMThank You
Bernard Mitchell
May 20, 1:47 PMI keep getting errors in the code I didn't write and I haven't touched the boiler plate code.
import java.util.Scanner;
public class Main {
}
This isn't the complete answer but what I've done so far
Bernard Mitchell
May 20, 3:49 PMNever mind got it although I don't know if the way I did it is the most efficient. Thanks love this site!!!!
thales
Jul 15, 8:35 AMwhat is wrong with this code:
thales
Jul 15, 8:53 AMthis code works in eclips?
thales
Jul 15, 9:06 AMI think i have a very good queston ( at least i hope so).
Why isnt declaring enough en should i initialize the string. so
String message=""; works ( initializing)
String message; does not work, why not? for a string it does not mather right. i get it if it was an int but now??
Mari Garchagudashvili
Sep 9, 2:59 AMWhat is wrong with this code?
L
Nov 22, 3:51 AMMy solution
Islam Elzohary
Dec 22, 4:55 AMmy code
https://www.learneroo.com/modules/20/nodes/147#&togetherjs=FcxFnlbpqR
Hanlise
May 14, 10:32 AMdef function1(a):
x=range (1, 5)
output=""
thisdict={
1:"One",
2:"Two",
3:"Three",
4:"Four"
}
Using python
Roger
Aug 13, 7:51 PMim not too comfortable with loops just yet but these are my 2 solutions on js:
/*function convert(a) {
let num = a
if (num < 1) {
return 'too small'
} else if (num == 1) {
return 'one'
} else if ( num == 2) {
return 'two'
} else if (num == 3) {
return 'three'
} else if (num == 4) {
return 'four'
} else if (num > 4) {
return 'too large'
} else {
return 'input a number'
}
};*/
const arry = ['too small', 'one', 'two', 'three', 'four', 'too high'];
function convert(a) {
let num = arry[a];
if(a < 1) {
return arry[0]
} else if (a > 4) {
return arry[5]
} else {
return num
}
}
Roger
Aug 15, 10:02 AMconst word = {
1: 'one',
2: 'two',
3: 'three',
};
function convert(i) {
let num = word[i]
if ( i < 1 || i > 3) {
return 'out of range'
} else {
return num
}
}