CRACKED
All you need are reusable methods. e.g a method to print 1
digit number, 2
digit number and so on... then you call printOne
in printTwo
after printing the first as shown...
import java.util.Scanner;
public class x {
// Instance fields are below....
// Placed below for easy readability of code...
public static void main (String args[]) {
// You know what this does...
System.out.print (" Input number\n > ");
Scanner sc = new Scanner (System.in);
// Create int array to collect all digits...
String number = Integer.toString (sc.nextInt ());
int[] digits = new int[number.length ()];
for (int i = 0; i < number.length (); i++)
digits [i] = Integer.parseInt (number.charAt (i) + "");
// All you need is a simple method to print a given amount..even up to ten million
switch (digits.length) {
case 1:
printOne (digits);
break;
case 2:
printTwo (digits);
break;
case 3:
printThree (digits);
break;
case 4:
printFour (digits);
break;
case 5:
printFive (digits);
break;
case 6:
printSix (digits);
break;
}
}
static void printOne (int[] arr) {
// Just print coresponding value, easy since i converted to int[]
System.out.print (below_ten [arr [0]]);
}
static void printTwo (int[] arr) {
// check if it is a ~teen/not (you know what i mean)
if (arr [0] == 1)
System.out.print (below_twenty [arr [1] + 1]);
else {
System.out.print (below_hundered [arr [0]] + " ");
if (arr [1] != 0) // added this to prevent 30 = thirty zero
printOne (new int[] {arr [1]}); // this wont be needed for the ~teens
}
}
static void printThree (int[] arr) {
// now everything is siple, just print first digits and call previous, to call previous...
// you get where that goes...
System.out.print (below_thousand [arr [0]] + " ");
printTwo (new int[] {arr [1], arr [2]});
}
// Now you have the algorithm, Challenge=print up to ten million... :-)
static void printFour (int[] arr) {
// challenge
}
static void printFive (int[] arr) {
//exercise
}
static void printSix (int[] arr) {
//bounty :-b)
}
// Placed below for easy readability of code...
static String hundred = " hundred";
static String[] below_ten = new String[] {
"zero","one","two","three","four","five", "six","seven","eight","nine"
};
static String[] below_twenty = new String[] {
"","ten","eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen", "nineteen"
};
static String[] below_hundered = new String[] {
"","","twenty","thirty","forty","fifty","sixty", "seventy","eighty","ninety"
};
static String[] below_thousand = new String[] {
"", below_ten [1] + hundred, below_ten [2] + hundred, below_ten [3] + hundred, below_ten [4] + hundred, below_ten [5] + hundred, below_ten [6] + hundred, below_ten [7] + hundred, below_ten [8] + hundred, below_ten [9] + hundred,
};
}
_Good luck...
edit
In case you have trouble with the other methods....works more than charm....
static void printFour (int[] arr) {
System.out.print (below_million [arr [0]]);
printThree (new int[] {arr [1], arr [2], arr [3]});
}
static void printFive (int[] arr) {
printTwo (new int[] {arr [0], arr [1]});
System.out.print (thousand);
printThree (new int[] {arr [2], arr [3], arr [4]});
}
static void printSix (int[] arr) {
printThree (new int[] {arr [0], arr [1], arr [2]});
System.out.print (thousand);
printThree (new int[] {arr [3], arr [4], arr [5]});
}
static String thousand = " thousand ";
static String[] below_million = new String[] {
"", below_ten [1] + thousand, below_ten [2] + thousand, below_ten [3] + thousand, below_ten [4] + thousand, below_ten [5] + thousand, below_ten [6] + thousand, below_ten [7] + thousand, below_ten [8] + thousand, below_ten [9] + thousand,
};