Results 1 to 9 of 9
  1. #1
    Gakusei is offline Member
    Join Date
    May 2008
    Posts
    3
    Rep Power
    0

    Default [SOLVED] \t not working correctly?

    Hi everyone, I'm having a bit of trouble with "\t" for some reason.

    This is what I'm trying to output:

    Java Code:
    N    10*N    100*N    1000*N
    
    1    10      100      1000
    2    20      200      2000
    3    30      300      3000
    4    40      400      4000
    5    50      500      5000
    My code works fine except that it doesn't put the tab doesn't show up.
    Here is my code:

    Java Code:
    import javax.swing.JOptionPane;
    
    public class Ex6 {
        public static void main(String args[]) {
            String msg;
            
            msg = "N\t10*N\t100*N\t1000*N\n\n";         // \t doesn't seem to be working for some reason...
            
            for (int i = 1; i <= 5; i++) {
                msg += String.format("%d\t%d\t%d\t%d\n", i, i*10, i*100, i*1000);
            }
            
            JOptionPane.showMessageDialog(null, msg);
        }
    }
    And this is what I get:

    Java Code:
    N10*N100*N1000*N
    
    1101001000
    2202002000
    3303003000
    4404004000
    5505005000
    Does anyone happen to know what the problem is here?

  2. #2
    danielstoner's Avatar
    danielstoner is offline Senior Member
    Join Date
    Apr 2008
    Location
    Canada
    Posts
    191
    Rep Power
    12

    Default

    Actually it works but not in the GUI. It works at the console.

    Java Code:
    import javax.swing.JOptionPane;
    
    public class Ex6
    {
        public static void main(String args[])
        {
            String msg;
    
            msg = "N\t10*N\t100*N\t1000*N\n\n"; // \t doesn't seem to be working for some reason...
    
            for (int i = 1; i <= 5; i++)
            {
                msg += String.format("%d\t%d\t%d\t%d\n", i, i * 10, i * 100, i * 1000);
            }
    
            [B]System.out.println(msg);[/B]
            JOptionPane.showMessageDialog(null, msg);
        }
    }
    Daniel @ [www.littletutorials.com]
    Language is froth on the surface of thought

  3. #3
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    13

    Default

    Java Code:
    import javax.swing.JOptionPane;
    
    public class Ex6Rx {
        public static void main(String args[]) {
            String msg;
    
            msg = String.format("%11sN%9s10*N%5s100*N%3s1000*N\n",
                                "", "", "", "");
    
            for (int i = 1; i <= 5; i++) {
                msg += String.format("%12d%12d%12d%12d\n",
                                      i, i*10, i*100, i*1000);
            }
    
            JOptionPane.showMessageDialog(null, msg, "", -1);
        }
    }

  4. #4
    Gakusei is offline Member
    Join Date
    May 2008
    Posts
    3
    Rep Power
    0

    Default

    Weird that it works in the console but not in the GUI.

    Thanks for the reply, hardwired, that method worked. It also worked when I used the tab key in place of the "\t"s.

  5. #5
    sukatoa's Avatar
    sukatoa is offline Senior Member
    Join Date
    Jan 2008
    Location
    Cebu City, Philippines
    Posts
    556
    Rep Power
    12

    Default

    String.format("%d\t%d\t%d\t%d\n", i, i*10, i*100, i*1000);
    Have you tried also to put some whitespace?

    Like,
    %d \t %d \t %d \t %d\n
    Have you also test it like that?
    Last edited by sukatoa; 05-06-2008 at 03:33 PM.
    freedom exists in the world of ideas

  6. #6
    Gakusei is offline Member
    Join Date
    May 2008
    Posts
    3
    Rep Power
    0

    Default

    I just tried doing with the whitespace

    %d \t %d \t %d \t %d\n
    The spaces show up, but it seems like \t is completely ignored.

  7. #7
    Join Date
    Jan 2011
    Posts
    7
    Rep Power
    0

    Default Re: [SOLVED] \t not working correctly?

    How did you solve it?



    Quote Originally Posted by Gakusei View Post
    Hi everyone, I'm having a bit of trouble with "\t" for some reason.

    This is what I'm trying to output:

    Java Code:
    N    10*N    100*N    1000*N
    
    1    10      100      1000
    2    20      200      2000
    3    30      300      3000
    4    40      400      4000
    5    50      500      5000
    My code works fine except that it doesn't put the tab doesn't show up.
    Here is my code:

    Java Code:
    import javax.swing.JOptionPane;
    
    public class Ex6 {
        public static void main(String args[]) {
            String msg;
            
            msg = "N\t10*N\t100*N\t1000*N\n\n";         // \t doesn't seem to be working for some reason...
            
            for (int i = 1; i <= 5; i++) {
                msg += String.format("%d\t%d\t%d\t%d\n", i, i*10, i*100, i*1000);
            }
            
            JOptionPane.showMessageDialog(null, msg);
        }
    }
    And this is what I get:

    Java Code:
    N10*N100*N1000*N
    
    1101001000
    2202002000
    3303003000
    4404004000
    5505005000
    Does anyone happen to know what the problem is here?

    How did you solve it?

  8. #8
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,983
    Rep Power
    8

    Default Re: [SOLVED] \t not working correctly?

    I hope (s)he remembers after 9 years. Maybe you should open up a new post and ask a fresh question....
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  9. #9
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: [SOLVED] \t not working correctly?

    My first question would be, if you are using a GUI framework like Swing, why not simply use a JTable?

    Beside that, I think you can use HTML with a number of Swing text components, but even then you'd still have to produce some form of table, only this time in HTML...
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. requestFocusInWindow() not working
    By Iyengar in forum AWT / Swing
    Replies: 7
    Last Post: 01-02-2009, 04:44 PM
  2. DynaValidatorForm isn't working
    By Peter in forum Web Frameworks
    Replies: 3
    Last Post: 02-15-2008, 11:14 AM
  3. sendredirect not working
    By ranga in forum Java Servlet
    Replies: 0
    Last Post: 02-11-2008, 10:48 AM
  4. How Do I Embed Java Correctly To A Web Page
    By abcd in forum Java Applets
    Replies: 7
    Last Post: 01-28-2008, 07:53 AM
  5. Working With ANT
    By JavaForums in forum Eclipse
    Replies: 0
    Last Post: 04-26-2007, 08:16 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
  •