在读取字符数组的字符串不使用任何字符串函数,甚至没有的charAt在Java中 [英] Reading a string in Character array without using any String function, not even charAt in Java

查看:291
本文介绍了在读取字符数组的字符串不使用任何字符串函数,甚至没有的charAt在Java中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想读在字符数组的字符串不使用任何String类的功能,甚至没有的charAt(),长度()在Java中的。我知道有一个方法字符C =(char)的System.in.read()。但在这一点,用户炭写在命令行上一个完整的句子,甚至用于输入单个字符。我希望该用户将可以输入一个字符,然后我可以做一些关于它的操作,那么下一个字符放在同一行....(正如我们在C ++做)。请给我一些suggetstions。我想一个code工作在C类似像++

  // C ++ code
焦C,D [50];
cin.get(C);
INT I = 0;
而(C!='\\ n')
{
    D [我++] = C;
    ... //任何Ex pressions
    ...
    cin.get(C);
}


解决方案

有一次处理一个字符,没有简单的方法。

查看SO <一个href=\"http://stackoverflow.com/questions/4007534/why-cant-we-read-one-character-at-a-time-from-system-in\">this和<一个href=\"http://stackoverflow.com/questions/1066318/how-to-read-a-single-char-from-the-console-in-java-as-the-user-types-it\">this.你可以解决这个问题,有些头疼和特定操作系统的的无论从 System.in数据,而是从自己的结构。如果你找到一个方法来刷新 System.in 在每次击键,你的问题就解决了​​。据我所知,你无法从Java注册键盘IRQ。

您没有 - 字符串要求是比较容易的部分,因为,你会在C ++中,你可以使用相同的结构。这个问题的背后是 cin.get机制()这在Java中没有模拟。

I want to read a String in a char array without using any of String class function, not even charAt(), lenght() in Java. I know one method 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. 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++) . Please give me some suggetstions. I want a code working similar like in 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. If you find a way to flush System.in at each keystroke, your problem is solved. As far as I know, you can't register a keyboard IRQ from Java.

Your no-String requirement is the easy part, as you may use the same structure as you would in C++. The problem is the mechanism behind cin.get() which has no analogue in Java.

这篇关于在读取字符数组的字符串不使用任何字符串函数,甚至没有的charAt在Java中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆