[英]Reading a string in Character array without using any String function, not even charAt in Java
I want to read a String in a char array without using any of String class function, not even charAt(), lenght() in Java. 我想在不使用任何String类函数的情况下读取char数组中的String,甚至不使用Java中的charAt(),lenght()。 I know one method char c=(char) System.in.read(). 我知道一种方法char c =(char)System.in.read()。 But in this, the user char write a whole sentence on command Line even for inputting a single char. 但是在这种情况下,用户char即使在输入单个char的情况下,也会在命令行上写下整个句子。 I want that user will be able to type one character and then i can do some operations on it, then the next character on the same line....(as we do in c++) . 我希望该用户将能够键入一个字符,然后我可以对此进行一些操作,然后在同一行上进行下一个字符....(就像我们在c ++中所做的那样)。 Please give me some suggetstions. 请给我一些建议。 I want a code working similar like in c++ 我想要类似C ++的代码
// c++ code
char c, d[50];
cin.get(c);
int i=0;
while(c != '\n')
{
d[i++]=c;
... //any expressions
...
cin.get(c);
}
There's no easy way to handle one character at a time. 一次处理一个字符没有简单的方法。
See on SO this and this . 如此看这个和这个 。 You may work around this problem with some headaches and on particular OSes and no data from System.in
but rather from your own structures. 您可能会遇到一些令人头疼的问题,并且在特定的OS上可以解决此问题, 而不是来自System.in
数据,而是来自您自己的结构。 If you find a way to flush System.in
at each keystroke, your problem is solved. 如果您找到在每次击键时都刷新System.in
的方法,则可以解决您的问题。 As far as I know, you can't register a keyboard IRQ from Java. 据我所知,您不能从Java注册键盘IRQ。
Your no- String
requirement is the easy part, as you may use the same structure as you would in C++. 没有String
要求很容易,因为您可以使用与C ++相同的结构。 The problem is the mechanism behind cin.get()
which has no analogue in Java. 问题是cin.get()
背后的机制,在Java中没有类似机制。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.