|
I noticed a particular If statement in my code is not executed.
I am trying to compare the length of 2 string buffers and append one of them and decrement the length of the other.
The code is an ugly hack because I am time constrained. I also do not have a debugger on the school lab computers so println statements I am using to find out what the program is doing:
// Pad Method
public static void pad( int pnum, int pwidth) throws IllegalArgumentException { if (pnum <= 0 || pwidth <=0 ) throw new IllegalArgumentException( "Plese enter a positive number and try again!" ); // create strings of pnum and pwidth to add to printf below // string pad is added to create a blank space padding later on
String num = String.valueOf( pnum ); String width = String.valueOf( pwidth); String pad = " "; // convert strings value of pad and num to StringBuffer to modify its length later StringBuffer sbpnum = new StringBuffer( num); StringBuffer sbpwidth = new StringBuffer( width); // if sbpwidth length is greater than sbpnum.length then add pad incrementally if ( (sbpwidth.length()) > (sbpnum.length())) { System.out.println( " Entering the If sbpwidth.length > sbpnum.length\n"); System.out.println( "sbpwidth.length is" + sbpwidth.length() + " before deincrement\n"); sbpnum.append( pad ); sbpwidth.setLength((sbpwidth.length()) -1); System.out.println( "After change sbpwidth.length is " + sbpwidth.length() ); }
Any idea what the java compiler is doign and why the condition never becomes true? |