Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8824947
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T06:45:28+00:00 2026-06-14T06:45:28+00:00

I am writing some code for a programming contest in java. The input to

  • 0

I am writing some code for a programming contest in java. The input to the program is given using stdin and output is on stdout. How are you folks testing programs that work on stdin/stdout? This is what I am thinking:

Since System.in is of type InputStream and System.out is of type PrintStream, I wrote my code in a func with this prototype:

void printAverage(InputStream in, PrintStream out)

Now, I would like to test this using junit. I would like to fake the System.in using a String and receive the output in a String.

@Test
void testPrintAverage() {

    String input="10 20 30";
    String expectedOutput="20";

    InputStream in = getInputStreamFromString(input);
    PrintStream out = getPrintStreamForString();

    printAverage(in, out);

    assertEquals(expectedOutput, out.toString());
}

What is the ‘correct’ way to implement getInputStreamFromString() and getPrintStreamForString()?

Am I making this more complicated than it needs to be?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T06:45:29+00:00Added an answer on June 14, 2026 at 6:45 am

    Try the following:

    String string = "aaa";
    InputStream stringStream = new java.io.ByteArrayInputStream(string.getBytes())
    

    stringStream is a stream that will read chars from the input string.

    OutputStream outputStream = new java.io.ByteArrayOutputStream();
    PrintStream printStream = new PrintStream(outputStream);
    // .. writes to printWriter and flush() at the end.
    String result = outputStream.toString()
    

    printStream is a PrintStream that will write to the outputStream which in turn will be able to return a string.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am writing a simple client program socket programming when compiling the code using
I'm writing some code that will take a screenshot of another application, given its'
I am writing some code to type strings using the Robot class. Everything is
I am writing some code that connects to a website, and using C#, and
Greetings all. I am writing some code using the Boost Units library and have
Folks, I've been writing some code in Scala lately to teach myself the language
I´m writing some Shell code and, for some logic of programming, I need to
I want to experiment with some D3D programming, but I hate writing code to
I am writing some code to gain practice programming Python. The code is as
I am re-writing some code for a new re-design and I am wondering if

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.