
/*

   	Tom DeDonno

	Program demonstrates initial methods used to read in string...
	$30.00+$45+10% = 75 + 7.5 = 82.5

	raises exception whenever bad data is read
	new exceptions are not created only existing exception class
 
	Demonstrates use of StringTokenizer, and substring indexOf method
*/

import java.util.*; //For StringTokenizer
public class DivideByZero3 {
  
  	private double sum, current;

	void setCurrent( double sc ) { current = sc; }

	private void doIt( ) { //throws TidalWaveException {

		try {

			Exception tryE = new Exception( "Zero Value" );
			if( current < 0.0  )
				throw new Exception( "Negative Value" );
			
			if( current == 0.0  )
				throw tryE;
			System.out.println( "Current = " + current );
		} catch( Exception e ) {
		System.out.println( "Message->" + e.getMessage() + "<-");
		} 
		System.out.println( "End of Program." );
	} //end do It
	
        private void myString( String str ) {
          
          int i = str.indexOf( ':' );
          
          String str1 = str.substring( 0, i );
          String str2 = str.substring( i+1, str.length() );
          
          System.out.println( "i = " + i );
          System.out.println( "str1->" + str1 + "<-" );
          System.out.println( "str2->" + str2 + "<-" );
        }
        

        private void myStringTokenizer( String str ) {
          
          // Str is string, second argument is delimiters tab,newline,spance...
          StringTokenizer st = new StringTokenizer( str, "\t\n\r%$+" );
          double sum = 0.0 ;
          
          while( st.hasMoreTokens( ) ) {
            
              String temp = st.nextToken();
            
              System.out.println( "Next Token is ->" + temp + "<-" );
              Double dbl = new Double( temp.toString() );
              sum += dbl.doubleValue();
              
              //sum += (new Double( temp.toString() )).doubleValue();
            }
            System.out.println( "Sum = " + sum );
          }
          
          private void myStringTokenizer2( String str ) {
                
          
          StringTokenizer st = new StringTokenizer( str, "\n\r\t%$+", true );
          String temp;
            while( st.hasMoreTokens( ) ) {
            
              temp = st.nextToken();
              System.out.println( "Next Token is ->" + temp + "<-" );
              //sum += (new Double( temp.toString() )).doubleValue();
            }
          }
        
	public static void main( String[] args ) {

		DivideByZero3 oneTime = new DivideByZero3();
                
                oneTime.myString( "12:30" );
                oneTime.myString( "8:04" );

		oneTime.setCurrent( 0.0 );
		oneTime.doIt();
		oneTime.setCurrent( -1.0 );
		oneTime.doIt();
                oneTime.setCurrent( 2.0 );
                oneTime.doIt();
                
                String[] testString = { "$5.0 $67.32 + 27% 35", 
                  "$56.0 $ 23.0 ++ "  };
                for( int i = 0; i < 1 ; ++i ) {  //testString.length
                oneTime.myStringTokenizer( testString[i] );
                oneTime.myStringTokenizer2( testString[i] );
              }
              
       }
}
		
		

