Android Open Source - aSimpleDiceRoller String Utils






From Project

Back to project page aSimpleDiceRoller.

License

The source code is released under:

GNU General Public License

If you think the Android project aSimpleDiceRoller listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/*
 *  aSimpleDiceRoller - Simple dice roller application for Android.
 *  Copyright  2012 Elad Alfassa <elad@fedoraproject.org>
 * /*  w w w .  j ava  2  s  .  com*/
 *  This file is part of aSimpleDiceRoller.
 *
 *   aSimpleDiceRoller is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   aSimpleDiceRoller is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with aSimpleDiceRoller.  If not, see <http://www.gnu.org/licenses/>.

 */

package com.elad.diceroller;
import java.util.ArrayList;
/*
 * This class was written by Buglord from Nerdfighteria IRC.
 * However, he did not want any copyright on it.
 */
public class StringUtils {
  /**
  * finds the first pair of ( ) which doesn't contain any more ( ) pairs
  */
  public static String findSub(String string){
    String sub = null;
    if(string.contains("(")&&string.contains(")")){
        int last = 0;
        char[] chars = string.toCharArray();
        for(int pos = 0;pos<chars.length;pos++){
          if(chars[pos]=='('){
            last = pos;
          }else if(chars[pos]==')'){
            sub = string.substring(last+1, pos); 
            break;
          }
        }
    }else{
      return string;
    }
    return sub;
  }
  /**
  * separates the string into number and not-number segments.
  */
  public static String[] separate(String string){
    ArrayList<String> strings = new ArrayList<String>();
    char[] chars = string.toCharArray();
    boolean workingOnNumber;
    if(Character.isDigit(chars[0])){
      workingOnNumber=true;
    }else{
      workingOnNumber=false;
    }
    int b = 0;
    for(int a = 0;a<chars.length;a++){
      if (workingOnNumber) {
        if(!Character.isDigit(chars[a])){
          strings.add(String.copyValueOf(chars, b, a-b));
          b=a;
          workingOnNumber=false;
        }
      }else{
        if(Character.isDigit(chars[a])){
          strings.add(String.copyValueOf(chars, b, a-b));
          b=a;
          workingOnNumber=true;
        }
      }
    }
    strings.add(String.copyValueOf(chars, b, chars.length-b));
    Object[] objs = strings.toArray();
    String[] strs = new String[objs.length];
    for(int a = 0;a<objs.length;a++){
      strs[a]=(String)objs[a];
    }
    return strs;
  }
  private static boolean isNumber(String str){
    if(Character.isDigit(str.toCharArray()[0])){
      return true;
    }else{
      return false;
    }
  }
  /**
  * does the multiplication as noted by the '*' sign.
  */
  public static String[] doMultiplcation(String[] strings){
    ArrayList<String> strs = new ArrayList<String>();
    boolean isNumber;
    boolean doneCalc = false;
    for(int a = 0;a<strings.length;a++){
      if(a>0){
        isNumber = isNumber(strings[a]);
        if(strings[a].equals("*")){
          if(doneCalc){
            double res = Double.parseDouble(strs.get(strs.size()-1))*Double.parseDouble(strings[a+1]);
            strs.set(strs.size()-1,String.valueOf(res));
          }else{
            double res = Double.parseDouble(strings[a-1])*Double.parseDouble(strings[a+1]);
            strs.add(String.valueOf(res));
          }
          doneCalc=true;
        }else if(isNumber&&doneCalc){
        }else if(doneCalc){
          doneCalc=false;
        }else{
          strs.add(strings[a-1]);
        }
      }
    }
    if (!doneCalc)
      strs.add(strings[strings.length-1]);
    String[] strsA = new String[strs.size()];
    Object[] objs = strs.toArray();
    for(int a = 0;a<strsA.length;a++){
      strsA[a]=(String)objs[a];
    }
    return strsA;
  }
  /**
  * does the division as noted by the '/' sign.
  */
  public static String[] doDivide(String[] strings){
    ArrayList<String> strs = new ArrayList<String>();
    boolean isNumber;
    boolean doneCalc = false;
    for(int a = 0;a<strings.length;a++){
      if(a>0){
        isNumber = isNumber(strings[a]);
        if(strings[a].equals("/")){
          if(doneCalc){
            double res = Double.parseDouble(strs.get(strs.size()-1))/Double.parseDouble(strings[a+1]);
            strs.set(strs.size()-1,String.valueOf(res));
          }else{
            double res = Double.parseDouble(strings[a-1])/Double.parseDouble(strings[a+1]);
            strs.add(String.valueOf(res));
          }
          doneCalc=true;
        }else if(isNumber&&doneCalc){
        }else if(doneCalc){
          doneCalc=false;
        }else{
          strs.add(strings[a-1]);
        }
      }
    }
    if (!doneCalc)
      strs.add(strings[strings.length-1]);
    String[] strsA = new String[strs.size()];
    Object[] objs = strs.toArray();
    for(int a = 0;a<strsA.length;a++){
      strsA[a]=(String)objs[a];
    }
    return strsA;
  }
  /**
  * does the addition as noted by the '+' sign.
  */
  public static String[] doAddition(String[] strings){
    ArrayList<String> strs = new ArrayList<String>();
    boolean isNumber;
    boolean doneCalc = false;
    for(int a = 0;a<strings.length;a++){
      if(a>0){
        isNumber = isNumber(strings[a]);
        if(strings[a].equals("+")){
          if(doneCalc){
            double res = Double.parseDouble(strs.get(strs.size()-1))+Double.parseDouble(strings[a+1]);
            strs.set(strs.size()-1,String.valueOf(res));
          }else{
            double res = Double.parseDouble(strings[a-1])+Double.parseDouble(strings[a+1]);
            strs.add(String.valueOf(res));
          }
          doneCalc=true;
        }else if(isNumber&&doneCalc){
        }else if(doneCalc){
          doneCalc=false;
        }else{
          strs.add(strings[a-1]);
        }
      }
    }
    if (!doneCalc)
      strs.add(strings[strings.length-1]);
    String[] strsA = new String[strs.size()];
    Object[] objs = strs.toArray();
    for(int a = 0;a<strsA.length;a++){
      strsA[a]=(String)objs[a];
    }
      return strsA;
  }
  /**
  * does the subtraction as noted by the '-' sign.
  */
  public static String[] doSubtraction(String[] strings){
    ArrayList<String> strs = new ArrayList<String>();
    boolean isNumber;
    boolean doneCalc = false;
      for(int a = 0;a<strings.length;a++){
        if(a>0){
          isNumber = isNumber(strings[a]);
          if(strings[a].equals("-")){
            if(doneCalc){
              double res = Double.parseDouble(strs.get(strs.size()-1))-Double.parseDouble(strings[a+1]);
              strs.set(strs.size()-1,String.valueOf(res));
            }else{
              double res = Double.parseDouble(strings[a-1])-Double.parseDouble(strings[a+1]);
              strs.add(String.valueOf(res));
            }
            doneCalc=true;
          }else if(isNumber&&doneCalc){
          }else if(doneCalc){
            doneCalc=false;
          }else{
            strs.add(strings[a-1]);
          }
        }
      }
      if (!doneCalc)
        strs.add(strings[strings.length-1]);
      String[] strsA = new String[strs.size()];
      Object[] objs = strs.toArray();
      for(int a = 0;a<strsA.length;a++){
        strsA[a]=(String)objs[a];
      }
      return strsA;
    }
  public static String[] doMultAndDiv(String[] strings){
        ArrayList<String> strs = new ArrayList<String>();
        boolean isNumber;
        boolean doneCalc = false;
        for(int a = 0;a<strings.length;a++){
                if(a>0){
                        isNumber = isNumber(strings[a]);
                        if(strings[a].equals("/")){
                                if(doneCalc){
                                        double res = Double.parseDouble(strs.get(strs.size()-1))/Double.parseDouble(strings[a+1]);
                                        strs.set(strs.size()-1,String.valueOf(res));
                                }else{
                                        double res = Double.parseDouble(strings[a-1])/Double.parseDouble(strings[a+1]);
                                        strs.add(String.valueOf(res));
                                }
                                doneCalc=true;
                        }else if(strings[a].equals("*")){
                                if(doneCalc){
                                        double res = Double.parseDouble(strs.get(strs.size()-1))*Double.parseDouble(strings[a+1]);
                                        strs.set(strs.size()-1,String.valueOf(res));
                                }else{
                                        double res = Double.parseDouble(strings[a-1])*Double.parseDouble(strings[a+1]);
                                        strs.add(String.valueOf(res));
                                }
                                doneCalc=true;
                        }else if(isNumber&&doneCalc){
                        }else if(doneCalc){
                                doneCalc=false;
                        }else{
                                strs.add(strings[a-1]);
                        }
                }
        }
        if(!doneCalc){
          strs.add(strings[strings.length-1]);
        }
        String[] strsA = new String[strs.size()];
        Object[] objs = strs.toArray();
        for(int a = 0;a<strsA.length;a++){
                strsA[a]=(String)objs[a];
        }
        return strsA;
    }
  public static String[] doAddAndSub(String[] strings){
        ArrayList<String> strs = new ArrayList<String>();
        boolean isNumber;
        boolean doneCalc = false;
        for(int a = 0;a<strings.length;a++){
                if(a>0){
                        isNumber = isNumber(strings[a]);
                        if(strings[a].equals("-")){
                                if(doneCalc){
                                        double res = Double.parseDouble(strs.get(strs.size()-1))-Double.parseDouble(strings[a+1]);
                                        strs.set(strs.size()-1,String.valueOf(res));
                                }else{
                                        double res = Double.parseDouble(strings[a-1])-Double.parseDouble(strings[a+1]);
                                        strs.add(String.valueOf(res));
                                }
                                doneCalc=true;
                        }else if(strings[a].equals("+")){
                                if(doneCalc){
                                        double res = Double.parseDouble(strs.get(strs.size()-1))+Double.parseDouble(strings[a+1]);
                                        strs.set(strs.size()-1,String.valueOf(res));
                                }else{
                                        double res = Double.parseDouble(strings[a-1])+Double.parseDouble(strings[a+1]);
                                        strs.add(String.valueOf(res));
                                }
                                doneCalc=true;
                        }else if(isNumber&&doneCalc){
                        }else if(doneCalc){
                                doneCalc=false;
                        }else{
                                strs.add(strings[a-1]);
                        }
                }
        }
        if(!doneCalc){
          strs.add(strings[strings.length-1]);
        }
        String[] strsA = new String[strs.size()];
        Object[] objs = strs.toArray();
        for(int a = 0;a<strsA.length;a++){
                strsA[a]=(String)objs[a];
        }
        return strsA;
    }



}




Java Source Code List

com.elad.diceroller.ASimpleDiceRollerActivity.java
com.elad.diceroller.DiceListActivity.java
com.elad.diceroller.DiceRollerShared.java
com.elad.diceroller.NerdmodeActivity.java
com.elad.diceroller.StringUtils.java