JDK-4004086 : java.io: Add support for simple input parsing
  • Type: Enhancement
  • Component: core-libs
  • Sub-Component: java.io
  • Affected Version: 1.1,1.1.7
  • Priority: P5
  • Status: Closed
  • Resolution: Duplicate
  • OS: solaris_2.5,windows_98
  • CPU: generic,x86
  • Submitted: 1996-09-16
  • Updated: 2000-03-10
  • Resolved: 2000-03-10
Related Reports
Duplicate :  
Relates :  
Relates :  
Description
There needs to be a class in java.io that can take ASCII/Unicode input from
the keyboard for all the primitive types.

Currently the only built-in support is for binary data.  That is almost
never what is wanted for terminal I/O.

The class should be called something like PrintInputStream and it would
read character date and assemble it into values of the primitive types.
Currently, programmers are obliged to do this by hand, forming the worst
possible impression on those new to the language.  Even Pascal has better
support than this.  Even C has better support than this.  Even C++ has better
support than this.   

This is how we must currently read an integer from System.in

        import java.util.*;
        static DataInputStream dis = new DataInputStream( System.in );
        static StringTokenizer st;

    try {
        st=new StringTokenizer(dis.readLine());
        int myint = Integer.parseInt(st.nextToken());


We have to offer something a bit better!

peter.vanderlinden@Eng 1996-09-15

Comments
SUGGESTED FIX /* * @(#)PrintInputStream.java 1.0 96/9/18 Peter van der Linden * * Copyright (c) 1996 Sun Microsystems, Inc. All Rights Reserved. * * Permission to use, copy, modify, and distribute this software * and its documentation for NON-COMMERCIAL purposes and without * fee is hereby granted provided that this copyright notice * appears in all copies. Please refer to the file "copyright.html" * for further important copyright and licensing information. * * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. */ import java.util.*; import java.io.*; // take this out when you put it in the io package. //package java.io; /** * An input stream that lets you read primitive Java data types * as ASCII or Unicode values from a stream in a portable way. * This class is intended for interactive I/O, and the * readln*() routines expect each value to be followed by a newline, * as you enter it from the terminal. * There are also read*() routines for each of the primitive types * that do not need a terminating newline. These are not yet * implemented and just throw an IOException when called. * * Use this class PrintInputStream to get character values * so the int value 33 would be represented by "33". * * Use the class DataInputStream to get binary values * so the int value 33 would be represented by 0x0021 * * @see DataInputStream * @see PrintStream * @version 1.0, 18 Sept 1996 * @author Peter van der Linden */ public class PrintInputStream extends FilterInputStream implements DataInput { private DataInputStream dis; /** * Creates a new PrintInputStream. * @param in the input stream */ public PrintInputStream(InputStream in) { super(in); dis = new DataInputStream(in); } /** * Reads data into an array of bytes. * This method blocks until some input is available. * @param b the buffer into which the data is read * @return the actual number of bytes read, -1 is * returned when the end of the stream is reached. * @exception IOException If an I/O error has occurred. */ public final int read(byte b[]) throws IOException { return dis.read(b); } /** * Reads data into an array of bytes. * This method blocks until some input is available. * @param b the buffer into which the data is read * @param off the start offset of the data * @param len the maximum number of bytes read * @return the actual number of bytes read, -1 is * returned when the end of the stream is reached. * @exception IOException If an I/O error has occurred. */ public final int read(byte b[], int off, int len) throws IOException { return dis.read(b, off, len); } /** * Reads bytes, blocking until all bytes are read. * @param b the buffer into which the data is read * @exception IOException If an I/O error has occurred. * @exception EOFException If EOF reached before all bytes are read. */ public final void readFully(byte b[]) throws IOException { dis.readFully(b, 0, b.length); } /** * Reads bytes, blocking until all bytes are read. * @param b the buffer into which the data is read * @param off the start offset of the data * @param len the maximum number of bytes read * @exception IOException If an I/O error has occurred. * @exception EOFException If EOF reached before all bytes are read. */ public final void readFully(byte b[], int off, int len) throws IOException { dis.readFully(b, off, len); } /** * Skips bytes, blocks until all bytes are skipped. * @param n the number of bytes to be skipped * @return the actual number of bytes skipped. * @exception IOException If an I/O error has occurred. */ public final int skipBytes(int n) throws IOException { return dis.skipBytes(n); } StringTokenizer st; /** * Reads a boolean. * @return the boolean read. */ public final boolean readBoolean() throws IOException { throw new IOException(); } /** * Reads a boolean, discarding the rest of the line. * @return the boolean read. */ public final boolean readlnBoolean() throws IOException { st=new StringTokenizer(dis.readLine()); boolean bo = new Boolean(st.nextToken()).booleanValue(); return bo; } /** * Reads an 8 bit byte. * @return the 8 bit byte read. */ public final byte readByte() throws IOException { return dis.readByte(); } /** * Reads an unsigned 8 bit byte. * @return the 8 bit byte read. */ public final int readUnsignedByte() throws IOException { return dis.readUnsignedByte(); } /** * Reads a 16 bit short. * @return the 16 bit short read. */ public final short readShort() throws IOException { throw new IOException(); } /** * Reads a 16 bit short, discarding the rest of the line. * @return the 16 bit short read. */ public final short readlnShort() throws IOException { st=new StringTokenizer(dis.readLine()); int i = Integer.parseInt(st.nextToken()); if (i<-32768 || i>32767) throw new IOException(); return (short)i; } /** * Reads 16 bit short. * @return the 16 bit short read. */ public final int readUnsignedShort() throws IOException { throw new IOException(); } /** * Reads 16 bit short, discarding the rest of the line * @return the 16 bit short read. */ public final int readlnUnsignedShort() throws IOException { st=new StringTokenizer(dis.readLine()); int i = Integer.parseInt(st.nextToken()); if (i<0 || i>65535) throw new IOException(); return i; } /** * Reads a 16 bit char. * @return the read 16 bit char. */ public final char readChar() throws IOException { throw new IOException(); } /** * Reads a 16 bit char, discarding the rest of the line. * @return the read 16 bit char. */ public final char readlnChar() throws IOException { return dis.readLine().charAt(0); } /** * Reads a 32 bit int. * @return the 32 bit integer read. */ public final int readInt() throws IOException { throw new IOException(); } /** * Reads a 32 bit int, discarding the rest of the line. * @return the 32 bit integer read. */ public final int readlnInt() throws IOException { st=new StringTokenizer(dis.readLine()); return Integer.parseInt(st.nextToken()); } /** * Reads a 64 bit long. * @return the 64 bit long read. */ public final long readLong() throws IOException { throw new IOException(); } /** * Reads a 64 bit long, discarding the rest of the line. * @return the 64 bit long read. */ public final long readlnLong() throws IOException { st=new StringTokenizer(dis.readLine()); return Long.parseLong(st.nextToken()); } /** * Reads a 64 bit double. * @return the 64 bit double read. */ public final double readDouble() throws IOException { throw new IOException(); } /** * Reads a 64 bit double, discarding the rest of the line. * @return the 64 bit double read. */ public final double readlnDouble() throws IOException { st=new StringTokenizer(dis.readLine()); return new Double( st.nextToken() ).doubleValue(); } /** * Reads a 32 bit float. * @return the read 32 bit float. */ public final float readFloat() throws IOException { throw new IOException(); } /** * Reads a 32 bit float, discarding the rest of the line. * @return the read 32 bit float. */ public final float readlnFloat() throws IOException { st=new StringTokenizer(dis.readLine()); return new Float( st.nextToken() ).floatValue(); } /** * Reads a String terminated by a newline * a synonym for readLine(). * @return the read String (sans the newline) */ public final String readString() throws IOException { return readLine(); } /** * Reads a String terminated by a newline * a synonym for readLine(). * @return the read String (sans the newline) */ public final String readlnString() throws IOException { return readLine(); } private char lineBuffer[]; /** * Reads in a line that has been terminated by a \n, \r, * \r\n or EOF. * @return a String copy of the line. */ public final String readLine() throws IOException { return dis.readLine(); } /** * Reads a UTF format String. * @return the String. */ public final String readUTF() throws IOException { return readUTF(this); } /** * Reads a UTF format String from the given input stream. * @return the String. */ public final static String readUTF(DataInput in) throws IOException { return DataInputStream.readUTF(in); } }
11-06-2004

EVALUATION To be addressed in the New I/O work. -- mr@eng 2000/3/9
03-08-0175