How can I use System.out.format() to display multiple items on one line in Java? -
import java.text.*; public class program17 { public static void main(string[] args) { numberformat money=numberformat.getcurrencyinstance(); int[] num={2013,2015,2017,2019,2021}; double[] price={19.85,15.39,16.59,12.35,15.15}; int[] qty={43,12,34,17,10}; double[] value={0,0,0,0,0}; system.out.println("part \titem \titem \ttotal"); system.out.println("number\tprice\tquantity\tvalue\n"); for(int i=0;i<5;i++) { value[i]=val(price[i],qty[i]); //system.out.println(num[i]+"\t"+money.format(price[i])+"\t"+qty[i]+"\t"+money.format(value[i])); system.out.format("%4d\t $%5d.2f\t %2d\t %6.2f", num[i], price[i], qty[i], value[i]); } } public static double val(double p, int q) { double x=p*q; return x; } }
here 100% of code. here output of solution: captured output http://maradastudios.ucoz.com/school/capture2.png
as may able tell, display "data lines" of program system.out.println() statement, however, have had need of system.out.format() in particular type of situation, decided best learn error means. think may because variable price[i] attempting display double, rather int, num[i]. came conclusion because @ first, ran
system.out.format("%4d\t", num[i);
after worked fine, ran with
system.out.format("%4d\t $%5d.2f\t", num[i], price[i);
and got error after that. complete code attempting run. tell me problem system.out.format() statement, , assist me correct it?
you've got stray d
in format string. delete , you'll go.
system.out.format("%4d\t $%5d.2f\t %2d\t %6.2f", num[i], price[i], qty[i], value[i]); ^
Comments
Post a Comment