Results 1 to 3 of 3
  1. #1
    musdem is offline Member
    Join Date
    Mar 2012
    Posts
    8
    Rep Power
    0

    Default Input class can't handle multiple inputs

    I created an Input class in order make string inputs easier and now if I attempt to get input more than once from it it returns the error from the catch statement.
    Here's the code:
    Java Code:
    import java.io.*;
    class Read
    {
    	public static String string ()
    	{
    		String input = "";
    		InputStreamReader isr = new InputStreamReader(System.in);
    		BufferedReader buffer = new BufferedReader(isr);
    		try
    		{
    			input = buffer.readLine();
    			buffer.close();
    		}
    		catch (IOException e)
    		{
    			System.out.println("An input error has occurred");
    		}
    		return input;
    	}
    }
    I call it like this:
    Java Code:
    input = Read.string();
    Am I overlooking something simple or do I need to code this completely differently?

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,662
    Blog Entries
    7
    Rep Power
    21

    Default Re: Input class can't handle multiple inputs

    When you close your BufferedReader, it closes its wrapped Reader (the InputStreamReader); that wrapped reader closes its wrapped InputStream which is System.in; once closed it can't read anything anymore. Here it's safe not to close anything but simply forget about those readers.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    musdem is offline Member
    Join Date
    Mar 2012
    Posts
    8
    Rep Power
    0

    Default Re: Input class can't handle multiple inputs

    Thank you for your help. :)

Similar Threads

  1. How to handle multiple listeners on stateChanged
    By Cleeve in forum AWT / Swing
    Replies: 1
    Last Post: 08-13-2011, 06:24 PM
  2. multiple Data from Jsp that having same name of multiple input tage to servlet
    By rahul9323.2007@gmail.com in forum Advanced Java
    Replies: 0
    Last Post: 07-29-2011, 01:00 PM
  3. if statement with multiple inputs?
    By soc86 in forum New To Java
    Replies: 3
    Last Post: 01-20-2009, 04:44 AM
  4. Multiple line user input
    By jointhelabel in forum New To Java
    Replies: 3
    Last Post: 10-26-2008, 02:41 AM
  5. handle wrong input int/null
    By Camden in forum New To Java
    Replies: 1
    Last Post: 12-16-2007, 09:37 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •