2

I am writing java code for translating signals. If a given string (INPUT) is:

C*12.387a0d14assc7*18.65d142a

Its translation (OUTPUT) should be:

C*12387:1000a0d14assc7*1865:100d142a 

ALGORITHM:

Wherever in the string a star(*) follows a number containing decimal (in this string there are two, first is '*12.387' and the second is *18.65'), this number is to be changed into fraction as in the above example 12.387 is converted into 12387:1000 and 18.65 converted into 1865:100

If decimal number is isolated I can convert it into fraction with the following code:

    double d = 12.387;
    String str = Double.toString(d);        
    String[] fraction = str.split("\\.");

    int denominator = (int)Math.pow(10, fraction[1].length());
    int numerator = Integer.parseInt(fraction[0] + "" + fraction[1]);

    System.out.println(numerator + ":" + denominator);

But I do not how to separate the substring of decimal number from the string it contains. Being new to java and programming I need help. Thanks in anticipation.

Jit
  • 131
  • 4

2 Answers2

6

Using regex and capturing groups is a good way to implement your parsing:

String s = "C*12.387a0d14assc7*18.65d142a";
StringBuffer result = new StringBuffer();
Matcher m = Pattern.compile("\\*(\\d+)\\.(\\d+)").matcher(s);
while (m.find()) {
    String num = m.group(1);
    String denom = m.group(2);
    String divisor = "1" + new String(new char[denom.length()]).replace("\0", "0");
    String replacement = "*" + num + denom + ":" + divisor;
    m.appendReplacement(result, replacement);
}
m.appendTail(result);
System.out.println(result.toString());
beny23
  • 34,390
  • 5
  • 82
  • 85
1

I was writing a solution which uses regex'es but then tried without them. The solution here is general (for any programming language). Sure it would be interesting to see if it is faster than regex based solution. Anyway I suspect that regex based solution might be faster. Please see this solution too (it is not perfect though :) ).

import java.util.*;    

class DoubleConvert 
{
    public static void main (String[] args)
    {
        StringBuilder buffer = new StringBuilder("C*12.387a0d14assc7*18.65d142a");
        int j, m, k; 
        int i = 0;
        while (i < buffer.length())
        {
            if (buffer.charAt(i) == '*')
            {
                m = -1; k = -1;
                j = i; //remember where * found
                while ( i + 1 < buffer.length() )
                {
                    i++;
                    if (Character.isDigit(buffer.charAt(i)))
                    {
                        continue;
                    }
                    else if (buffer.charAt(i) == '.')
                    {
                        m = i; // remember where . found
                        while (i + 1 < buffer.length())
                        {
                            i++;
                            if (Character.isDigit(buffer.charAt(i)))
                            {
                                continue;
                            }
                            else
                            {
                                k = i; //remember the last position
                                break;
                            }
                        }
                    }
                    else //let's see what we got
                    {
                        if (m > 0 && j > 0 && m - j > 0 && k - m > 0) //there must exist strings
                        {
                            System.out.println("Found " + buffer.substring(j, m) 
                            + " second part " + buffer.substring(m, k));
                            buffer.replace(j+1, k, 
                                    buffer.substring(j+1, m) + 
                                    buffer.substring(m+1, k) +
                                    ":1" +
                                    new String(new char[k-1-m]).replace("\0","0"));
                        }
                        break;
                    }
                }
            }
            else 
            {
                i++;
            }
        }
        System.out.println("Result " + buffer);
    }
}

The output

Found *12 second part .387
Found *18 second part .65
Result C*12387:1000a0d14assc7*1865:100d142a
  • Your this code is quite competitive. It is quite educative which is missing in all great books, it has solved my many other similar problems. It has given me insight and innovativeness. – Jit Jul 22 '15 at 06:18