Results 1 to 8 of 8
  1. #1
    Join Date
    Jul 2008
    Location
    Phoenix, AZ
    Posts
    6
    Rep Power
    0

    Question Escape Sequence \t does not display.

    Hello all, just a quick question for anyone who might be able to help. I'm using NetBeans 6.1, and I find that I can use \n with no problem, but when I try to use the tab escape sequence \t, nothing displays. It's as if i'm not even using it at all. I have tried it in System.out.print -type lines and also in strings for message/dialog boxes, but to no avail.

    Is this a common problem? Thanks in advance for any help you can provide!

  2. #2
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    10

    Default Tabby the Cat

    Tabs are all over the place and widely used. I do not use them but just about everyone else in the world does. By 'not display' ..... where display? Did you try opening the file ??.... Display where ?
    Introduction to Programming Using Java.
    Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor

  3. #3
    Join Date
    Jul 2008
    Location
    Phoenix, AZ
    Posts
    6
    Rep Power
    0

    Default RE: Escape Sequence \t does not display

    Here is an example of a program in which I have used the escape sequence '\t' (\t is marked in red):

    /*
    SortingStudents.java
    */

    package sortingstudents; //name of program package

    import javax.swing.JOptionPane; //program uses JOptionPane

    public class SortingStudents {//begin class

    SortingStudents

    //begin of method main starts Java application
    public static void main(String[] args)
    {
    String studentNumberString =

    JOptionPane.showInputDialog(
    "Please enter the number of students:");

    int numberOfStudents = Integer.parseInt

    (studentNumberString);
    String output = "Student Name\tScore\n"
    + "--------------------\t--------\n";
    String[] name = new String[numberOfStudents];
    double[] score = new double[numberOfStudents];

    getName(name, numberOfStudents);
    getScore(name, score, numberOfStudents);
    output = sortScores(name, score, numberOfStudents,

    output);
    printResults(name, score, numberOfStudents,

    output);
    }




    public static String[] getName(String[] name, int

    numberOfStudents)
    {
    int i = 0;

    for (i = 0; i < numberOfStudents; i++)
    {
    name[i] = JOptionPane.showInputDialog("Enter

    Student #"
    + (i + 1) + "\'s Name: ");


    }

    return name;

    }

    public static double[] getScore(String[] name, double

    [] score,
    int numberOfStudents)
    {
    for (int j = 0; j < numberOfStudents; j++)
    {
    String scoreString =

    JOptionPane.showInputDialog("Enter "
    + name[j] + "\'s Score: ");
    score[j] = Double.parseDouble(scoreString);

    }
    return score;
    }

    public static String sortScores(String[] name, double

    [] score,
    int numberOfStudents,

    String output)
    {
    for (int k = numberOfStudents - 1; k >= 1; k--)
    {
    double currentMin = score[0];
    String currentNameMin = "";
    int currentMinIndex = 0;

    for (int m = 1; m <= k; m++)
    {
    if (currentMin > score[m])
    {
    currentMin = score[m];
    currentNameMin = name[m];
    currentMinIndex = m;
    }
    if (currentMinIndex != k)
    {
    score[currentMinIndex] = score[k];
    name[currentMinIndex] = name[k];
    score[k] = currentMin;
    name[k] = currentNameMin;
    }
    }

    }
    for (int q = 0; q < numberOfStudents; q++)
    {
    output += name[q] + ""\t\t\t"
    + score[q] + "\n";
    }
    return output;


    }

    public static void printResults(String[] name, double

    [] score,
    int numberOfStudents,

    String output)
    {
    JOptionPane.showMessageDialog(null, output,

    "Results",


    JOptionPane.PLAIN_MESSAGE);
    }
    }

    /* Please Note that when I run the program, where I have included \t, I get nothing at all as output.

    i.e.: String output = "Student Name\tScore\n"

    When i run the program, it looks like this in the input dialog box:

    ............................Student NameScore
    ..............................................^
    .......................................(no tab space where there should be space)
    */

    /* Also please note that I am having no trouble at all with other escape sequences such as \n. */
    Last edited by dreamingofgreen; 07-21-2008 at 02:34 AM.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    18,922
    Rep Power
    28

    Default

    tabs are a request to the presenting software to insert some blanks when a tab char is read. Not all software need honor them.
    How were you displaying the string with the tab in it?
    What do you expect to happen when you feed a tab char to a method?

    Here's what I get when I copy the output of
    System.out.print("o/p=" + output); // in sortScores before return
    to wordpad:
    o/p=Student Name Score

    The spacing for the above changes after I save it. Before saving it, there are 4-5 spaces between Name and Score. After save looks like 1.
    Last edited by Norm; 07-21-2008 at 03:50 AM.

  5. #5
    Join Date
    Jul 2008
    Location
    Phoenix, AZ
    Posts
    6
    Rep Power
    0

    Question Sorry

    I'm sorry but I don't understand your response.

    Do you know why when I run my program, it would display the \n sequences correctly but not the \t sequences?

    When you say software, do you mean my IDE? NetBeans?

    I guess I expect that when i feed a tab character sequence to a method, it will move the text over one tab space, just as it creates a new line just fine when i include "\n".

    Thanks for your reply. I appreciate your help!

  6. #6
    skaspersen is offline Member
    Join Date
    Jul 2008
    Posts
    31
    Rep Power
    0

    Default

    JOptionPane will not render tabs.
    You will need to use a JTextArea if you want the tabs to be rendered.
    If you have a GUI a JTable would be your best option

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    18,922
    Rep Power
    28

    Default

    software
    That was a generic reference to any part of any program running on a computer. From Notepad to the JOptionPane class to Netbeans, etc.

  8. #8
    Join Date
    Jul 2008
    Location
    Phoenix, AZ
    Posts
    6
    Rep Power
    0

Similar Threads

  1. Replies: 0
    Last Post: 02-14-2008, 11:00 AM
  2. Using escape sequence with like clause (%)
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-14-2008, 10:57 AM
  3. Using escape sequence with like clause (_)
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-14-2008, 10:55 AM
  4. Escape sequence problem
    By eva in forum New To Java
    Replies: 2
    Last Post: 01-21-2008, 11:15 PM
  5. Frog Escape 0.0.1 alpha
    By JavaBean in forum Java Software
    Replies: 0
    Last Post: 07-14-2007, 08:26 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
  •