Results 1 to 6 of 6
Thread: Find \t \n in text
- 05-11-2012, 09:58 AM #1
Member
- Join Date
- Apr 2012
- Posts
- 9
- Rep Power
- 0
Find \t \n in text
Hi! So I have gotten a string from a JTextPane using
Now my problem is in tokenizing it.Java Code:int length = textPane.getDocument().getLength(); text = textPane.getDocument().getText(0, length);
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?
This will output: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; }
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.
- 05-11-2012, 10:27 AM #2
AN21XX
- Join Date
- Mar 2012
- Location
- Munich
- Posts
- 297
- Rep Power
- 2
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:
EDIT: My suggestion is you change line 33 to a pure 'else' statement and in that one you include the switch.Java Code:switch(c) { case '\n': // etc.... break; }Last edited by Sierra; 05-11-2012 at 10:31 AM.
I like likes!.gif)
- 05-11-2012, 10:27 AM #3
Moderator
- Join Date
- Apr 2009
- Posts
- 10,460
- Rep Power
- 16
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:
which would check for a tab, rather than:Java Code:if (char[i] == '\n') ...
which would check for a '\' followed by the letter 'n'.Java Code:if (char[i] == '\\') if (char[i+1] == 'n')Please do not ask for code as refusal often offends.
- 05-11-2012, 10:28 AM #4
Moderator
- Join Date
- Apr 2009
- Posts
- 10,460
- Rep Power
- 16
Re: Find \t \n in text
Bah!
Slow, aged typing fingers!Please do not ask for code as refusal often offends.
- 05-11-2012, 10:30 AM #5
AN21XX
- Join Date
- Mar 2012
- Location
- Munich
- Posts
- 297
- Rep Power
- 2
Re: Find \t \n in text
Don't talk about age... :)
I like likes!.gif)
- 05-11-2012, 10:33 AM #6
Member
- Join Date
- Apr 2012
- Posts
- 9
- Rep Power
- 0
Similar Threads
-
WebEngine find text
By PhQ in forum JavaFXReplies: 6Last Post: 09-15-2011, 08:46 PM -
Searching through folder to find text inside all files
By dazzabiggs in forum New To JavaReplies: 3Last Post: 05-03-2011, 01:20 PM -
How to find specific text from .txt file
By krechlich in forum New To JavaReplies: 11Last Post: 03-18-2011, 06:57 AM -
Find and Replace in Text File
By hamidsharifi in forum New To JavaReplies: 2Last Post: 02-13-2011, 02:01 AM -
find and replace text from a text file
By gezzel in forum New To JavaReplies: 2Last Post: 09-19-2008, 04:04 PM


2Likes
LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks