4

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!

Youcef LAIDANI
  • 55,661
  • 15
  • 90
  • 140
Glace
  • 127
  • 1
  • 3
  • 10
  • related - https://stackoverflow.com/questions/9295621/string-how-to-replace-multiple-possible-characters-with-a-single-character – MasterJoe Mar 26 '20 at 20:00

1 Answers1

4

You dont need to use regex, you can use two replace to solve your problem :

String replace = convert.replace("1", "one ").replace("0", "zero ");

Example :

int i = 55;
System.out.println(Integer.toBinaryString(i));
System.out.println(Integer.toBinaryString(i).replace("1", "one ").replace("0", "zero "));

Output

110111
one one zero one one one 

Edit after more than one year.

As @Soheil Pourbafrani ask in comment, is that possible to traverse the string only one time, yes you can, but you need to use a loop like so :

before Java 8

int i = 55;
char[] zerosOnes = Integer.toBinaryString(i).toCharArray();
String result = "";
for (char c : zerosOnes) {
    if (c == '1') {
        result += "one ";
    } else {
        result += "zero ";
    }
}
System.out.println(result);
=>one one two one one one

Java 8+

Or more easier if you are using Java 8+ you can use :

int i = 55;
String result = Integer.toBinaryString(i).chars()
        .mapToObj(c -> (char) c == '1' ? "one" : "two")
        .collect(Collectors.joining(" "));
=>one one two one one one
Youcef LAIDANI
  • 55,661
  • 15
  • 90
  • 140
  • 1
    Thanks! Didn't know you could use multiple replace in one line. – Glace May 01 '17 at 13:28
  • yes @Glace you could do because replace return String – Youcef LAIDANI May 01 '17 at 13:29
  • Is it optimal using two replace function in a row! Because each `replace` method will traverse the `String` while we can do the replacement in just one traverse. I don't know if java will optimize such an in a row replace methods to be done in just one traverse? – Soheil Pourbafrani Jul 05 '18 at 05:30
  • @SoheilPourbafrani yes you can, check my edit I post two other solutions hope this can help you :) – Youcef LAIDANI Jul 05 '18 at 07:30