Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / January 2006

Tip: Looking for answers? Try searching our database.

Scanner class

Thread view: 
mert - 16 Jan 2006 07:37 GMT
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
IchBin - 16 Jan 2006 08:21 GMT
> Hi eveyone,  :twisted:
> I have a problem about Scanner class which make me really annoyed.
[quoted text clipped - 5 lines]
> 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();
     }
   }

Signature

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

Roedy Green - 16 Jan 2006 08:48 GMT
On Mon, 16 Jan 2006 01:37:04 -0600,
m_ozkaya@ug.bilkent.edu-dot-tr.no-spam.invalid (mert) wrote, quoted or
indirectly quoted someone who said :

>Hi eveyone,  :twisted:
>I have a problem about Scanner class which make me really annoyed.
[quoted text clipped - 5 lines]
>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.
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.