java - String Formatting -


i trying construct program prints pascal's triangles @ different heights through calling method construct triangle int parameter height of triangle. when trying run program, first pascal's triangle prints bu exception error, reading this:

java.util.formatflagsconversionmismatchexception: conversion = s, flags = 0 @ java.util.formatter$formatspecifier.failmismatch(unknown source) @ java.util.formatter$formatspecifier.checkbadflags(unknown source) @ java.util.formatter$formatspecifier.checkgeneral(unknown source) @ java.util.formatter$formatspecifier.<init>(unknown source) @ java.util.formatter.parse(unknown source) @ java.util.formatter.format(unknown source) @ java.io.printstream.format(unknown source) @ pascalstriangle.drawtriangle(pascalstriangle.java:19) @ pascalstriangle.main(pascalstriangle.java:39) @ sun.reflect.nativemethodaccessorimpl.invoke0(native method) @ sun.reflect.nativemethodaccessorimpl.invoke(unknown source) @ sun.reflect.delegatingmethodaccessorimpl.invoke(unknown source) @ java.lang.reflect.method.invoke(unknown source) @ edu.rice.cs.drjava.model.compiler.javaccompiler.runcommand(javaccompiler.java:272) 

through debugging, noticed there issue how formatted string output in line 19. new java programming , have tried work through different formatting issues in code, have been stumped on how make work. suggestions on how can prevent exception error happening?

here code program:

public class pascalstriangle {    private int height;    public void drawtriangle(int height) {     system.out.println("a pascal triangle height " + height);     for(int = 0; <= height; i++) {       int number = 1;       system.out.format("%" + ((height-i) * 2) + "s", " ");        for(int j = 0; j <= i; j++) {         system.out.format("%5d", number);         number = number * (i - j) / (j + 1);        }     system.out.println();        }   }      public static void main(string[] args) {      pascalstriangle pascal = new pascalstriangle();      pascal.drawtriangle(4);     pascal.drawtriangle(10);     pascal.drawtriangle(7);     pascal.drawtriangle(2);   } } 

since in for loop iterating while i <= height possible i equal height creating "%0s" incorrect (number after % must positive or negative). try adding + 1 in code

system.out.format("%" + ((height - + 1) * 2) + "s", " "); //                                   ^here 

or if i should number of rows change i <= height i < height

update

i not sure if meant in comment maybe try way:

public void drawtriangle(int height) {     system.out.println("a pascal triangle height " + height);     (int = 0; <= height; i++) {         int number = 1;         if (i != height)             system.out.format("%" + ((height - i) * 2) + "s", " ");          (int j = 0; j <= i; j++) {             if (j == 0)                 system.out.print(number);             else                 system.out.format("%4d", number);             number = number * (i - j) / (j + 1);          }         system.out.println();     } } 

i added few conditions prevent inserting spaces @ last row , when printing first 1 in row. changed %5d %4d create more "balanced triangle". in case make more "width" change %4d %6d , use "%" + ((height - i) * 3) + "s" (3 half of %6d, %8d use ((height - i) * 4))


Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

javascript - jQuery .height() return 0 when visible but non-0 when hidden -