Results 1 to 4 of 4

Thread: How to get a Java Program output in Tabular format

  1. #1
    Join Date
    Apr 2012
    Posts
    52

    How to get a Java Program output in Tabular format

    Is there any way to get the output of my handwritten java program in tabular format? I don’t want to create any table or something its just that the output should be in a proper column and row format.

    I am coded the program in such a way that the system asks the user to input two numbers, then calculates two number with addition and subtraction and then prints out the output with the following section.
    Code:
    Operation                   Result
    10+10                         20
    10-10                          0

  2. #2
    Join Date
    May 2011
    Posts
    111

    Re: How to get a Java Program output in Tabular format

    If you have been able to code the entire program successfully then I don’t think you should face any problem while preparing the format and coding for the resulting output. Obviously if you try to simply print the result of the entered input numbers and the operations performed on them then you might get a messy output where all the numbers would be sticking each other as if there is no space on the screen and also the output would not be understandable.

    The best you can try out is the use of Escape sequence for arranging the output in a proper arranged fashion. Make use of \t (tab escape sequence) which might help you in separating and maintaining the distance between the “operation” and “Result” column.

  3. #3
    Join Date
    May 2011
    Posts
    99

    Re: How to get a Java Program output in Tabular format

    Even I think making the use of escape characters in such cases is very much recommended. I guess not just tab escape character but you will also have to make use of \n i.e. new line for creating the above format. I mostly take help of the escape sequences to create such tabular format of output. Anyways I will help you with various types of escape sequence along with their description.

    • \t : used to add tab
    • \n : used for inserting new line
    • \f : used to insert a formfeed
    • \r : used to insert a carriage return
    • \b : used to insert a backspace
    • \" : used to add double quote
    • \\ : used to insert a backslash
    • \' : used to insert a single quote

  4. #4
    Join Date
    May 2009
    Posts
    513

    Re: How to get a Java Program output in Tabular format

    After playing a lot around the code I found the following trick. I would request you to first look at the following code:
    Code:
    System.out.printf("%s \t \t %s\n", "Operation", "Result");		
    String format = "%-28s %-7d \n";
    String operators = Integer.toString(num1) + "+" + Integer.toString(num2);
    System.out.printf(format, operators, total);
    The resulting input will be no doubt similar as per your requirement. Here what I did is made use of the following things:
    • %s – which is for String value
    • %d – representing decimal value
    • \n – for newline

    I converted the two numbers into string first combining them together into a string value called as Operators. Then I printed all of them together.

Similar Threads

  1. Replies: 3
    Last Post: 04-01-2014, 02:28 PM
  2. how to increase the number of decimal digits in JAVA program output?
    By Dennis Racket in forum Software Development
    Replies: 2
    Last Post: 19-01-2012, 01:38 PM
  3. How to ASP.NET data will be output directly into Excel format
    By Savannah87 in forum Software Development
    Replies: 4
    Last Post: 12-02-2010, 03:58 AM
  4. C program is unable to give correct output
    By Ujagar in forum Software Development
    Replies: 4
    Last Post: 29-01-2010, 07:45 PM
  5. Link List Example in Java Sample program in Java
    By trickson in forum Software Development
    Replies: 2
    Last Post: 04-08-2009, 08:23 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •