United StatesChange Country, Oracle Worldwide Web Sites Communities I am a... I want to...
JDK-4993740 : Scanner should implement Iterable<String>

Details
Type:
Enhancement
Submit Date:
2004-02-12
Status:
Resolved
Updated Date:
2016-01-12
Project Name:
JDK
Resolved Date:
2016-01-12
Component:
core-libs
OS:
generic,windows_xp
Sub-Component:
java.util
CPU:
generic,x86
Priority:
P3
Resolution:
Won't Fix
Affected Versions:
5.0,6
Fixed Versions:

Related Reports

Sub Tasks

Description
Name: rmT116609			Date: 02/12/2004


A DESCRIPTION OF THE REQUEST :
Scanner could act as an Iterator of String but not as an Iterable of String.

Scanner not implements Iterable<String>  and thus is not compatible with foreach (enhancement 'for' loop) to iterate over a stream (i.e. a Readable).


JUSTIFICATION :
I think this feature could simply improve simple text parsing in order to implements by example word count, word filtering etc...

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
Scanner should implement Iterate<String>
ACTUAL -
Scanner doesn't implement Iterate<String>

---------- BEGIN SOURCE ----------
public class TestScanner {
  public static void main(String[] args) {
    final Scanner scanner=Scanner.create(System.in);
    
    for(String text:scanner)
      out.printf("%s\n",text);
  }
}
---------- END SOURCE ----------

CUSTOMER SUBMITTED WORKAROUND :
use an intermediary anonymous class like this :

public class TestScanner {
	public static void main(String[] args) {
	  final Scanner scanner=Scanner.create(System.in);
    
    Iterable<String> iterable=new Iterable<String>() {
      public Iterator<String> iterator() {
        return scanner;
      }
    };
    
    for(String text:iterable)
      out.printf("%s\n",text);
	}
}
(Incident Review ID: 238515) 
======================================================================
###@###.### 11/1/04 20:38 GMT

                                    

Comments
j.u.Scanner now has added Stream<String> tokens() in jdk9
                                     
2016-01-12
EVALUATION

Contribution-Forum:https://jdk-collaboration.dev.java.net/servlets/ProjectForumMessageView?messageID=12006&forumID=1463
                                     
2006-03-15



Hardware and Software, Engineered to Work Together