I am working on a code that will convert binary digits to its corresponding value in words.
For example, I would input "3" and the code will convert the number to "11", which is the binary representation of "3". The code will proceed to convert that "11" to "one one" which will be outputted.
I have already wrote the binary conversion part, but I am having difficulty converting it to words.
public class BinaryWords {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
String S = sc.nextLine(); //how many times the for loop will repeat
for (int i = 0; i < S.length() + 1; i++) {
int A = sc.nextInt(); //input the number
String convert = Integer.toBinaryString(A); //converts the number to binary String
String replace = convert.replaceAll("[1 0]", "one, zero "); //replaces the String to its value in words
System.out.println(replace);
}
}
}
I tried using the replaceAll function with the regex [1, 0], which (I think) will convert (both?) 1 and 0 to the sequence specified in the next field.
I would like to convert every 1 to a "one" and every 0 to a "zero".
Any help is appreciated, thanks!