我对如何使用和显示数组列表有疑问。在我的情况下,我认为我正在使用数组列表,因为编译器没有抱怨。但是当我显示它时,会出现一个看起来像这样的错误:
run:
Average Monthly Electricity Bill: 463.26
Exception in thread "main" java.util.IllegalFormatConversionException: f != java.util.ArrayList
Average Monthly Electricity Price Per Kilowatt: at java.util.Formatter$FormatSpecifier.failConversion(Formatter.java:4045)
at java.util.Formatter$FormatSpecifier.printFloat(Formatter.java:2761)
at java.util.Formatter$FormatSpecifier.print(Formatter.java:2708)
at java.util.Formatter.format(Formatter.java:2488)
at java.io.PrintStream.format(PrintStream.java:970)
at java.io.PrintStream.printf(PrintStream.java:871)
at CO2FromElectricityTester.main(CO2FromElectricityTester.java:72)
Java Result: 1
BUILD SUCCESSFUL (total time: 0 seconds)
我该如何解决这个问题?任何帮助将不胜感激。以下是我的代码:
public class CO2FromElectricityTester {
public static void main(String args[]){
// declare & initialize variables
double months = 3.0;
double emissionFactor = 1.37;
int kilowattHoursSept = 109;
int kilowattHoursOct = 87;
int kilowattHoursNov = 93;
double monthlyCostSept = 551.51;
double monthlyCostOct = 392.84;
double monthlyCostNov = 445.42;
double avgKilowattHours = (kilowattHoursSept + kilowattHoursOct +
kilowattHoursNov) / 3;
double avgMonthlyCost = (monthlyCostSept + monthlyCostOct +
monthlyCostNov) / 3;
// create object
CO2FromElectricity CO2 = new CO2FromElectricity();
// // // declare & initialize variables for methods
// // create array lists
ArrayList<Double> monthlyPriceForElec = new ArrayList<Double>();
ArrayList<Double> monthlyElectricBill = new ArrayList<Double>();
// average monthly price for electricity
double avgPricePerKilowatt = CO2.calcPricePerKilowatt(avgKilowattHours,
avgMonthlyCost);
// average monthly electric bill
double avgCO2Emission = CO2.calcCO2Emission(emissionFactor, months,
avgMonthlyCost, avgPricePerKilowatt);
// initialize array lists
monthlyPriceForElec.add(avgPricePerKilowatt);
monthlyElectricBill.add(avgCO2Emission);
// display results
System.out.printf("%1s%34.2f%n", "Average Monthly Electricity Bill: ",
avgMonthlyCost);
System.out.printf("%1s%20.2f%n", "Average Monthly Electricity Price Per "
+ "Kilowatt: ", monthlyPriceForElec);
System.out.printf("%1s%10.2f%n", "CO2 Emissions from Electricity Usage "
+ "in a 3 Month Period: ", monthlyElectricBill);
}
}
如果有帮助,下面是我声明方法的其他文件:
public class CO2FromElectricity {
// default constructor
CO2FromElectricity(){
}
// method for calculating price per kilowatt
public double calcPricePerKilowatt(double kilowattHours, double monthlyCost){
return monthlyCost / kilowattHours;
}
// method for calculating CO2 emission
public double calcCO2Emission(double emissionFactor, double months,
double avgMonthlyCost, double avgPricePerKilowatt){
return (avgMonthlyCost / avgPricePerKilowatt) * emissionFactor * months;
}
}
答案 0 :(得分:0)
错误很明显。您正在尝试格式化指定浮点格式的ArrayList。你的问题有点不清楚,但不是
System.out.printf("%1s%20.2f%n", "Average Monthly Electricity Price Per "
+ "Kilowatt: ", monthlyPriceForElec); // you want the result
我想你想要
System.out.printf("%1s%20.2f%n", "Average Monthly Electricity Price Per "
+ "Kilowatt: ", avgPricePerKilowatt); // like this
然后在下一行,您需要avgCO2Emission
而不是monthlyElectricBill
。
如果您想打印整个列表,则需要分别迭代列表。
答案 1 :(得分:0)
协助,不是答案的一部分。 使用接口而不是具体类。 不要这样做:
ArrayList<Double> monthlyPriceForElec = new ArrayList<Double>();
请改为:
List<Double> monthlyPriceForElec = new ArrayList<Double>();
列表(包括ArrayList)是数据的集合。 通过循环遍历列表并显示每个元素来显示列表的内容。
List<Double> hooty = new ArrayList<Double>();
... put stuff in hooty.
for (Double blammy : hooty)
{
System.out.printf("blammy not hooty: %20.2f", blammy);
}
答案 2 :(得分:0)
System.out.printf没有对Collections的特殊支持,它会将它视为常规对象(不是Iterable)。如果要格式化集合中的项目,则需要循环遍历它们并在每个项目上调用System.out.printf()。
答案 3 :(得分:0)
在你做打印语句的地方,你放了“System.out.printf”。我认为您应该放置“System.out.println”,否则编译器只会有一个巨大的脑放屁。我认为这是错误,但我不确定。尝试将打印语句的“f”部分更改为“ln”,看看是否有效。