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!

Recommended Answers

All 10 Replies

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

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

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

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

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 you post your code

| and : have it's own special character like endline

not really I was able to pull it off without a special character

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() );

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 "[\\.:\\|]"

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 :)

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! :)

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.