Results 1 to 8 of 8

Thread: Java I/O help

  1. #1
    RobertGif is offline Member
    Join Date
    Nov 2013
    Posts
    4
    Rep Power
    0

    Default Java I/O help

    Hello! I have been using Scanners to get user input so far in my work and I have had an interest in learning to use java.IO instead. I attempted to write a simple program to take user input through System.in and then output what ever the user typed:

    Java Code:
    import java.io.*;
    
    class InputTest {
    	static BufferedInputStream in = new BufferedInputStream(System.in);
    	static BufferedOutputStream out = new BufferedOutputStream(System.out);
    	
    	public static void main(String [] args) throws IOException {
    		
    		try {
    			for(int x = 0; x <= in.available(); x++) {
    				int n = in.read();
    				out.write(n);
    			}
    		} catch (IOException e) {
    			System.err.println("It ain't workin!");
    		}
    		in.close();
    		out.close();	
    	}
    }
    Okay. While running this program, I get my desired output for input that is under 5 ints long. Example:

    input: 1
    output: 1

    input: 22
    output: 22

    input: 333
    output: 333

    input: 4444
    output: 4444

    input: 55555
    output: 5555 - only four 5

    I am confused as to why this is starting to happen. Is it a problem with the loop? Or the fact that I don't fully understand how these inputstreams work in the first place.

    Should I try making an int array, storing the input in the array in the loop, then outputting the array?

    Thanks for any help you can provide or pointing me in the right direction.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: Java I/O help

    Can you copy the contents of the command prompt window from when you execute the program that shows what is entered and what is printed?
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    RobertGif is offline Member
    Join Date
    Nov 2013
    Posts
    4
    Rep Power
    0

    Default Re: Java I/O help

    Sure, thanks for your help Norm!

    C:\Users\Robert\Desktop\CodeChef\InputTest>java InputTest
    1
    1
    C:\Users\Robert\Desktop\CodeChef\InputTest>java InputTest
    22
    22
    C:\Users\Robert\Desktop\CodeChef\InputTest>java InputTest
    333
    333
    C:\Users\Robert\Desktop\CodeChef\InputTest>java InputTest
    4444
    4444
    C:\Users\Robert\Desktop\CodeChef\InputTest>java InputTest
    55555
    5555
    C:\Users\Robert\Desktop\CodeChef\InputTest>java InputTest
    666666
    66666
    C:\Users\Robert\Desktop\CodeChef\InputTest>java InputTest
    7777777
    77777
    C:\Users\Robert\Desktop\CodeChef\InputTest>java InputTest
    88888888
    888888


    The first line is what I entered, the second is what the program is outputting

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: Java I/O help

    To see what is happening change your code by adding some println statements like this:
    Java Code:
                for(int x = 0; x <= in.available(); x++) {
                    System.out.println("1av="+in.available() + ", x="+x);
                    int n = in.read();
                    out.write(n);
                }
                System.out.println("2av="+in.available());
    The output should show you what is happening.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    RobertGif is offline Member
    Join Date
    Nov 2013
    Posts
    4
    Rep Power
    0

    Default Re: Java I/O help

    Okay, I went through I large process of figuring out how to set up a valid loop without having to call in.read() twice, the first time would be used to get in.available(). Instead I ended up using a do while() loop. Thanks for pointing me in the right direction Norm. The Eurika moment occurred when I actually figured out how out.write() worked.

    One last question, when out.close() is run, does it flush the stream? That is why it pops up as output?

    Thanks so much.

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: Java I/O help

    That is why it pops up as output?
    Sorry, I don't understand what you are asking.
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    RobertGif is offline Member
    Join Date
    Nov 2013
    Posts
    4
    Rep Power
    0

    Default Re: Java I/O help

    When you close the bufferstream, does it automatically flush the stream?

    When you flush the output stream, is that when you can see it in the command prompt?

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: Java I/O help

    Closing the stream should push everything out of the buffer.

    Flushing a stream should push everything out of the buffer. Depending on the buffer size and the amount of data written to the buffer there can be output before a flush.
    If you don't understand my response, don't ignore it, ask a question.

Posting Permissions

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