Click here to Skip to main content
15,886,518 members
Please Sign up or sign in to vote.
5.00/5 (2 votes)
See more:
I need to make a table that well somewhat look like the one on the bottom. I am not exactly sure what is wrong with my coding. I get odd errors when I execute it. Please help me! Thank you so much for taking the time to read through my mess. :)

errors:
Exception in thread "main" java.util.IllegalFormatConversionException: d != java.lang.Double
at java.base/java.util.Formatter$FormatSpecifier.failConversion(Formatter.java:4426)
at java.base/java.util.Formatter$FormatSpecifier.printInteger(Formatter.java:2938)
at java.base/java.util.Formatter$FormatSpecifier.print(Formatter.java:2892)
at java.base/java.util.Formatter.format(Formatter.java:2673)
at java.base/java.io.PrintStream.format(PrintStream.java:1053)
at java.base/java.io.PrintStream.printf(PrintStream.java:949)
at TemptTable.main(TemptTable.java:23)


| V= 5 V=10 V=15 V=20 V=25 V=30 V=35 V=40 V=45 V=50
--------------------------------------------------------------------------------
T = -20 | -34.0 -40.7 -45.0 -48.2 -50.8 -53.0 -54.9 -56.6 -58.1 -59.5
T = -15 | -28.1 -34.5 -38.6 -41.7 -44.1 -46.2 -48.1 -49.7 -51.1 -52.4
T = -10 | -22.3 -28.3 -32.2 -35.1 -37.5 -39.4 -41.2 -42.7 -44.1 -45.3
T = -5 | -16.4 -22.1 -25.8 -28.6 -30.8 -32.7 -34.3 -35.7 -37.0 -38.2
T = 0 | -10.5 -15.9 -19.4 -22.0 -24.1 -25.9 -27.4 -28.8 -30.0 -31.1
T = 5 | -4.6 -9.7 -13.0 -15.4 -17.4 -19.1 -20.5 -21.8 -23.0 -24.0
T = 10 | 1.2 -3.5 -6.6 -8.9 -10.7 -12.3 -13.6 -14.8 -15.9 -16.9
T = 15 | 7.1 2.7 -0.2 -2.3 -4.0 -5.5 -6.8 -7.9 -8.9 -9.8
T = 20 | 13.0 8.9 6.2 4.2 2.6 1.3 0.1 -0.9 -1.8 -2.7
T = 25 | 18.9 15.1 12.6 10.8 9.3 8.1 7.0 6.1 5.2 4.4
T = 30 | 24.7 21.2 19.0 17.4 16.0 14.9 13.9 13.0 12.2 11.5
T = 35 | 30.6 27.4 25.4 23.9 22.7 21.7 20.8 20.0 19.3 18.6
T = 40 | 36.5 33.6 31.8 30.5 29.4 28.5 27.7 26.9 26.3 25.7

What I have tried:

import java.util.Scanner;
public class TemptTable
{
public static void main(String[] args)
{
System.out.println("not actually a title thingy");

System.out.print(" ");

for(int v = 5; v <= 50; v+=5)
System.out.print(" " + "V= " + v);

System.out.println("\n--------------------------------------------------------------------------------------");

for (int t = -20; t <= 40; t+=5)
{
System.out.print ("T= " + t + " | ");
for (int v = 5; v <= 50; v+=5)
{
double equation = 35.74 + (0.6215 * t) - (35.75 * Math.pow(v, 0.16)) + (0.4275 * t * Math.pow(v, 0.16));
System.out.printf("%4d%2f", equation);
}
System.out.println();
}
}
}
Posted
Updated 10-Oct-19 9:08am

1 solution

Reading the exception that your code throws shows it has trouble converting your double into a '%d'which is a decimal integer. Makes sense, a double isn't an integer.

See this tutorial from Oracle.

If I replace;
System.out.printf("%4d%2f", equation);

with:
System.out.printf("%.1f ", equation);


It runs as expected for me and gives the result you show in your question.

Hope this helps you. Cheers.

UPDATE:

You can make it look neater with a "fill" method that replaces a String of preset characters(String chr) with an input string (String msg). There are many possibilities, like using a JFrame(window) and a JTable to display the result. But thats for you to try out.

public static void main(String[] args)
{
	System.out.println("not actually a title thingy");
	System.out.print(" ");
	for(int v = 5; v <= 50; v+=5)
	System.out.print(" " + "V= " + v);
	System.out.println("\n--------------------------------------------------------------------------------------");
		
	for (int t = -20; t <= 40; t+=5)
	{
		System.out.print( fill("T = " + t + " | ","           ") );
		for (int v = 5; v <= 50; v+=5)
		{
			double equation = (35.74 + (0.6215 * t) - (35.75 * Math.pow(v, 0.16)) + (0.4275 * t * Math.pow(v, 0.16)));
			String res = String.format("%.1f ", equation);
			System.out.printf( fill(res, "        ") );
		}
		System.out.println();
	}
}

public static String fill(String msg, String chr) 
{
	String res = "";
	int leng = chr.length();
	for (int s=0; s<leng; s++) res += chr;
	String concat = msg+res;
	return concat.substring(0, leng);
}
 
Share this answer
 
v2
Comments
LillyLeaf 10-Oct-19 20:22pm    
Thank you so much for helping me! You’re amazing!!! So cool!!! :) The numbers actually show up! but how would I make it look cleaner... in a way where they would kind of line up with each other.
Catey Category 11-Oct-19 1:02am    
Sure thing, take a loot at the update of my solution.
LillyLeaf 11-Oct-19 1:13am    
Thank you very much! You are amazing. :D
Catey Category 11-Oct-19 2:02am    
Thanks, good luck with your app. :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900