Thursday, July 4, 2013

printf with Strings

//all formatting options: http://docs.oracle.com/javase/6/docs/api/java/util/Formatter.html#syntax

//test padding on both sides of "HI"//

(-8..8).each{ i ->
    if (i == 0) i = '' // avoid illegal printf syntax
    
    String f = "**%${i}s**"
    print ((f + ' --> ').padRight(15)) // just for debugging
    printf(f+'\n', "HI")
}


//Tic Tac Toe//

List field = [["","x","o"],["","x",""],["o","",""]]
field.eachWithIndex { line,index ->
    if(index > 0) {
        println "---+" * 2 + "---"
    }
    printf(" %-2s| %-2s| %-2s\n", line)
}

No comments:

Post a Comment