简体   繁体   English

TypeError [ERR_INVALID_ARG_TYPE]:“块”参数必须是字符串或缓冲区类型之一。 接收型号

[英]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.

相关问题 TypeError [ERR_INVALID_ARG_TYPE]:“块”参数必须是字符串类型或 Buffer 或 Uint8Array 的实例 - TypeError [ERR_INVALID_ARG_TYPE]: The "chunk" argument must be of type string or an instance of Buffer or Uint8Array TypeError [ERR_INVALID_ARG_TYPE]:“路径”参数必须是字符串类型。 收到未定义和代码:'ERR_INVALID_ARG_TYPE' - TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string. Received undefined and code: 'ERR_INVALID_ARG_TYPE' [ERR_INVALID_ARG_TYPE]:第一个参数必须是字符串类型或缓冲区的实例。 使用 admin.auth().verifyIdToken 时 - [ERR_INVALID_ARG_TYPE]: The first argument must be of type string or an instance of Buffer. when using admin.auth().verifyIdToken 类型错误 [ERR_INVALID_ARG_TYPE]:“文件”参数必须是字符串类型。 接收类型对象 - TypeError [ERR_INVALID_ARG_TYPE]: The "file" argument must be of type string. Received type object fs.FileRead -&gt; TypeError [ERR_INVALID_ARG_TYPE]:“路径”参数必须是字符串、缓冲区或 URL 类型之一。 接收类型未定义 - fs.FileRead -> TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be one of type string, Buffer, or URL. Received type undefined TypeError [ERR_INVALID_ARG_TYPE]:“数据”参数必须是字符串类型或 Buffer、TypedArray 或 DataView 的实例 - TypeError [ERR_INVALID_ARG_TYPE]: The "data" argument must be of type string or an instance of Buffer, TypedArray, or DataView TypeError [ERR_INVALID_ARG_TYPE]:“路径”参数必须是字符串类型。 收到 Object 的实例 - TypeError [ERR_INVALID_ARG_TYPE]: The “path” argument must be of type string. Received an instance of Object TypeError [ERR_INVALID_ARG_TYPE]:第一个参数必须是 string、Buffer、ArrayBuffer、Array 或类似数组的类型之一 Object - TypeError [ERR_INVALID_ARG_TYPE]: The first argument must be one of type string, Buffer, ArrayBuffer, Array, or Array-like Object Webpack 类型错误&#39;TypeError [ERR_INVALID_ARG_TYPE]:“路径”参数必须是字符串类型。 接收类型布尔值(真)&#39; - Webpack type error 'TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string. Received type boolean (true)' TypeError [ERR_INVALID_ARG_TYPE]:“原始”参数的类型必须为 Function。接收到的类型未定义 - TypeError [ERR_INVALID_ARG_TYPE]: The "original" argument must be of type Function. Received type undefined
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM