Scanner class

M

mert

Hi eveyone, :twisted:
I have a problem about Scanner class which make me really annoyed
I could not find the meaning of useDelimiter(
method.Besides
I do not understand why it is used and when
If anyone inform me , I will be happy
Thank
Mer
http://www.devplug.com
 
I

IchBin

mert said:
Hi eveyone, :twisted:
I have a problem about Scanner class which make me really annoyed.
I could not find the meaning of useDelimiter()
method.Besides,
I do not understand why it is used and when.
If anyone inform me , I will be happy.
Thanks
Mert
http://www.devplug.com

You can change the delimiter that is used to tokenize the input,
through the useDelimiter method of Scanner. You can pass in a String or
a java.util.regex.Pattern to the method. See the JavaDocs page for
Pattern for information on what patterns are appropriate. For example,
you can read the input one line at a time by using the newline character
(\n) as a delimiter. Here is a readFile() method that uses a newline
character as the delimiter:

private static void readFile(String fileName) {
try {
Scanner scanner = new Scanner(new File(fileName));
scanner.useDelimiter(System.getProperty("line.separator"));
while (scanner.hasNext()) {
System.out.println(scanner.next());
scanner.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}


--

Thanks in Advance...
IchBin, Pocono Lake, Pa, USA
http://weconsultants.servebeer.com/JHackerAppManager
__________________________________________________________________________

'If there is one, Knowledge is the "Fountain of Youth"'
-William E. Taylor, Regular Guy (1952-)
 
R

Roedy Green

Hi eveyone, :twisted:
I have a problem about Scanner class which make me really annoyed.
I could not find the meaning of useDelimiter()
method.Besides,
I do not understand why it is used and when.
If anyone inform me , I will be happy.
Thanks
Mert
http://www.devplug.com

from the javadoc

The scanner can also use delimiters other than whitespace. This
example reads several items in from a string:

String input = "1 fish 2 fish red fish blue fish";
Scanner s = new Scanner(input).useDelimiter("\\s*fish\\s*");
System.out.println(s.nextInt());
System.out.println(s.nextInt());
System.out.println(s.next());
System.out.println(s.next());
s.close();

prints the following output:

1
2
red
blue

Looks like it is much like split where you provide a pattern to
describe your delimiter, which is otherwile white space.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Void problem 1
Scanner annoyances 11
Problem with KMKfw libraries 1
School Project 1
I'm tempted to quit out of frustration 1
scanner clas 8
Exceptions 1
Scanner class and regex problem 1

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top