I have an assignment where I must convert an int into a string of digits like so,
123: one two three
193: one nine three
I can not use the String.valueof() method and then convert to a char array! What other way is there of doing this? Modulus?
I have an assignment where I must convert an int into a string of digits like so,
123: one two three
193: one nine three
I can not use the String.valueof() method and then convert to a char array! What other way is there of doing this? Modulus?
step 1: convert your int into a string (new Integer(123).toString())
step 2: loop on your string (on each character)
step 3: in the loop, have a switch on the value, from 0 to 9 which prints the actual word representing the digit
So that will look like:
String intStringValue = new Integer(123).toString();
for( char c : intStringValue.toCharArray() ) {
int digit = Integer.parseInt(new String(new char[] {c}));
switch(digit){
case 0: System.out.print("zero "); break;
case 1:....
}
}
public class HelloWorld{
public static void main(String []args)
{
String values[]={"zero","one","two","three","four","five","six","seven","eight","nine"};
int number=453;
String num=Integer.toString(number);
for(int i=0;i<num.length();i++)
{
char index=num.charAt(i);
System.out.print(values[Character.getNumericValue(index)]);
} }}
You can try this solution. It works.
Well I won't give you any code, but maybe some paragraph-form pseudocode and an idea to get you to think outside of the box, since you'll not always have a pre-made method ready for you. As a worst case scenario if you can't find an alternate method, think about how to break it down.
What number system is the input formatted in? Is it binary, hexadecimal, decimal? Find out what the possible range of values is for any given number in that number system, and create a block or two of code that will determine which number in that range the number is.
In binary we know that a number can either be a 1 or a 0, so for each possible digit in any length of successive digits we know that the only two possible String representations of these numbers is "One" and "Zero".
You shouldn't need to use a single method in analyzing the numbers, nor should you need to use a method to concatenate onto the String you are creating.
If you try to process the number as the integer it truly represents, you could have any number of possibilities as large as an int datatype can hold (2 to the power of 32 = 4294967296 possible combinations of numbers - with that many possibilities to check, you would LITERALLY be better off writing a program to write that block of your program!!), but if you process the int digit by digit, you can have - at WORST - 10 possible combinations(0-9 inclusive) multiplied by the number of digits, up to a maximum of 10 digits in an int(in decimal form). Moreover, in the former case, you would have to check for every single number in one giant absurd iteration. In the latter case, you only need to check 10 values, and iterate 1-10 times inclusive depending on the amount of digits in the number.