Skip to content

Instantly share code, notes, and snippets.

@bkawk
Created February 10, 2019 05:58
Show Gist options
  • Save bkawk/60ba23f199250a89c995318eb8a561fd to your computer and use it in GitHub Desktop.
Save bkawk/60ba23f199250a89c995318eb8a561fd to your computer and use it in GitHub Desktop.
public class numberToString {
public static void main(String[] args) {
int input = 20 ;
String undred = "undred";
String[] endNumbers = {"", "one", "two", "three","four", "five", "six", "seven", "eight", "nine", "ten"};
String[] weirdTeens = {"", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"};
String[] overTwenty = {"", "", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"};
if (input <= 10) System.out.println(input + " " + endNumbers[input]);
if (input > 10 && input < 20) System.out.println(input + " " + weirdTeens[input - 10]);
if (input > 15) {
int[]digits = Integer.toString(input).chars().map(c -> c-'0').toArray();
if (digits.length < 3) { System.out.print(input + " " + overTwenty[digits[0]] + endNumbers[digits[1]]);} else System.out.print(input + " " + undred);
}
}
}
@AlfredoTigolo
Copy link

version of java jdk 1.7.0_09 does not compile

/*

C:\Documents and Settings\Administrator\My Documents\JavaReview>javac HelloWorld
Console.java
HelloWorldConsole.java:13: error: illegal start of expression
int[]digits = Integer.toString(input).chars().map(c -> c-'0').toArray();
^
1 error

C:\Documents and Settings\Administrator\My Documents\JavaReview>javac -version
javac 1.7.0_09

C:\Documents and Settings\Administrator\My Documents\JavaReview>
*/

@bkawk
Copy link
Author

bkawk commented Feb 10, 2019

public class numberToString {
public static void main(String[] args) {
int input = 20 ;
String undred = "undred";
String[] endNumbers = {"", "one", "two", "three","four", "five", "six", "seven", "eight", "nine", "ten"};
String[] weirdTeens = {"", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"};
String[] overTwenty = {"", "", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"};
if (input <= 10) System.out.println(input + " " + endNumbers[input]);
if (input > 10 && input < 20) System.out.println(input + " " + weirdTeens[input - 10]);
if (input > 15) {
String test = Integer.toString(input);
String StringArray[] = test.split("\B");
int IntegreArray[] = new int[StringArray.length];
for (int i = 0; i < StringArray.length; i++) {
IntegreArray[i] = Integer.parseInt(StringArray[i]);
if (IntegreArray.length < 3) { System.out.print(input + " " + overTwenty[IntegreArray[0]] + endNumbers[IntegreArray[1]]);} else System.out.print(input + " " + undred);
}
}
}
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment