basic reading input from user in java -
i want read character , store char[]
array , here 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++; }
and fgetc
method:
public static int fgetc(inputstream stream)
and method should returns character input stream.
but keep getting error message when compile:
error: possible loss of precision
message[index] = fgetc(system.in); ^ required: char found: int
what should put inside fgetc
can collect input user??
your code expecting char
, return int
here:
public static int fgetc(inputstream stream) // ↑ tells method return int
you can
change method signature return
char
.public static char fgetc(inputstream stream) // ↑ tells method return char
cast returned value
char
casting conversion (§5.5) converts type of expression type explicitly specified cast operator (§15.16).
message[index] = (char) fgetc(system.in); // ↑ cast returning value char
Comments
Post a Comment