1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

Solved: Help with Java Tabular Format

Discussion in 'Software Development' started by Carlos_Mendoza, May 3, 2012.

Thread Status:
Not open for further replies.
Advertisement
  1. Carlos_Mendoza

    Carlos_Mendoza Thread Starter

    Joined:
    Nov 12, 2011
    Messages:
    124
    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?
     
  2. sepala

    sepala

    Joined:
    May 20, 2010
    Messages:
    4,038
    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
     
  3. Carlos_Mendoza

    Carlos_Mendoza Thread Starter

    Joined:
    Nov 12, 2011
    Messages:
    124
    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.
     
  4. andythepandy

    andythepandy

    Joined:
    Jul 2, 2006
    Messages:
    367
  5. Carlos_Mendoza

    Carlos_Mendoza Thread Starter

    Joined:
    Nov 12, 2011
    Messages:
    124
    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?
     
  6. sepala

    sepala

    Joined:
    May 20, 2010
    Messages:
    4,038
    Please copy and paste the code. Then only we can examine the issue
     
  7. andythepandy

    andythepandy

    Joined:
    Jul 2, 2006
    Messages:
    367
    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
     
  8. Carlos_Mendoza

    Carlos_Mendoza Thread Starter

    Joined:
    Nov 12, 2011
    Messages:
    124
    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
    
     
  9. andythepandy

    andythepandy

    Joined:
    Jul 2, 2006
    Messages:
    367
    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
     
  10. Carlos_Mendoza

    Carlos_Mendoza Thread Starter

    Joined:
    Nov 12, 2011
    Messages:
    124
    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
     
  11. andythepandy

    andythepandy

    Joined:
    Jul 2, 2006
    Messages:
    367
    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:
    %-[COLOR="Red"]28[/COLOR]s
    
     
  12. Carlos_Mendoza

    Carlos_Mendoza Thread Starter

    Joined:
    Nov 12, 2011
    Messages:
    124
    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);
    
     
  13. andythepandy

    andythepandy

    Joined:
    Jul 2, 2006
    Messages:
    367
    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
     
  14. Carlos_Mendoza

    Carlos_Mendoza Thread Starter

    Joined:
    Nov 12, 2011
    Messages:
    124
    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!
     
  15. Carlos_Mendoza

    Carlos_Mendoza Thread Starter

    Joined:
    Nov 12, 2011
    Messages:
    124
    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?
     
  16. Sponsor

As Seen On
As Seen On...

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.

Join over 733,556 other people just like you!

Loading...
Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/1051819