Setting Delimiters for Scanner : Scanner « File « Java Tutorial

Java Tutorial
1. Language
2. Data Type
3. Operators
4. Statement Control
5. Class Definition
6. Development
7. Reflection
8. Regular Expressions
9. Collections
10. Thread
11. File
12. Generics
13. I18N
14. Swing
15. Swing Event
16. 2D Graphics
17. SWT
18. SWT 2D Graphics
19. Network
20. Database
21. Hibernate
22. JPA
23. JSP
24. JSTL
25. Servlet
26. Web Services SOA
27. EJB3
28. Spring
29. PDF
30. Email
31. J2ME
32. J2EE Application
33. XML
34. Design Pattern
35. Log
36. Security
37. Apache Common
38. Ant
39. JUnit
Java
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Tutorial » File » Scanner 
11. 54. 7. Setting Delimiters for Scanner

Scanner defines where a token starts and ends based on a set of delimiters.

The default delimiters are the whitespace characters.

To Change the delimiters

  1. Scanner: useDelimiter(String pattern)
  2. Scanner: useDelimiter(Pattern pattern)
  3. Pattern is a regular expression that specifies the delimiter set.

Use Scanner to compute the average of a list of comma-separated values.

import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;

public class MainClass {
  public static void main(String args[]) throws IOException {

    FileWriter fout = new FileWriter("test.txt");
    fout.write("2, 3.4,    5,6, 7.4, 9.1, 10.5, done");
    fout.close();

    FileReader fin = new FileReader("Test.txt");
    Scanner src = new Scanner(fin);
    // Set delimiters to space and comma.
    // ", *" tells Scanner to match a comma and zero or more spaces as
    // delimiters.

    src.useDelimiter(", *");

    // Read and sum numbers.
    while (src.hasNext()) {
      if (src.hasNextDouble()) {
        System.out.println(src.nextDouble());
      else {
        break;
      }
    }
    fin.close();
  }
}
2.0
3.4
5.0
6.0
7.4
9.1
10.5
11. 54. Scanner
11. 54. 1. Using Scanner to receive user input
11. 54. 2. Using Scanner: the complement of Formatter
11. 54. 3. In general, to use Scanner, follow this procedure
11. 54. 4. Creating a Scanner: read from standard input: Scanner conin = new Scanner(System.in)
11. 54. 5. Creating a Scanner to read from a string
11. 54. 6. Using Scanner to read several different unknown types of data
11. 54. 7. Setting Delimiters for Scanner
11. 54. 8. To obtain the current delimiter pattern: Pattern delimiter( )
11. 54. 9. Searching for the specified pattern within the next line of text
11. 54. 10. To find within the next count characters
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.