• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

InputStreams

 
Ranch Hand
Posts: 130
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I need help with this question from jetBrainsAcademy/Hyperskill.
reference: https://hyperskill.org/learn/step/9097
Question:

Read an input text from the console and print it as a sequence of bytes.

Use System.in as input stream directly. Avoid using Scanner.

Sample input: abc
Sample output: 979899

current implementation


Test input: abc
Output: 9798991000000000000000000000000000000000

Why is the length of the inputstream 4 instead of three.

Thank you for the support.
 
Marshal
Posts: 79965
396
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Where did you get 4 from? That is the length of the input, not the stream. Work out what line 6 does. It doesn't do what you think it does.
Separate your printout with spaces, so you can read the individual bytes. Print them in hexadecimal, so you can work out what they mean with an ASCII chart (this Unicode page is identical to ASCII). When you have worked out what the numbers mean you will probably see why you are getting 4.
 
Campbell Ritchie
Marshal
Posts: 79965
396
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I merged your stuff with the following thread. I hope that is okay by you.
 
Kelvin Okornoe
Ranch Hand
Posts: 130
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Help!

Note:
There's a difference between testing the program in IDEA and validating your solution by Hyperskill platform. In IDEA, you type the text and then press Enter to push the typed text into the input stream. In this case, the end-of-line symbol is also appended as the last symbol of the typed sequence of characters. Pressing Enter doesn't close the input stream. It is still opened and waits for an input. To actually close the input stream in the IDEA press Ctrl+D (Windows and Linux) or <command>+D (MacOS), which will produce an end-of-file event.
Hyperskill platform produces test input and sends end-of-file event right away. It does not append additional end-of-line symbols.



Failed test #1 of 3. Wrong answer

This is a sample test from the problem statement!

Test input:
abc
Correct output:
cba

Your code output:
                                            cba



I do not know how to avoid those spaces

test1.PNG
test output from hyerskill
test output from hyerskill
 
Sheriff
Posts: 28326
96
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Kelvin Okornoe wrote:I do not know how to avoid those spaces



Don't put them into the array in the first place.

You have this mysterious code:



It looks to me like you think the first line is going to tell you how many characters are going to be read from that Reader. But it doesn't. Put in some debugging code and see what "reader.toString()" returns.

The fact is that you can't tell how many characters will be read from a Reader before you read them all. So using an array to receive those characters is a bad idea because you have to set up the size of the array to be the (unknown) number of characters you are going to read.

 
Campbell Ritchie
Marshal
Posts: 79965
396
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:. . . You have this mysterious code . . .

. . . which looks familiar. Yes, I have seen it before. I think your two threads are so similar that I ought to merge them. Please don't ask the same question twice.

Don't use command/ctrl‑D (nor ctrl‑Z) on System.in. If you do, you will close the stream and cannot reopen it.
Is IDEA the same as IntelliJ (JetBrains)?
 
Kelvin Okornoe
Ranch Hand
Posts: 130
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Kelvin Okornoe,
I have merged your topic into this topic. I hope that helps.


Yes it helps, Thank you
 
Kelvin Okornoe
Ranch Hand
Posts: 130
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

Paul Clapham wrote:. . . You have this mysterious code . . .

. . . which looks familiar. Yes, I have seen it before. I think your two threads are so similar that I ought to merge them. Please don't ask the same question twice.

Don't use command/ctrl‑D (nor ctrl‑Z) on System.in. If you do, you will close the stream and cannot reopen it.
Is IDEA the same as IntelliJ (JetBrains)?


Yeas IDEA is the same as IntelliJ
 
Kelvin Okornoe
Ranch Hand
Posts: 130
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:

Kelvin Okornoe wrote:I do not know how to avoid those spaces



Don't put them into the array in the first place.

You have this mysterious code:



It looks to me like you think the first line is going to tell you how many characters are going to be read from that Reader. But it doesn't. Put in some debugging code and see what "reader.toString()" returns.

The fact is that you can't tell how many characters will be read from a Reader before you read them all. So using an array to receive those characters is a bad idea because you have to set up the size of the array to be the (unknown) number of characters you are going to read.


Any suggestion on how to go about it?
 
Campbell Ritchie
Marshal
Posts: 79965
396
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Start by wrapping that code in a print statement.
 
Kelvin Okornoe
Ranch Hand
Posts: 130
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Start by wrapping that code in a print statement.


It prints some weird integer value of 31, I don't know where that is coming from
 
Campbell Ritchie
Marshal
Posts: 79965
396
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is really basic stuff.

Take the length() call out of the line with the print call and see what happens. Count its letters.
 
Kelvin Okornoe
Ranch Hand
Posts: 130
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is my current implementation, and the solution was accepted.
 
Campbell Ritchie
Marshal
Posts: 79965
396
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That doesn't solve your current problem. Just because you got a tick from an automated tester doesn't mean you have an ideal solution, or even a good solution.
I would say that an exercise using System.in.read() is going to be awkward because System.in.read() is a pain in its own right. I am going to challenge you to write that app so you get cba and you don't close System.in.
 
Kelvin Okornoe
Ranch Hand
Posts: 130
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:That doesn't solve your current problem. Just because you got a tick from an automated tester doesn't mean you have an ideal solution, or even a good solution.
I would say that an exercise using System.in.read() is going to be awkward because System.in.read() is a pain in its own right. I am going to challenge you to write that app so you get cba and you don't close System.in.


okay am up for it,
any clues to begin with?
 
Campbell Ritchie
Marshal
Posts: 79965
396
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Start by finding out how you got 31.
 
Kelvin Okornoe
Ranch Hand
Posts: 130
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Start by finding out how you got 31.


hahaha, I know why I had 31 after returning abc as the input.

reader.toString() returns the string representation of the object which is

and the length of it is 31
 
Campbell Ritchie
Marshal
Posts: 79965
396
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And how do you think you can get System.in.read() to stop without closing System.in?
 
Kelvin Okornoe
Ranch Hand
Posts: 130
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:And how do you think you can get System.in.read() to stop without closing System.in?


check for -1?
 
Campbell Ritchie
Marshal
Posts: 79965
396
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You only get −1 if you close System.in.
 
Paul Clapham
Sheriff
Posts: 28326
96
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:You only get −1 if you close System.in.



Which there was some business near the beginning about pressing a control character (operating-system dependent) to do that from the outside.

Frankly it's bad enough depending on data from stdin without more ugly complications like that. It would have been better for the requirements to be more like "Take all input until the user presses Enter and print it out reversed."
 
Campbell Ritchie
Marshal
Posts: 79965
396
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:. . . some business . . . about pressing a control character . . . to [close System.in]

Yes, there was, I think. Anybody who makes somebody use System.in.read() should be force‑fed Church tea by the gallon.

. . . "Take all input until the user presses Enter . . ."

Which is one possible solution to my little challenge.
 
I've read about this kind of thing at the checkout counter. That's where I met this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic