How to do simple formatting in java times tables?
Here is my code
Code java:
import javax.swing.*;
public class lab8a
{
int rc = 0;
public static void main(String[] args)
{
lab8a t = new lab8a();
t.process();
}
int process()
{
boolean keepgoing = true;
while(keepgoing)
{
rc = userPrompt();
if(rc == 0)
{
keepgoing = false;
continue;
}
rc = calcTable(rc);
} //End of while
return rc;
} //End of method
int calcTable(int n)
{
int width = n;
int length = n;
int i,j;
int threeD[][] = new int[length][width];
for (i=0; i < length; i++)
{
System.out.println("");
for(j=0; j < width; j++)
{
{
threeD[i][j]= (i+1)*(j+1);
System.out.printf("%4d ",threeD[i][j]);
System.out.printf("%4d ",threeD[i] + "|");
System.out.printf("%4d ",threeD[j] + "_");
}
}
}
return 0;
} //End of method
public int userPrompt()
{
String ans = JOptionPane.showInputDialog(null,
"Please enter a number between 1 and 32");
if (ans == null)
{
return 0;
}
else
{
int n = Integer.parseInt(ans);
return n;
}
}
}
im working with in java, i'm trying to figure out how to get a nice border around the first row and column in my times table. Something like "|" and "_" . Any ideas?
I put the formatting under the System.out.println but it is giving me these errors
Exception in thread "main" java.util.IllegalFormatConversionException: d != java.lang.String
at java.util.Formatter$FormatSpecifier.failConversion (Formatter.java:3999)
at java.util.Formatter$FormatSpecifier.printInteger(F ormatter.java:2709)
at java.util.Formatter$FormatSpecifier.print(Formatte r.java:2661)
at java.util.Formatter.format(Formatter.java:2433)
at java.io.PrintStream.format(PrintStream.java:920)
at java.io.PrintStream.printf(PrintStream.java:821)
at lab8a.calcTable(lab8a.java:55)
at lab8a.process(lab8a.java:26)
at lab8a.main(lab8a.java:11)
Re: How to do simple formatting in java times tables?
"%4d ",threeD[j] + "_"
Here you specify that you are passing the first argument in as a double, then pass it a string.
Chris
Re: How to do simple formatting in java times tables?
ok i changed that but still im getting crappy results the "|" and "_" are still within the time table not just within the first row and column
Re: How to do simple formatting in java times tables?