[英]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.我正在尝试解决 HackerEarth 中的一个问题。 And I'm using javascript. Below is my code.
我正在使用 javascript。下面是我的代码。
// 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.每当我尝试将输入字符串转换为 integer 时,我都会收到上述错误。 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);我试过 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/这个问题好像是参考HackerEarth的基本I/O教程问的: 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()
.请记住,
input
包含整个字符串——而不是像使用 C 的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.但是随后您只是将它们转换为
int
并加入它们并尝试打印它们。
The problem with that is that "helloworld" is not an integer. If you try to parse it as one, it'll give out NaN
.问题在于“helloworld”不是 integer。如果您尝试将其解析为一个,它会给出
NaN
。 Adding that to the first 10
will also get NaN
and you will have a problem.将其添加到前
10
也会得到NaN
,您将遇到问题。
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.我们只解析应该解析的部分,把output转回字符串再打印出来。
Note: the .toString()
on line 3 is optional - since when we do concatenation of a number and a string, the output is a string.注意:第 3 行的
.toString()
是可选的 - 因为当我们连接数字和字符串时,output 是一个字符串。 I converted it to a string just to be safe, but it's up to you if you want to do it or not.为了安全起见,我将其转换为字符串,但是否要这样做取决于您。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.