java - method that returns char with int -
can explain method me , why returning char
instead of int
??
public static int fgetc(inputstream stream) { char toret = '\0'; if (instream.isempty()) { try { toret = (char) stream.read(); if (toret == cr) toret = (char) stream.read(); if ((int) toret == 0xffff) return eof; } catch (eofexception eof) { return eof; } catch (ioexception ioe) { writeline ("unexpected io exception caught!\n", system.out); writeline (ioe.tostring(),system.out); } } else toret = ((mylibcharacter) instream.pop ()).charvalue(); return toret; }
and lets want store value user input character until newline detected,
int index = 0; while (message[index] != '\n\) { message[index] = fgetc(system.in); index++; }
why wrong?? tips , appreciated.
sorry might little messy, feel free edit or ask me questions regarding post.
char
(and other primitive types) in java
int
's.
a narrowing primitive conversion may used if type of variable byte, short, or char, , value of constant expression representable in type of variable.
so in my previous answer your previous question, must cast or change return type, because:
char c = 'a' + 1; char c = 45;
or
char c = 'a'; c = c++;
Comments
Post a Comment