Results 1 to 6 of 6
Like Tree2Likes
  • 1 Post By Sierra
  • 1 Post By Tolls

Thread: Find \t \n in text

  1. #1
    onions is offline Member
    Join Date
    Apr 2012
    Posts
    9
    Rep Power
    0

    Default Find \t \n in text

    Hi! So I have gotten a string from a JTextPane using
    Java Code:
             int length = textPane.getDocument().getLength();
             text = textPane.getDocument().getText(0, length);
    Now my problem is in tokenizing it.

    If the text was
    "A
    sample text!
    [There is a tab here]Example."

    I would want the output to be:
    TOKEN: A
    TOKEN: \n
    TOKEN: sample
    TOKEN: text
    TOKEN: !
    TOKEN: \t
    TOKEN: Example
    TOKEN: .

    But instead, the code can't seem to read the \n and \t and they just come out blank. (They are not equal to spaces though)
    Is there a way to find these in the text?
    Java Code:
    public ArrayList tokenizeText(String text){
                ArrayList <String> sen=new ArrayList<String>();
                String word="",string="";
                char c;
                for(int i=0;i<text.length();i++){
                    c=text.charAt(i);
                    if(Character.isLetter(c)){
                        while(Character.isLetter(c) && i<text.length()){
                        word+=c;
                        c=' ';
                            if(i+1<text.length() && Character.isLetter(text.charAt(i+1))){
                                i++;
                                c=text.charAt(i);
                            } 
                        }
                       word=word.trim();
                       sen.add(word);
                       word="";
                    }
                    else if(Character.isDigit(c)){
                      while(Character.isDigit(c) && i<text.length()){
                        word+=c;
                        c=' ';
                            if(i+1<text.length() && Character.isDigit(text.charAt(i+1))){
                                i++;
                                c=text.charAt(i);
                            } 
                        }
                       word=word.trim();
                       sen.add(word);
                       word="";
                    }
                    else if(c==' '){
                        while(c==' ' && i<text.length()){
                            sen.add("SPACE");
                            word+=c;
                            c='z';
                            if(i+1<text.length() && text.charAt(i+1)==' '){
                                i++;
                                c=text.charAt(i);
                            } 
                        }
                    }
                    else if(c=='\\'){                  //This part of the loop is never entered when i test it
                        if(i+1<text.length()){
                            if(text.charAt(i+1)=='n')
                            {   i++; 
                                sen.add("NEWLINE");
                            }
                            else if(text.charAt(i+1)=='r'){
                                i++;
                                sen.add("RETURN");
                            }
                            else if(text.charAt(i+1)=='t'){
                                i++;
                                sen.add("TAB");
                            }
                        }
                        else
                            sen.add("\\");
                    }
                    else{
                        string=c+" ";
                        string=string.trim();
                        sen.add(string);
                    }
                }
               for(int i=0;i<sen.size();i++)
                        System.out.println("TOKEN: "+sen.get(i));
                return sen;
            }
    This will output:

    TOKEN: A
    TOKEN:
    TOKEN: sample
    TOKEN: SPACE
    TOKEN: text
    TOKEN: !
    TOKEN:
    TOKEN:
    TOKEN: Example
    TOKEN: .
    Last edited by onions; 05-11-2012 at 10:14 AM.

  2. #2
    Sierra is offline AN21XX
    Join Date
    Mar 2012
    Location
    Munich
    Posts
    297
    Rep Power
    3

    Default Re: Find \t \n in text

    "else if(c=='\\'){ //This part of the loop is never entered when i test it"

    Of course not, because 'c' does not contain a backslash...Not that the notation '\n' is ONE character - the backslash is used by the compiler to determine that the following character is not a normal character but a special one. You cannot check for a backslash... you need to use a switch statement:

    Java Code:
    switch(c)
    {
      case '\n': // etc....
        break;
    }
    EDIT: My suggestion is you change line 33 to a pure 'else' statement and in that one you include the switch.
    Last edited by Sierra; 05-11-2012 at 10:31 AM.
    onions likes this.
    I like likes!

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default Re: Find \t \n in text

    Those characters are not two characters (a '\' followed by a 'n' for example). They are single characters, eg by the escaped '\n'.
    So check for:
    Java Code:
    if (char[i] == '\n') ...
    which would check for a tab, rather than:
    Java Code:
    if (char[i] == '\\')
        if (char[i+1] == 'n')
    which would check for a '\' followed by the letter 'n'.
    onions likes this.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default Re: Find \t \n in text

    Bah!
    Slow, aged typing fingers!
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    Sierra is offline AN21XX
    Join Date
    Mar 2012
    Location
    Munich
    Posts
    297
    Rep Power
    3

    Default Re: Find \t \n in text

    Don't talk about age... :)
    I like likes!

  6. #6
    onions is offline Member
    Join Date
    Apr 2012
    Posts
    9
    Rep Power
    0

    Default Re: Find \t \n in text

    The switch worked perfectly. Thank you! :)

Similar Threads

  1. WebEngine find text
    By PhQ in forum JavaFX
    Replies: 6
    Last Post: 09-15-2011, 08:46 PM
  2. Searching through folder to find text inside all files
    By dazzabiggs in forum New To Java
    Replies: 3
    Last Post: 05-03-2011, 01:20 PM
  3. How to find specific text from .txt file
    By krechlich in forum New To Java
    Replies: 11
    Last Post: 03-18-2011, 06:57 AM
  4. Find and Replace in Text File
    By hamidsharifi in forum New To Java
    Replies: 2
    Last Post: 02-13-2011, 02:01 AM
  5. find and replace text from a text file
    By gezzel in forum New To Java
    Replies: 2
    Last Post: 09-19-2008, 04:04 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
  •