Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: How to end reading from console?

  1. #1
    Junior Member
    Join Date
    Nov 2013
    Thanked 0 Times in 0 Posts

    Default How to end reading from console?

    Hi, so this is my program. All is working fine expcet stoping reading from console. Problem is that even after you write done if you are fast enought you are able to write more. As you can see I can't close scanner because of two inputs. Even so I tried to close it but problem was still here.

    Any ideas how to fix it? Thank you in advance for your answers.

    public static void main(String...args) {
            System.out.println("Write first input:");
            System.out.print(streamToString(System.in, ""));
            System.out.println("Write secondinput:");
            System.out.print(streamToString(System.in, ""));
    public static String streamToString(InputStream input, String errorMessage) {
            Scanner scanner = new Scanner(input);
            StringBuilder data = new StringBuilder("");
            while (scanner.hasNext()) {
                String line = scanner.nextLine();
                if (line.equals("done")) {
                    System.out.println("Reading from console finished.");
                } else {
                    data.append(line + "\n");
            if (data.length() > 0) {
                data.deleteCharAt(data.length() - 1);
            if (data.toString().equals("")) {
                if (!errorMessage.equals("")) {
                return "";
            } else {
                return data.toString();
    Last edited by Slapy; May 21st, 2014 at 08:56 AM. Reason: little change in code

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Eastern Florida
    Thanked 2,475 Times in 2,426 Posts

    Default Re: How to end reading from console?

    Can you explain why the program should stop receiving input from the user?
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Scanner reading from console via System.in causing NoSuchElementException?
    By Pipastrolo in forum What's Wrong With My Code?
    Replies: 2
    Last Post: December 11th, 2013, 08:56 AM
  2. [SOLVED] System.Console.Writeline vs Console.Writeline
    By Techstudent in forum Java Theory & Questions
    Replies: 1
    Last Post: September 12th, 2013, 05:57 PM
  3. What is that at the end of a method
    By poetzmij in forum Java Theory & Questions
    Replies: 8
    Last Post: November 10th, 2011, 07:53 AM
  4. End Element exception reading XML file
    By treshr in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: October 5th, 2011, 12:58 AM
  5. Reading input from console (greek chars)
    By lemmyz in forum File I/O & Other I/O Streams
    Replies: 10
    Last Post: August 16th, 2010, 05:37 AM