简体   繁体   English

在不使用任何String函数的情况下读取Character数组中的字符串,甚至不使用Java中的charAt

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

相关问题 我们如何在不使用任何内置函数的情况下找到Java中String的长度? (甚至不包括charAt()或toCharArray()或length()) - How can we find the length of a String in Java without using any inbuilt functions? (not even charAt() or toCharArray() or length()) Java中的字符串:charAt函数使用 - String in Java : charAt Function use 有没有办法使用循环而不使用除 charAt(0) 以外的任何函数来拆分字符串? - Is there a way to split a string using loops and without using any function other than charAt(0)? 使用charAt()在字符串中分配Java - java assignment in string by using charAt() Java:for 循环使用字符串 charAt 方法崩溃 - Java: for loop crashing using string charAt method 使用Java,这个charAt()如何; 把一个字符串变成一个int? - Using Java, how is this charAt(); turn a string into an int? 在不使用任何字符串函数的情况下在java中从字符串创建子字符串 - making a substring from string in java without using any string function 如何使用replace和charAt方法替换Java字符串中字符的多次出现 - How to replace multiple occurences of a character in a java string using replace and charAt methods Java charAt 方法返回的值甚至不是字符串的一部分 - Java charAt method returning values not even part of the string 如何在不使用Java中的替换功能的情况下替换字符串中的字符? - How to replace character in string without using the replace function in Java?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM