Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Register Tags Active Topics RSS Search Mark Forums Read

Sponsored Links



How to get a Java Program output in Tabular format

Software Development


Reply
 
Thread Tools Search this Thread
  #1  
Old 17-07-2012
Member
 
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

Reply With Quote
  #2  
Old 17-07-2012
Member
 
Join Date: May 2011
Posts: 103
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.
Reply With Quote
  #3  
Old 18-07-2012
Member
 
Join Date: May 2011
Posts: 95
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
Reply With Quote
  #4  
Old 18-07-2012
Member
 
Join Date: May 2009
Posts: 503
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.
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "How to get a Java Program output in Tabular format"
Thread Thread Starter Forum Replies Last Post
how to convert the Nero 11 & patent activation fax output to e-mail format Iraqat Windows Software 3 04-01-2014 02:28 PM
how to increase the number of decimal digits in JAVA program output? Dennis Racket Software Development 2 19-01-2012 01:38 PM
How to ASP.NET data will be output directly into Excel format Savannah87 Software Development 4 12-02-2010 03:58 AM
C program is unable to give correct output Ujagar Software Development 4 29-01-2010 07:45 PM
Link List Example in Java Sample program in Java trickson Software Development 2 04-08-2009 08:23 PM


All times are GMT +5.5. The time now is 11:54 AM.