Modify a text file: $/getdec1a.java
$/getdec1a.java
//// Convert keyboard input to a decimal number. //// bam:2a20: getdec1a.java String title="Convert keyboard input to a decimal number."; String who="B.A.Martin: 2012/10/20 getdec1a.java"; String digits=""; // String of digits entered (but not decoded). String results=""; // List of previous results. int number=-1; // Most-recent number decoded. boolean debug=false; // DEBUG int nsave, msave; String old=""; void setup() { //// Set up screen size. size( 400, 300 ); } void draw() { //// paint the background (and instructions), then display the number. background( 200,240,255 ); scene(); show(); } void scene() { //// Title, signature, and instructions to user. fill(0); // Black text. text( title, width/2-100, 10 ); text( who, 20, height-20); // text( "Enter a number, using the keyboard.", 10, 50 ); fill( 150,150,150 ); text( "Space-bar to compute result.)", 10, 62 ); text( "q to quit. = for debugging info.", 10, 74 ); } void show() { //// Display the string of digits. if (digits.length() > 0 ) { fill( 0,0,255 ); text( "Digits entered so far:", 10, 100 ); text( digits, 140, 100 ); } //// Display all previous results (if any). if (results.length() > 0 ) { fill(255,0,0); text( "Results:", 20, 150 ); text( results, 20, 170 ); } if (debug) debug(); } void debug() { // DEBUG fill( 0,150,255 ); text( "Length(): "+digits.length(), width-200, 100 ); // if (old.length() > 0) { fill( 255,0,255 ); int m=2; text( "Old number: "+nsave, width-200, 100+12*m++ ); text( "Old string: "+old, width-200, 100+12*m++ ); text( "Old multiplier: "+msave, width-200, 100+12*m++ ); text( "Old length: "+old.length(), width-200, 100+12*m++ ); } } //// EVENT HANDLERS //// void keyPressed() { //// If key is a digit, just append it to the string of digits. if ( key >= '0' && key <= '9' ) { //// Input a digit. int digit= key - '0'; digits = digits + digit; // Append digit to string. } else if (key == '=') { } else if (key == '?') { debug= ! debug; } else if (key == 'q') { exit(); //++++ Check for . decimal point. // +++ Check for + - // +++ Also check for 'h' maybe 'e' } else if (key == ' ') { //// SPACE: Decode the # and empty the string. compute(); } } void compute() { //// Compute value of digits string. Store in number. Clear string. number= dec( digits ); // Decode the string. results += number; // Append number to result results += '\n'; // Append newline old= digits; // (Save old string for debugging.) digits= ""; // Empty the string. } int dec( String s ) { //// Convert string to decimal. //// number= sum of a[j] * 10 ^ j int number; // Multiplier for highest digit is 10^( # of digits minus 1 ) int multiplier= 1; for ( int j=0; j< digits.length(); j++ ) { multiplier = multiplier * 10; } multiplier = multiplier / 10; // DEBUG: save multiplier. msave= multiplier; // Now, for each digit, extract the value and multiply. // Add that product to the total. number= 0; for ( int j=0; j< digits.length(); j++ ) { int d= digits.charAt(j) - '0'; number += d * multiplier; multiplier /= 10; } nsave= number; return number; }