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 9194303
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T21:22:48+00:00 2026-06-17T21:22:48+00:00

I am not understanding why the following code seems to skip the first input

  • 0

I am not understanding why the following code seems to skip the first input character.

import java.io.*;
import java.util.Arrays;

public class Input
{
    public static void main (String args[])
    {
        try {
            echo(System.in);
        }
        catch (IOException e) {
            e.printStackTrace();
        }
    }

    static void echo(InputStream stream) throws IOException
    {
        byte[] input = new byte[10];

        char[] inputChar = new char[10];
        int current;
        while ((current = stream.read()) > 0){
            stream.read(input);

            int i = 0;
            while (input[i] != 0) {
                inputChar[i] = (char)input[i];
                if (i < 9) {
                    i++;
                }
                else {
                    break;
                }
            }   
            System.out.println(Arrays.toString(inputChar));
        }
    }
}

If I give input as “1234567890” the output I get is “[2, 3, 4, 5, 6, 7, 8, 9, 0,]” The last character in inputChar seems to be blank. However, if it did not read any byte into input[9], it should be null character (ASCII 0). If I give an input which is less than 10 characters in length I get null character for the remaining positions in inputChar. So I am not sure what is going on here.

  • 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-17T21:22:49+00:00Added an answer on June 17, 2026 at 9:22 pm

    while ((current = stream.read()) > 0){

    You have your first read here, you’re not making any use of it but it’s an actual read that will advance the “position” in your stream.

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

Sidebar

Related Questions

I found the following code somewhere, but I am not understanding the code properly.
I'm having problems understanding why following code leaks in one case, and not in
The following code doesn't compile. package varargspkg; public class Main { public static void
I'm having a devil of a time understanding references. Consider the following code: class
I am having trouble understanding the following article: http://www.ibm.com/developerworks/java/library/j-jtp01255.html Under, Generics are not covariant
I have some not understanding actions from gnu clisp Suppose, I have some code
I have the following code below which seems to be working fine: // GPS
The following code seems to always follow the true branch. #include <map> #include <iostream>
I'm having trouble understanding why I get an error in the following code. I'm
I'm having trouble understanding how Exceptions are handled in TPL. The following code should

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.