title: How to calculate mix fraction in my java calculator?
body: <p>I have been working on this calculator for the past 2 months, and was not able to do the mix fraction calculation. At this point, I just want to know how i can make everything in my test run correctly. That means that mix fraction would give correct calculation. How would you do it? solution?</p>
  
  <pre><code>static int DO_DEBUG = 0;  // set to 1 for messages on, or 0 for messages off
  
      static final boolean BASIC_VERSION = false;
  
  
      public static void main( String[] args ) {
  
          calculator();
          //test();
      }
  
  
      public static void test() {
          String[] tests  = { 
                  "1+1",
                  "12 * 12",
                  "1_1/2 * 1_1/2",
                  "12 + mom",
                  "12/3 / 3",
                  "1_/3 + 5",
                  "1 &amp; 4",
                  "12 +5",
                  "1/3 - 1_4/5/6"
          };
          for (int i = 0; i &lt; tests.length; i++ ) {
              System.out.println( "TESTING: " + tests[i] );
              processInput( tests[i] );
          }
  
      }
      static void calculator() {
  
          Scanner console = new Scanner ( System.in ); //turing input on
  
          System.out.println();
          String level = "Basic";
          if (! BASIC_VERSION ) {
              level = "Advanced";
          }
          System.out.println( "Welcome to the " + level + " Calculator");// have the option to pick basic or advance
  
          String input = "";
          while ( true ) {
              printPrompt();//this prompt is introduced in a different method
              input = console.nextLine();
              if ( isQuitRequest( input ) ) {//method to quit, says 'bye'
                  break;//ends
              }
              processInput( input );// doing the task
          }
          System.out.println( "bye" );
          System.out.println( );
          console.close(); // quite keyword that closes console
      }
  
      static boolean isQuitRequest( String input ) {
          return input.equalsIgnoreCase( "quit" );
      }
  
      static void processInput( String input )  {
  
          if ( isQuitRequest( input ) ) { // if quit, does not reach here
              return;
          }
  
          String error = null;
  
          String[] tokens = input.split(" ");
          if ( tokens.length &lt; 3 ) {
              error = "Not enough entires.";
          } else if (tokens.length &gt; 3 ) {
              error = "Too many entries.";
          }
          if (error != null ) {
              printError( input, error );
              return;
          }
  
          String operator = tokens[1];
          String addition  = "+";
          String subtraction = "-";
          String multiplication = "*";
          String division = "/";
          double num1 = Double.parseDouble(tokens[0]);
          double num2 = Double.parseDouble(tokens[2]);
          double result;
          String number1 = tokens[0];
          String operation = tokens[1];
          String number2 = tokens[2];
          debug( "processInput: parse result number1='" + number1 + "' "
                  + "number2='" + number2 + "' "
                  + "operation='" + operation  + "'."
              );
  
          if ( (! isNumberValid( number1 ) ) 
                  || (! isNumberValid( number2 )
                  || ( operation.length() != 1 )
                  || ("+-*/".indexOf( operation ) &lt; 0 )) ) {
              error = "Syntax error.";
          }
              // If there is an error, print it,
              //  else print the parse results.
          if (error != null ) {
              printError( input, error );
          } else {
              //System.out.println( "Numbers are " + number1 + " and "
                  //  + number2 + ".  Operation is " + operation +".");
  
              if (operator.equals(addition)){
                  result = num1 + num2;
                  System.out.println("Answer is " + result);          
              }
              else if (operator.equals(subtraction)){
                  result = num1 - num2;
                  System.out.println("Answer is " + result);          
              }
              else if (operator.equals(multiplication)){
                  result = num1 * num2;
                  System.out.println("Answer is " + result);          
              }
              else if (operator.equals(division)) {
                  result = num1 / num2;
                  System.out.println("Answer is " + result);          
              }
              else {
  
              }
  
                  }
          }
  
  
  
      //just validating -_-
       static boolean isNumberValid( String  numstr ) {
           boolean isValid = true;
           if ( BASIC_VERSION ) {
               isValid = isValidInt( numstr );
           } else {    // ADVANCED version.
               isValid =  isValidMixedFraction( numstr );;
           }
           return isValid;
       }
  
      //This is to assure that everything entered is valid
       static boolean isValidInt( String  numstr ) {
           boolean isValid = true;
           for( int i = 0; i &lt; numstr.length(); i++ ) {
              char c = numstr.charAt( i );
              if (! Character.isDigit( c )) {
                  isValid = false;
              }
           }
           return isValid;
       }
  
  
       // formating of mixed numbers
       static boolean isValidMixedFraction( String  numstr ) {
           boolean isvalid = true;
  
              // get parts this string around the _
           String[] underscoreTokens = numstr.split("_");
           String firstPart = underscoreTokens[0];
           String secondPart;
           if( underscoreTokens.length == 1 ) {
               secondPart = null;
           } else  if( underscoreTokens.length == 2 ) {
               secondPart =  underscoreTokens[1];
           } else {  // underscoreTokens.length &gt; 2 length matters
               return false;
           }
  
  
           debug( "isValidMixedFraction:  firstPart='"+ firstPart + "',  secondPart='"
                   + secondPart +"'" );        
           if (secondPart == null ) {
               // first part can be "n" or "n/n"
               if( (! isValidInt( firstPart ) ) &amp;&amp; (! isValidFraction( firstPart )) ) {
                   isvalid = false;
               }
           } else {  // 2nd part not null.
  
               if  ( ! isValidInt( firstPart ) ) {
                   isvalid = false;
               }
               if  ( ! isValidFraction( secondPart ) ) {
                   isvalid = false;
               }
           }   // end else second part not null
           return isvalid;
       }
  
      //validating the format of the fraction if it is to be valid
       static boolean isValidFraction( String  numstr ) {
           boolean isValid = true;
              // get parts this string around the _
           String[] slashTokens = numstr.split("/");
           if( slashTokens.length != 2 ) {
              return false;
           }
  
           String firstPart = slashTokens[0];
           String secondPart = slashTokens[1];
  
           if ( ! isValidInt(firstPart) ) {
               isValid = false;
           }  else if (! isValidInt(secondPart) ) {
               isValid = false;
           }
           return isValid;
       }
  
  
       static void printError( String input, String error ) {//telling you what is wrong 
          System.out.println( "ERROR: " + error + "  for input '" + input + "'." );
      }
  
      static void printPrompt() {
          System.out.print( "&gt; " );
      }
  
      static void debug( String s ) {// checking validality 
          if ( 0 &lt; DO_DEBUG) {
              System.out.println( "DBG: " + s);}
          }
  </code></pre>