Advertisement

There's no such thing as a stupid question, but they're the easiest to answer.
Login
Search

Advertisement

Software Development Software Development
Search Search
Search for:
Tech Support Guy > > >

Solved: Help with Java Tabular Format


(!)

Carlos_Mendoza's Avatar
Carlos_Mendoza Carlos_Mendoza is offline
Member with 115 posts.
THREAD STARTER
 
Join Date: Nov 2011
Experience: Larning VBA from Forum's
03-May-2012, 08:52 AM #1
Solved: Help with Java Tabular Format
I am writing my 1st Java Program, and I need to have my output in Tabular format, and I have read tutorials on how to do it, but I am just not getting it.

My programs asks for userinput of two numbers, then performs calculations on this numbers. The output that needs to be in tabular format should look like this:
Code:
Math Problem                   Solution
2+2                                4
1*50                              50
How can I have it set up like this?
sepala's Avatar
Computer Specs
Member with 4,038 posts.
 
Join Date: May 2010
Location: Sri Lanka
Experience: Advanced
03-May-2012, 03:35 PM #2
Quote:
Originally Posted by Carlos_Mendoza View Post
I am writing my 1st Java Program, and I need to have my output in Tabular format, and I have read tutorials on how to do it, but I am just not getting it.

My programs asks for userinput of two numbers, then performs calculations on this numbers. The output that needs to be in tabular format should look like this:
Code:
Math Problem                   Solution
2+2                                4
1*50                              50
How can I have it set up like this?
Use escape Characters. In this case "tab" escape character is better. You can find the complete list of characters in here

http://docs.oracle.com/javase/tutori...haracters.html
Carlos_Mendoza's Avatar
Carlos_Mendoza Carlos_Mendoza is offline
Member with 115 posts.
THREAD STARTER
 
Join Date: Nov 2011
Experience: Larning VBA from Forum's
03-May-2012, 03:50 PM #3
Thank you for the response

I tried to use
Code:
" \t\t\t\t "
and add numerous tabs, but dependent upon the length of the data it wouldn't always line up.
andythepandy's Avatar
andythepandy andythepandy is offline
Computer Specs
Member with 367 posts.
 
Join Date: Jul 2006
Location: England
Experience: Intermediate to Advanced
03-May-2012, 04:53 PM #4
Take a look at the String.format (http://docs.oracle.com/javase/1.5.0/...ang.Object...)) method. It is much like Cs printf so you give a Format String (http://docs.oracle.com/javase/1.5.0/...er.html#syntax) and then the values you want to print.

Eg. you can specify a constant width decimal by:
Code:
String.format("%10d", decimalVariable);
In this example the printed number will be at least 10 wide.
Carlos_Mendoza's Avatar
Carlos_Mendoza Carlos_Mendoza is offline
Member with 115 posts.
THREAD STARTER
 
Join Date: Nov 2011
Experience: Larning VBA from Forum's
03-May-2012, 07:52 PM #5
Quote:
Originally Posted by andythepandy View Post
Take a look at the String.format (http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/String.html#format(java.lang.String, java.lang.Object...)) method. It is much like Cs printf so you give a Format String (http://docs.oracle.com/javase/1.5.0/...er.html#syntax) and then the values you want to print.

Eg. you can specify a constant width decimal by:
Code:
String.format("%10d", decimalVariable);
In this example the printed number will be at least 10 wide.
I had been toying with that syntax and came up with this:
Code:
String fmt = "%-7s %-7s % -7s %s%n";   
System.out.printf(fmt, userinput1, "+", userinput2, total);
but my javac command didn't like me adding in the "+" plus to show the addition of userinpu1 and userinput2?
sepala's Avatar
Computer Specs
Member with 4,038 posts.
 
Join Date: May 2010
Location: Sri Lanka
Experience: Advanced
04-May-2012, 01:01 AM #6
Please copy and paste the code. Then only we can examine the issue
andythepandy's Avatar
andythepandy andythepandy is offline
Computer Specs
Member with 367 posts.
 
Join Date: Jul 2006
Location: England
Experience: Intermediate to Advanced
04-May-2012, 08:03 AM #7
I imagine you want to output something like

45 + 12 = 57

with a newline

this can be accomplished with this:
Code:
String fmt = "%-7d %s %-7d %s %-7d \n";
System.out.printf(fmt, userinput1, "+", userinput2, "=", total);
where:
%-7d represents a decimal value
%s represents a string value
\n is a newline

Hope that fixes it
Carlos_Mendoza's Avatar
Carlos_Mendoza Carlos_Mendoza is offline
Member with 115 posts.
THREAD STARTER
 
Join Date: Nov 2011
Experience: Larning VBA from Forum's
04-May-2012, 08:47 AM #8
Quote:
Originally Posted by andythepandy View Post
I imagine you want to output something like

45 + 12 = 57

with a newline

this can be accomplished with this:
Code:
String fmt = "%-7d %s %-7d %s %-7d \n";
System.out.printf(fmt, userinput1, "+", userinput2, "=", total);
where:
%-7d represents a decimal value
%s represents a string value
\n is a newline

Hope that fixes it
That is SOOO Close....this is the output that the code is giving me:
Code:
Math Problem              Solution
4            +1              =5
And I can't seem to get it to line up perfectly under the headers...I'm sure it's a simple tweak but I have tried what I could think of. How can I get the output to be this:
Code:
Math Problem              Solution
4 + 1 =                      5
andythepandy's Avatar
andythepandy andythepandy is offline
Computer Specs
Member with 367 posts.
 
Join Date: Jul 2006
Location: England
Experience: Intermediate to Advanced
04-May-2012, 10:04 AM #9
From what I can see it may be best to do the following:
Code:
System.out.printf("%s \t \t %s\n", "Math problem", "Solution");
		
String fmt = "%-28s %-7d \n";
String problem = Integer.toString(userinput1) + "+" + Integer.toString(userinput2);
System.out.printf(fmt, problem, total);
I've made a separate string that contains the whole math problem as the width of the whole expression is what matters
Carlos_Mendoza's Avatar
Carlos_Mendoza Carlos_Mendoza is offline
Member with 115 posts.
THREAD STARTER
 
Join Date: Nov 2011
Experience: Larning VBA from Forum's
04-May-2012, 10:30 AM #10
Almost there!! It's probably my mistake because I just noticed it is set up wrong in the 2nd example I gave...but I need it to show:
Code:
Math Problem             Solution
2+1                      3
And the code you just postsed is matching my example (SORRY!!)
Code:
Math Problem               Solution
2+1                            3
I need the answer to be lined up with the "S" in solution
andythepandy's Avatar
andythepandy andythepandy is offline
Computer Specs
Member with 367 posts.
 
Join Date: Jul 2006
Location: England
Experience: Intermediate to Advanced
04-May-2012, 10:34 AM #11
You can change how many spaces there are in between the problem and solution by adjusting the format string. Lower the number 28 until it puts the solution at the correct location.
Code:
%-28s
Carlos_Mendoza's Avatar
Carlos_Mendoza Carlos_Mendoza is offline
Member with 115 posts.
THREAD STARTER
 
Join Date: Nov 2011
Experience: Larning VBA from Forum's
04-May-2012, 10:49 AM #12
Last question, if I wanted to show the same output, just with a different sign in between would I need to copy all of this code:
Code:
String fmt = "%-28s %-7d \n";
String problem = Integer.toString(userinput1) + "-" + Integer.toString(userinput2);
System.out.printf(fmt, problem, total2);
So my display code would actually look like this:
Code:
String fmt = "%-28s %-7d \n";
String problem = Integer.toString(userinput1) + "+" + Integer.toString(userinput2);
System.out.printf(fmt, problem, total);    
String fmt = "%-28s %-7d \n";
String problem = Integer.toString(userinput1) + "-" + Integer.toString(userinput2);
System.out.printf(fmt, problem, total);
andythepandy's Avatar
andythepandy andythepandy is offline
Computer Specs
Member with 367 posts.
 
Join Date: Jul 2006
Location: England
Experience: Intermediate to Advanced
04-May-2012, 10:58 AM #13
Yes, that's right. Remember obviously to change the value of 'total' to be userinput1 - userinput2

Also you don't need to redefine the 'fmt' variable after the first time

Last edited by andythepandy; 04-May-2012 at 10:59 AM.. Reason: Adding detail
Carlos_Mendoza's Avatar
Carlos_Mendoza Carlos_Mendoza is offline
Member with 115 posts.
THREAD STARTER
 
Join Date: Nov 2011
Experience: Larning VBA from Forum's
04-May-2012, 10:59 AM #14
Quote:
Originally Posted by Carlos_Mendoza View Post
Last question, if I wanted to show the same output, just with a different sign in between would I need to copy all of this code:
Code:
String fmt = "%-28s %-7d \n";
String problem = Integer.toString(userinput1) + "-" + Integer.toString(userinput2);
System.out.printf(fmt, problem, total2);
So my display code would actually look like this:
Code:
String fmt = "%-28s %-7d \n";
String problem = Integer.toString(userinput1) + "+" + Integer.toString(userinput2);
System.out.printf(fmt, problem, total);    
String fmt = "%-28s %-7d \n";
String problem = Integer.toString(userinput1) + "-" + Integer.toString(userinput2);
System.out.printf(fmt, problem, total);
Disregard, I answered my own question. I just changed the name "problem" to solution1 and solution2 so it read:
Code:
String fmt = "%-28s %-7d \n";
String solution1= Integer.toString(userinput1) + "+" + Integer.toString(userinput2);
System.out.printf(fmt, solution1, total);    
String fmt = "%-28s %-7d \n";
String solution2= Integer.toString(userinput1) + "-" + Integer.toString(userinput2);
System.out.printf(fmt, solution2, total);
I guess I could have done the same thing with problem (problem1, problem2) Opps!
Carlos_Mendoza's Avatar
Carlos_Mendoza Carlos_Mendoza is offline
Member with 115 posts.
THREAD STARTER
 
Join Date: Nov 2011
Experience: Larning VBA from Forum's
04-May-2012, 11:14 AM #15
Actually I do have one more question about this.

Could I do the same thing with JOptionPane.showMessageDialog that was just done with System.out.printf or is that not a possibility?
As Seen On

BBC, Reader's Digest, PC Magazine, Today Show, Money Magazine
WELCOME TO TECH SUPPORT GUY!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.


(clock)
THIS THREAD HAS EXPIRED.
Are you having the same problem? We have volunteers ready to answer your question, but first you'll have to join for free. Need help getting started? Check out our Welcome Guide.

Search Tech Support Guy

Find the solution to your
computer problem!




Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools


WELCOME
You Are Using: Server ID
Trusted Website Back to the Top ↑