本文介绍了用户在Java中的基本阅读输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想读取一个字符并将其存储到char[]
数组中,这是我的方法,称为getaline
I want to read a character and store it into the char[]
array and here is my method called getaline
public static int getaline(char message[], int maxlength)
{
int index = 0;
while (message[index] != '\n')
{
message[index] = fgetc(System.out);
index++;
}
index++;
}
和我的fgetc
方法:
public static int fgetc(InputStream stream)
,此方法应从输入流中返回一个字符.
and this method should returns a character from the input stream.
但是我在编译时总是收到错误消息:
But i keep getting an error message when i compile:
message[index] = fgetc(System.in);
^
required: char
found: int
我应该在fgetc
里面放些什么,以便我可以从用户那里收集输入信息?
what should i put inside fgetc
so that i can collect input from the user??
推荐答案
您的代码期望使用char
,但是您在此处返回int
:
Your code is expecting a char
, but you return an int
here:
public static int fgetc(InputStream stream)
// ↑ tells method will return an int
您可以
-
更改方法签名以返回
char
.
public static char fgetc(InputStream stream)
// ↑ tells method will return a char
将值返回到char
message[index] = (char) fgetc(System.in);
// ↑ cast returning value to a char
这篇关于用户在Java中的基本阅读输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!