Giter Site home page Giter Site logo

anhquan's People

Contributors

anhquande avatar

anhquan's Issues

BufferedReader reading blank line should produce "" instead of null

What steps will reproduce the problem?
1. Reader reader = new StringReader("  \n\n \n size\n 30   40 ");
2. Scanner scanner = new Scanner(reader);
3. scanner.next();
4. System.out.println(scanner.next());

What is the expected output? What do you see instead?
Should output empty string "";
Actually prints null.
This is a problem because aside from being a little counter-intuitive, the 
behavior differs from the standard java.io library behavior and breaks 
compatibility with scanners and such.

The source code of BufferedReader.readLine() is:

    /**
     * Reads the next line of characters.
     * 
     * @return The next line.
     */
    public String readLine() throws IOException {
    StringBuilder sb = null;
    boolean eol = false;
    int nextChar = read();

    while (!eol && (nextChar != -1)) {
        if (nextChar == 10) {
        eol = true;
        } else if (nextChar == 13) {
        eol = true;

        // Check if there is a immediate LF following the CR
        nextChar = read();
        if (nextChar != 10) {
            this.savedNextChar = nextChar;
        }
        }

        if (!eol) {
        if (sb == null) {
            sb = new StringBuilder();
        }

        sb.append((char) nextChar);
        nextChar = read();
        }

    }
    return (sb == null) ? null : sb.toString();
    }


I believe you can fix the problem by replacing the last return statement with:
    if (sb == null) {
        if(nextChar != -1){
        return "";
        }
        return null;
    }
    return sb.toString();

Also, thanks for the useful lib. Some form of java.io should really be included 
in gwt because it's used very frequently and makes porting things to gwt a real 
pain if you don't have this library.

Original issue reported on code.google.com by [email protected] on 4 May 2012 at 10:56

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.