Results 1 to 6 of 6
  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
    10

    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
    11

    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
    10

    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.

Similar Threads

  1. requestFocusInWindow() not working
    By Iyengar in forum AWT / Swing
    Replies: 7
    Last Post: 01-02-2009, 05:44 PM
  2. DynaValidatorForm isn't working
    By Peter in forum Web Frameworks
    Replies: 3
    Last Post: 02-15-2008, 12:14 PM
  3. sendredirect not working
    By ranga in forum Java Servlet
    Replies: 0
    Last Post: 02-11-2008, 11: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, 08: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
  •