JDK-4993740 : Scanner should implement Iterable<String>
  • Type: Enhancement
  • Status: Resolved
  • Resolution: Won't Fix
  • Component: core-libs
  • Sub-Component: java.util
  • Priority: P3
  • Affected Version: 5.0,6
  • OS: generic,windows_xp
  • CPU: generic,x86
  • Submit Date: 2004-02-12
  • Updated Date: 2016-01-12
  • Resolved Date: 2016-01-12
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