Question #64353

Problem 1: Number to Words (Loop with If, If … Else)
Create Number to Words on the left side of the app. It should ask the user to enter an integer that is between zero and one million inclusive. The user can then click a button that will convert the number into words and displays these words. For example, if the user enters 12345 the app will print “twelve thousand three hundred forty five”. Note that there is no “and” in the words and no commas in the input. Make sure that the app recognizes when the user enters an invalid input and presents a well-written error message to alert the user of the error. Please make sure that the error also helps the user enter the correct values. Only use if else and else if statements.

Expert's answer

package NumbersToWords;

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

import static java.lang.Character.isDigit;

/**

* Converts an integer that is between zero and one million inclusive into words using IF...ELSE.

*/

public class NumbersToWords {

private static final String[] nums = {"", " one", " two", " three", " four", " five", " six", " seven", " eight",

" nine", " ten", " eleven", " twelve", " thirteen", " fourteen", " fifteen",

" sixteen", " seventeen", " eighteen", " nineteen"};

private static final String[] tens = {"", " ten", " twenty", " thirty", " forty", " fifty",

" sixty", " seventy", " eighty", " ninety"};

private static final int SIZE = 6;

public static String convertNumbersToWords(String sNumber) {

int iNumber = Integer.parseInt(sNumber);

if (iNumber == 0)

return "zero";

if (iNumber == 1000000)

return "one million";

String result="";

// Number to array of ints

int[] n = new int[SIZE];

for (int i = SIZE-1; i >= 0; i--){

n[i]=iNumber % 10;

iNumber/=10;

}

if (n[0] != 0)

result = nums[n[0]] + " hundred";

if (n[1] != 0)

if (n[1] < 2)

result += nums[n[2]+10] + " thousand";

else

result += tens[n[1]] + nums[n[2]] + " thousand";

else

if (n[2] != 0)

result += nums[n[2]] + " thousand";

else

if (n[0] != 0)

result = result + " thousand";

if (n[3] != 0)

result += nums[n[3]] + " hundred";

if (n[4] != 0)

if (n[4] < 2)

result += nums[n[5]+10];

else

result += tens[n[4]] + nums[n[5]];

else

if (n[5] != 0)

result += nums[n[5]];

return result.replaceFirst(" ","");

}

public static boolean isPositiveNumber(String s) {

if (s.length() == 0) return false;

char[] chars = s.toCharArray();

for (int i = 0; i < chars.length; i++)

{

char c = chars[i];

if ((i != 0 && c == '-') || (!isDigit(c) ))

return false;

}

return true;

}

public static void main (String... args) throws IOException {

BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));

System.out.println("Please enter a number from 0 to 1,000,000 inclusively or press q to quit");

while(true) {

String sNumber = reader.readLine();

if (sNumber.equals("q"))

break;

if (!isPositiveNumber(sNumber)) {

System.out.println("Probably you've entered non-numeric characters or a negative number...");

System.out.println("Please enter a number from 0 to 1,000,000 inclusively or press q to quit");

continue;

}

if (Integer.parseInt(sNumber) > 1000000) {

System.out.println("The number you've entered is greater than 1,000,000...\n" +

"Please enter a number from 0 to 1,000,000 inclusively or press q to quit");

continue;

}

System.out.println(convertNumbersToWords(sNumber));

}

}

}

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

import static java.lang.Character.isDigit;

/**

* Converts an integer that is between zero and one million inclusive into words using IF...ELSE.

*/

public class NumbersToWords {

private static final String[] nums = {"", " one", " two", " three", " four", " five", " six", " seven", " eight",

" nine", " ten", " eleven", " twelve", " thirteen", " fourteen", " fifteen",

" sixteen", " seventeen", " eighteen", " nineteen"};

private static final String[] tens = {"", " ten", " twenty", " thirty", " forty", " fifty",

" sixty", " seventy", " eighty", " ninety"};

private static final int SIZE = 6;

public static String convertNumbersToWords(String sNumber) {

int iNumber = Integer.parseInt(sNumber);

if (iNumber == 0)

return "zero";

if (iNumber == 1000000)

return "one million";

String result="";

// Number to array of ints

int[] n = new int[SIZE];

for (int i = SIZE-1; i >= 0; i--){

n[i]=iNumber % 10;

iNumber/=10;

}

if (n[0] != 0)

result = nums[n[0]] + " hundred";

if (n[1] != 0)

if (n[1] < 2)

result += nums[n[2]+10] + " thousand";

else

result += tens[n[1]] + nums[n[2]] + " thousand";

else

if (n[2] != 0)

result += nums[n[2]] + " thousand";

else

if (n[0] != 0)

result = result + " thousand";

if (n[3] != 0)

result += nums[n[3]] + " hundred";

if (n[4] != 0)

if (n[4] < 2)

result += nums[n[5]+10];

else

result += tens[n[4]] + nums[n[5]];

else

if (n[5] != 0)

result += nums[n[5]];

return result.replaceFirst(" ","");

}

public static boolean isPositiveNumber(String s) {

if (s.length() == 0) return false;

char[] chars = s.toCharArray();

for (int i = 0; i < chars.length; i++)

{

char c = chars[i];

if ((i != 0 && c == '-') || (!isDigit(c) ))

return false;

}

return true;

}

public static void main (String... args) throws IOException {

BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));

System.out.println("Please enter a number from 0 to 1,000,000 inclusively or press q to quit");

while(true) {

String sNumber = reader.readLine();

if (sNumber.equals("q"))

break;

if (!isPositiveNumber(sNumber)) {

System.out.println("Probably you've entered non-numeric characters or a negative number...");

System.out.println("Please enter a number from 0 to 1,000,000 inclusively or press q to quit");

continue;

}

if (Integer.parseInt(sNumber) > 1000000) {

System.out.println("The number you've entered is greater than 1,000,000...\n" +

"Please enter a number from 0 to 1,000,000 inclusively or press q to quit");

continue;

}

System.out.println(convertNumbersToWords(sNumber));

}

}

}

## Comments

## Leave a comment