954,549 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Delimiter format error

Hi!

I use scanners delimiter. The next() function for scanner should return me the string by these punctuators:

|
.
:
space

But i don't know what to write inside scanner.useDelimiter("") parameter?

For example for this string "abc.123,zqw ttt|AAA",

scanner.next() should return abc
scanner.next() should return 123
scanner.next() should return zqw
scanner.next() should return ttt
scanner.next() should return AAA

Can somebody help me please?

Thank you!

servet
Newbie Poster
5 posts since Feb 2012
Reputation Points: 10
Solved Threads: 0
 

Hi!

I use scanners delimiter. The next() function for scanner should return me the string by these punctuators:

| . : space

But i don't know what to write inside scanner.useDelimiter("") parameter?

For example for this string "abc.123,zqw ttt|AAA",

scanner.next() should return abc scanner.next() should return 123 scanner.next() should return zqw scanner.next() should return ttt scanner.next() should return AAA

Can somebody help me please?

Thank you!


check here: http://www.java2s.com/Tutorial/Java/0180__File/SettingDelimitersforScanner.htm and here: http://forum.codecall.net/java-help/14956-using-delimiters-scanner.html

DavidKroukamp
Practically a Master Poster
Team Colleague
693 posts since Dec 2011
Reputation Points: 282
Solved Threads: 169
 

cOrRuPtG3n3t!x Thank you. But i had already research on many sites.But i could not do it..

servet
Newbie Poster
5 posts since Feb 2012
Reputation Points: 10
Solved Threads: 0
 

Then could you post your code so we may see how you did it

zeroliken
Veteran Poster
1,106 posts since Nov 2011
Reputation Points: 201
Solved Threads: 162
 

zeroliken i try to send these strings to delimiter

[: . |]

: . |

:,.|

|:|.||

But they does not work. I think the space, .(dot), | and : have it's own special character like endline \n

servet
Newbie Poster
5 posts since Feb 2012
Reputation Points: 10
Solved Threads: 0
 

like what I said my last post
Then could you post your code so we may see how you did it
we'll find out whats wrong if youpost your code
| and : have it's own special character like endline
not really I was able to pull it off without a special character

zeroliken
Veteran Poster
1,106 posts since Nov 2011
Reputation Points: 201
Solved Threads: 162
 

I am reading a log files line which is saved before with this function:

fileAccess.writeBytes(type + " | " + DateNow() + " | " + log_message + "\r\n");

example log file line:

info | 47:17:13 12.1.2012 | Aplication started. Version: alpha 1

line = fileAccess.readLine();
				System.out.println(line);
				scanner = new Scanner(line);
				scanner.useDelimiter("| . :");//what to write here!
				System.out.println(scanner.next());/**type*/
				System.out.println(scanner.next());/** | */
				System.out.println(scanner.next());/**seconds*/
				System.out.println(scanner.next());/** : */
				System.out.println(scanner.next());/**minutes*/
				System.out.println(scanner.next());/** : */
				System.out.println(scanner.next());/**hours*/
				System.out.println(scanner.next());/**  */
				logDate =  Integer.parseInt( scanner.next() );
				System.out.println(scanner.next());/** . */
				logMonth = Integer.parseInt( scanner.next() );
				System.out.println(scanner.next());/** . */
				logYear = Integer.parseInt( scanner.next() );
servet
Newbie Poster
5 posts since Feb 2012
Reputation Points: 10
Solved Threads: 0
 

The regex is [.:|] (ie any of the three chars in the []) EXCEPT that . and | are special chars in regex and need to be escaped with a \ to indicate that you want to use them literally, ie [\.:\|]
Next problem is that \ is a special character in Java string literals, and needs to be escaped with an extra \ you want to use it literally. This means that the Java string you need is "[\\.:\\|]"

JamesCherrill
Posting Genius
Moderator
6,373 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
 

and what do each those println show?

Edit: Anyway I think JamesCherrill's reply might suit best for your needs, you should have said that you were reading a file in the first post so I could have posted a more concise reply :)

zeroliken
Veteran Poster
1,106 posts since Nov 2011
Reputation Points: 201
Solved Threads: 162
 

JamesCherrill's answr is good. i also use string.split("") fucntion to split the words with space. İ don't know why it works. but it works :D

Thank you all! :)

servet
Newbie Poster
5 posts since Feb 2012
Reputation Points: 10
Solved Threads: 0
 

Please mark this "solved" for the archives. Thanks.

JamesCherrill
Posting Genius
Moderator
6,373 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You