Reputation: 267
I have a question on how to display booleans with printf
. In my case, I think the only problem I have is with displaying boolean types with printf
. How can I fix this? Any help will be greatly appreciated. When I run my program, I get this:
run:
_____________________________________________________________________________________________________________
| Household Waste Recycled | | Pounds of CO2 |
_____________________________________________________________________________________________________________
Index People Paper Plastic Glass Cans Total Emission ReductionNet Emission
Exception in thread "main" java.util.IllegalFormatConversionException: f != CO2FromWaste
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 CO2FromWasteTester.main(CO2FromWasteTester.java:55)
Java Result: 1
BUILD SUCCESSFUL (total time: 0 seconds)
Below is the code for my main method file:
import java.util.ArrayList;
public class CO2FromWasteTester {
public static void main(String args[]){
ArrayList<CO2FromWaste> CO2 = new ArrayList<CO2FromWaste>();
CO2.add(new CO2FromWaste(1, true, true, true, true));
CO2.add(new CO2FromWaste(2, true, false, true, false));
CO2.add(new CO2FromWaste(3, true, true, true, false));
CO2.add(new CO2FromWaste(4, false, false, false, true));
CO2.add(new CO2FromWaste(5, false, true, false, true));
CO2.add(new CO2FromWaste(6, true, true, true, true));
CO2FromWaste data;
for(int i = 0; i < CO2.size(); i++){
data = CO2.get(i);
data.getNumberOfPeople();
data.Paper();
data.Plastic();
data.Glass();
data.Can();
data.NetEmission();
data.reduction();
data.totalEmission();
}
// create table headings
System.out.println("___________________________________________________"
+ "__________________________________________________________");
System.out.printf("%65s%44s%n", "| Household Waste Recycled |",
"| Pounds of CO2 |");
System.out.println("___________________________________________________"
+ "__________________________________________________________");
System.out.printf("%1s%12s%12s%12s%12s%12s%20s%12s%12s%n%n", "Index", "People",
"Paper", "Plastic", "Glass", "Cans", "Total Emission", "Reduction",
"Net Emission");
// call methods
for(int i = 0; i < CO2.size(); i++){
data = CO2.get(i);
System.out.printf("%1f%1f%5b%5b%5b%5b%4.2f%4.2f%4.2f",
data = CO2.get(i),
data.getNumberOfPeople(),
data.Paper(),
data.Plastic(),
data.Glass(),
data.Can(),
data.totalEmission(),
data.reduction(),
data.NetEmission());
}
}
}
Upvotes: 1
Views: 668
Reputation: 532
Consider usage of the ternary operator:
printf("%s", (true==value)?"true":"false" );
Upvotes: 0
Reputation: 10233
It's because you are repeating the call data = CO2.get(i)
, and that can't be printed. Try this:
for(int i = 0; i < CO2.size(); i++){
data = CO2.get(i);
System.out.printf("%1f%1f%5b%5b%5b%5b%4.2f%4.2f%4.2f",
data.getNumberOfPeople(),
data.Paper(),
data.Plastic(),
data.Glass(),
data.Can(),
data.totalEmission(),
data.reduction(),
data.NetEmission());
}
Upvotes: 1