简体   繁体   中英

TypeError [ERR_INVALID_ARG_TYPE]: The "chunk" argument must be one of type string or Buffer. Received type number

I'm trying to solve one problem in HackerEarth. And I'm using javascript. Below is my code.

// Sample code to perform I/O:

process.stdin.resume();
process.stdin.setEncoding("utf-8");
var stdin_input = "";

process.stdin.on("data", function (input) {
    stdin_input += input;                               // Reading input from STDIN
});

process.stdin.on("end", function () {
   main(stdin_input);
});



// Warning: Printing unwanted or ill-formatted data to output will cause the test cases to fail


// Write your code here
function main(input) {
var data = input.split('\n');
var a = parseInt(data[0]);
var b = parseInt(data[1]);

var sum = a + b;
process.stdout.write(sum);


}

And whenever I'm compiling this I'm getting the below error

Execution failed.

Stack Trace:
events.js:174
throw er; // Unhandled 'error' event
^

TypeError [ERR_INVALID_ARG_TYPE]: The "chunk" argument must be one of type string or Buffer. Received type number
at validChunk (_stream_writable.js:258:10)
at SyncWriteStream.Writable.write (_stream_writable.js:292:21)
at main (/hackerearth/JAVASCRIPT_NODE_3789_1f3d_d89e_d4f9/s_d3fb_2843_f025_60ef.njs:28:16)
at ReadStream.<anonymous> (/hackerearth/JAVASCRIPT_NODE_3789_1f3d_d89e_d4f9/s_d3fb_2843_f025_60ef.njs:13:4)
at ReadStream.emit (events.js:194:15)
at endReadableNT (_stream_readable.js:1125:12)
at process._tickCallback (internal/process/next_tick.js:63:19)
Emitted 'error' event at:
at validChunk (_stream_writable.js:261:12)
at SyncWriteStream.Writable.write (_stream_writable.js:292:21)
[... lines matching original stack trace ...]
at process._tickCallback (internal/process/next_tick.js:63:19)

Whenever I'm trying to convert the input string to integer I'm getting the above error. I simply couldn't do any arithmetic operations with the input data. If any suggestions, please let me know...

I have tried process.stdout.write(""+sum); and it worked for me.

This question seems to be asked with reference to HackerEarth's basic I/O tutorial: https://www.hackerearth.com/practice/basic-programming/input-output/basics-of-input-output/tutorial/

Remember that input contains the entire string - not something like a "line by line" input like you'd get with C's scanf() .

You split the input on the basis of the newline. That's fine. But then you just converted them to int s and joined them and tried to print them.
The problem with that is that "helloworld" is not an integer. If you try to parse it as one, it'll give out NaN . Adding that to the first 10 will also get NaN and you will have a problem.

Instead, do something like this:

function main(input) {
    let inputParts = input.split('\n');
    let num = parseInt(inputParts[0]);
    inputParts[0] = (num * 2).toString(); // inputParts is now ["10", "helloworld"]
    process.stdout.write(inputParts.join('\n'));
}

We just parse the part that is supposed to be parsed, and convert the output back to string before printing it out.

Note: the .toString() on line 3 is optional - since when we do concatenation of a number and a string, the output is a string. I converted it to a string just to be safe, but it's up to you if you want to do it or not.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM