开发者

Android - Convert Arch to Decimal

I'm creating a feet and inches calculator. I want the user to be able to enter the information in various ways such as 1'-4-5/8" or 1 4 5/8.

When performing math, the above numbers will have to be converted to decimal (1'-4-5/8" is 16.625 in decimal inches). The final result will be in either decimal inches or then converted back to feet and inches.

How would I go about parsing the architectural measurement and converting it into decimal inches?

Any help would be greatly appreciated!

Thank you!

edit: After way too much time and trying many different things, I think I got something that will work. I'm ultimately going to limit the way the user can enter the length so I think the following is going to work. It may not be optimized but it's the best I can get right now.

public class delim_test_cases {

    public static void main(String[] args) {

        double inches = 0;
        double feet = 0;
        double fract = 0;
        String str = "开发者_如何学C2'-3-7/8";
        String[] TempStr;
        String delimiter = ("[-]+");
        TempStr = str.split(delimiter);

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

            for(int z=0; z< TempStr[i].length() ; z++ ) {

                if (TempStr[i].charAt(z) == '\'') {
                    String[] FeetStr;
                    String feetdelim = ("[\']+");
                    FeetStr = TempStr[i].split(feetdelim);
                    feet = Integer.parseInt(FeetStr[0]);
                }

                else if (TempStr[i].charAt(z) == '/') { 
                    String[] FracStr;
                    String fracdelim = ("[/]+");
                    FracStr = TempStr[i].split(fracdelim);
                    double numer = Integer.parseInt(FracStr[0]);
                    double denom = Integer.parseInt(FracStr[1]);
                    fract = numer/denom;
                }

                else  if (TempStr[i].indexOf("\'")==-1 && TempStr[i].indexOf("/")==-1) {
                    String inchStr;
                    inchStr = TempStr[i];
                    inches = Integer.parseInt(inchStr);
                }

            }
        }

        double answer = ((feet*12)+inches+fract);
        System.out.println(feet);
        System.out.println(inches);
        System.out.println(fract);
        System.out.println(answer);
    }
}


Why not just split on either the '-' or a space, then you have an array of possibly 3 strings.

So, you look at the last character, if it is a single quote, then multiply that by 12.

If a double quote, then split on '/' and just calculate the fraction, and for a number that is neither of these it is an inch, just add it to the total.

This way you don't assume the order or what if you just have fractional inches.

Just loop through the array and then go through the algorithm above, so if someone did

3'-4'-4/2" then you can calculate it easily.

UPDATE:

I am adding code based on the comment from the OP.

You may want to refer to this to get more ideas about split.

http://www.java-examples.com/java-string-split-example

But, basically, just do something like this (not tested, just written out):

public double convertArchToDecimal(String instr, String delimiter) {
  String[] s = instr.split(delimiter);
  int ret = 0;
  for(int t = 0; t < s.length; t++) {
    char c = s[t].charAt(s[t] - length);
    switch(c) {
       case '\'':  //escape single quote
         String b = s[t].substring(0, s[t].length - 1).split("/");
         try {
           ret += Integer.parse(s[t].trim()) * 12;
         } catch(Exception e) { // Should just need to catch if the parse throws an error
         }
         break;
       case '"':  // may need to escape the double quote
         String b = s[t].substring(0, s[t].length - 1).split("/");
         int f = 0;
         int g = 0;
         try {
            f = Integer.parse(b[0]);
            g = Integer.parse(b[1]);
            ret += f/g;
         } catch(Exception e) {
         }
         break;
       default:
         try {
           ret += Integer.parse(s[t].trim());
         } catch(Exception e) { // Should just need to catch if the parse throws an error
         }
         break;
     }
  }
  return ret;
}

I am catching more exceptions than is needed, and you may want to use the trim() method more than I did, to strip whitespace before parsing, to protect against 3 / 5 " for example.

By passing in a space or dash it should suffice for your needs.

You may also want to round to some significant figures otherwise rounding may cause problems for you later.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜