# Solved: Help with Java Tabular Format



## Carlos_Mendoza (Nov 12, 2011)

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:

```
Math Problem                   Solution
2+2                                4
1*50                              50
```
How can I have it set up like this?


----------



## sepala (May 20, 2010)

Carlos_Mendoza said:


> 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:
> 
> ...


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/tutorial/java/data/characters.html


----------



## Carlos_Mendoza (Nov 12, 2011)

Thank you for the response 

I tried to use 

```
" \t\t\t\t "
```
 and add numerous tabs, but dependent upon the length of the data it wouldn't always line up.


----------



## andythepandy (Jul 2, 2006)

Take a look at the String.format (http://docs.oracle.com/javase/1.5.0...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/docs/api/java/util/Formatter.html#syntax) and then the values you want to print.

Eg. you can specify a constant width decimal by:

```
String.format("%10d", decimalVariable);
```
In this example the printed number will be at least 10 wide.


----------



## Carlos_Mendoza (Nov 12, 2011)

andythepandy said:


> 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/docs/api/java/util/Formatter.html#syntax) and then the values you want to print.
> 
> Eg. you can specify a constant width decimal by:
> 
> ...


I had been toying with that syntax and came up with this:

```
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 (May 20, 2010)

Please copy and paste the code. Then only we can examine the issue


----------



## andythepandy (Jul 2, 2006)

I imagine you want to output something like

45 + 12 = 57

with a newline

this can be accomplished with this:

```
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 (Nov 12, 2011)

andythepandy said:


> I imagine you want to output something like
> 
> 45 + 12 = 57
> 
> ...


That is SOOO Close....this is the output that the code is giving me:

```
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:

```
Math Problem              Solution
4 + 1 =                      5
```


----------



## andythepandy (Jul 2, 2006)

From what I can see it may be best to do the following:

```
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 (Nov 12, 2011)

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:

```
Math Problem             Solution
2+1                      3
```
And the code you just postsed is matching my example (SORRY!!)

```
Math Problem               Solution
2+1                            3
```
I need the answer to be lined up with the "S" in solution


----------



## andythepandy (Jul 2, 2006)

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.

```
%-[COLOR="Red"]28[/COLOR]s
```


----------



## Carlos_Mendoza (Nov 12, 2011)

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:

```
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:

```
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 (Jul 2, 2006)

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


----------



## Carlos_Mendoza (Nov 12, 2011)

Carlos_Mendoza said:


> 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:
> 
> ```
> String fmt = "%-28s %-7d \n";
> ...


Disregard, I answered my own question. I just changed the name "problem" to solution1 and solution2 so it read:

```
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 (Nov 12, 2011)

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?


----------



## andythepandy (Jul 2, 2006)

Yes you can, you have to use the String.format(format, vars...) function to generate a string to show first though, i.e.

```
String fmt = "%-28s %-7d \n";
String solution1= Integer.toString(userinput1) + "+" + Integer.toString(userinput2);

String solution2= Integer.toString(userinput1) + "-" + Integer.toString(userinput2);

String displayString = String.format(fmt, solution1, total);
displayString += String.format(fmt, solution2, total);

JOptionPane.showMessageDialoag(null, displayString);
```
String.format takes the exact same arguments as System.out.printf. It takes the format string and then a list of object to print


----------



## Carlos_Mendoza (Nov 12, 2011)

andythepandy said:


> ```
> displayString += String.format(fmt, solution2, total);
> ```


What does that line of code mean? What is it telling java to do?


----------



## Carlos_Mendoza (Nov 12, 2011)

Using the above code it isn't putting a tab between my two headers in my header row 

I am trying to use this:

```
JOptionPane.showMessageDialog(null, "Math Problem\tSolution" + "\n" + displayString);
```
Which I thought would add a tab between the two but for some reason it's not.


----------



## andythepandy (Jul 2, 2006)

If you want the JOptionPane Dialog box to display the header row and the results, then you need to create a string that contains the header row, a new line, and the results. You cannot make multiple calls to JOptionPane.showMessageDialog() to display the header row and the results otherwise you will get one message with only the header row and another message (only displayed after you press OK) that contains the results.

The line above simply joins together the formatted header row with the results row. It is equivalent to writing this:

```
displayString = displayString + String.format(fmt, solution2, total);
```
The JOptionPane dialog box doesn't recognise \t as meaning a tab character. You can use the String.format function as like before like so:

```
JOptionPane.showMessageDialog(null, String.format("%s \t %s \n", "Math Problem", "Solution") + displayString);
```


----------



## Carlos_Mendoza (Nov 12, 2011)

Perfect yet again! Thank you for the support!


----------

