// Scott DeRuiter 7/17/01 // Pyramid.java // Use recursion to deconstruct a String, printing out a pyramid of the letters. // 7/20/2011 - Greenstein: Changed TextReader to Scanner import java.util.Scanner; public class Pyramid { private String chunk; private String original; public Pyramid ( ) { chunk = ""; original = ""; } public static void main ( String [] args ) { Pyramid p = new Pyramid ( ); p.GetPhrase ( ); p.Deconstruct ( ); } public void GetPhrase ( ) { Scanner console = new Scanner ( ); do { System.out.print ( "\n\nEnter a phrase: " ); chunk = console.nextLine ( ); } while ( chunk == null ); if ( chunk.length ( ) % 2 == 0 ) chunk = chunk + " "; original = chunk; System.out.println ( "\n\n" ); } public void Deconstruct ( ) { Piece ( 0 ); System.out.println ( "\n\n" ); } public String Piece ( int n ) { chunk = original.substring ( original.length ( ) / 2 - n, original.length ( ) / 2 + n + 1 ); System.out.println ( chunk ); if ( chunk.equals ( original ) ) return ""; else return Piece ( n + 1 ) ; } }