java - Error printing string formatted with %.0f using an int substitute -


i trying print string in tostring(). prints fine if remove %.0f , this.years substitute, not print if include it. not sure why. reason can think of perhaps because years int, , string formatting works doubles?

how can change code can substitute %.0f this.years?

// instance members private double amountborrowed; private double yearlyrate; private int years; public double a; public double n = years * 12;  // public instance method public myloan(double amt, double rt, int yrs) {     this.amountborrowed = amt;     this.yearlyrate = rt;     this.years = yrs;     this.n = yrs * 12;  public string tostring() {      string temp1 = string.format("loan: $%.2f @ %.2f %.0f years.", this.amountborrowed, this.yearlyrate, this.years);     return temp1; 

the error receive when trying print is:

exception in thread "main" java.util.illegalformatconversionexception: f != java.lang.integer @ java.util.formatter$formatspecifier.failconversion(formatter.java:4302) @ java.util.formatter$formatspecifier.printfloat(formatter.java:2806) @ java.util.formatter$formatspecifier.print(formatter.java:2753) @ java.util.formatter.format(formatter.java:2520) @ java.util.formatter.format(formatter.java:2455) @ java.lang.string.format(string.java:2940) @ hw2.myloan.tostring(myloan.java:61) @ hw2.myloan.main(myloan.java:101)

use '%d' instead of '%.0f' formating 'int'.

for more formatting see here http://docs.oracle.com/javase/7/docs/api/java/util/formatter.html


Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -