Advertisement
Guest User

Untitled

a guest
Jul 27th, 2017
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.91 KB | None | 0 0
  1. /*
  2.  * To change this template, choose Tools | Templates
  3.  * and open the template in the editor.
  4.  */
  5.  
  6. package treedraw;
  7.  
  8. /**
  9.  *
  10.  * @author mute
  11.  */
  12. public class Main {
  13.  
  14.     /**
  15.      * @param args the command line arguments
  16.      */
  17.     public static void main(String[] args) {
  18.                 final int height = 9; //Tree height
  19.         final int stemHeight = 2;
  20.         //counters
  21.         int count = 0;
  22.         int whiteCount = 4;
  23.         int drawCount = 1;
  24.         int i;
  25.  
  26.         //Strings
  27.         String drawSpace = "*";
  28.         String whiteSpace = "    ";
  29.         while ( count <= (height - 2) ) {
  30.  
  31.             while ( whiteCount >= 0 ) {
  32.  
  33. //              for ( i = whiteCount; i >= 0; i-- )  //This loop adjust the whiteSpace ' ' String
  34.                                     whiteSpace = whiteSpace.substring( 1, (whiteSpace.length()));
  35.  
  36.                 for ( i = drawCount; i >= 0; i--)  //This loop adjusts the drawSpace '*' String
  37.                                     drawSpace += '*';
  38.                 System.out.println( whiteSpace + drawSpace + whiteSpace );
  39.                 drawCount++;  //iterates the counters
  40.                 whiteCount--;
  41.                 count++;
  42.  
  43.             }
  44.                 }
  45.         while ( (count > ( height - 2 )) && (count < height ) ) {
  46.             drawCount = 3;
  47.             whiteCount = 3;
  48.                         whiteSpace = " ";
  49.                         drawSpace = "*";
  50.             //Resused for loop to draw stem rather than just write the characters...could be methodized later
  51.             for ( i = whiteCount; i >= 0; i-- )
  52.                 whiteSpace += ' ';
  53.             for ( i = drawCount; i >= 0; i-- )
  54.                     drawSpace += '*';
  55.             for ( i = stemHeight; i >= 0; i--)
  56.                 System.out.println( whiteSpace + drawSpace + whiteSpace );
  57.         }
  58.     }
  59. }
  60.  
  61.  
  62. ////////////
  63. Throws this error.
  64. ////////////
  65. Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: -1
  66.         at java.lang.String.substring(String.java:1937)
  67.         at treedraw.Main.main(Main.java:34)
  68. Java Result: 1
  69. BUILD SUCCESSFUL (total time: 1 second)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement