import java.util.Scanner;
/**
* An object of class Digit represents one digit in a three digit number
*/
public class Digit {
/**
* Main method which gets a valid number from the standard stream out outputs a series of lines pertaining to the value of each place digit in said number
*
* @param args the command line arguments for this program, they are not used
*/
private static final int NUMDIGITS = 3;
private static final String[] digits = {"ones","tens","hundreds","thousands","ten thousands","hundred thousands","millions"};
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String number = "";
Digit[] digits = new Digit[NUMDIGITS];
while(true) {
System.out.print("Enter a "+NUMDIGITS+" digit positive integer: ");
number = scan.nextLine();
if(getNumber(number)) {
break;
}
else {
System.out.println();
System.out.println("You didn't enter a "+NUMDIGITS+" digit number! try again");
}
}
for(int i=0; i<NUMDIGITS; i++) {
digits[i] = new Digit(i, Integer.parseInt((new Character(number.charAt(i))).toString()));
}
for(Digit digit : digits) {
System.out.println(digit.toString());
}
}
/**
* Static method to parse a string entered by the user and determine if it is a valid number and three digits long
*
* @param number the string representing the input by the user
* @param writeNum the Integer in which this method should write the output integer
* @return true if it is a three digit number, otherwise false
*/
private static boolean getNumber(String number) {
boolean returnval = false;
try {
Integer.parseInt(number);
if(number.length()!=NUMDIGITS) {
throw new NumberFormatException("Number is not "+NUMDIGITS+" digits!");
}
returnval = true;
}
catch(NumberFormatException e) {
}
return returnval;
}
private int position;
private int value;
private String digitValue;
/**
* Constuctor for objects of type Digit
*
* @param position the position of this digit in the three digit string it is a child of
* @param value the single digit value of this digit
*/
public Digit(int position, int value) {
this.position = position;
this.value = value;
this.digitValue = "The "+digits[NUMDIGITS-position-1]+" digit is: ";
}
/**
* Overridden toString() method to convert this digit to a textual representation of what it represents
*
* @return the string representation
*/
public String toString() {
return this.digitValue+new Integer(this.value).toString();
}
}