Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: How to do simple formatting in java times tables?

  1. #1
    Junior Member
    Join Date
    Dec 2011
    Posts
    11
    My Mood
    Stressed
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default How to do simple formatting in java times tables?

    Here is my code

    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)
    Last edited by helloworld922; December 19th, 2011 at 02:02 PM.


  2. #2
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Posts
    834
    My Mood
    Cynical
    Thanks
    7
    Thanked 105 Times in 90 Posts

    Default 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

  3. #3
    Junior Member
    Join Date
    Dec 2011
    Posts
    11
    My Mood
    Stressed
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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

  4. #4
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Posts
    834
    My Mood
    Cynical
    Thanks
    7
    Thanked 105 Times in 90 Posts

    Default Re: How to do simple formatting in java times tables?

    only print them if i = 0

Similar Threads

  1. Java formatting assignment.
    By BITmixit in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 17th, 2011, 11:27 PM
  2. HSSF cell date formatting in java
    By masepogurajesh in forum Java SE APIs
    Replies: 1
    Last Post: March 15th, 2011, 08:45 AM
  3. Creating Excel Pivot Tables using Java
    By prasad.virgo in forum Threads
    Replies: 4
    Last Post: August 4th, 2010, 12:20 PM
  4. merging two tables from two diffrent htmls files using java
    By sukant_at in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: September 1st, 2009, 05:13 AM
  5. merging two tables from two diffrent htmls files using java
    By sukant_at in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: August 7th, 2009, 12:48 PM