System.in.read(byte[])
读到byte数组中再转
读到byte数组中再转
解决方案 »
- " a.county_id = $a.county_id$".replaceAll(" *.county_id*county_id\\$","1=1")
- java中加载声音
- 刚学习JAVA不久,暑期想看多些电子书,各位介绍介绍~~
- 有个问题请教
- “Unable to write to output directory”是什么错误?
- 下例中抽象类里的抽象方法怎么能直接调用?
- 用JAVA做在线网页编辑器?
- 急急急需手机铃声转换工具 &&&不同型号手机所支持格式!!!
- 入门者小程序请教~~
- 请问如何选中Table中的某一行?
- 做了一个JDialog,如何把一个JTextarea和一个JButton填加上去?
- 谈谈对面向对象中“多态”的理解
{
public static void main(String [] args)
{
byte buf[]=new byte[1024];
String strinfo=null;
int pos=0;
int ch=0;
System.out.println("Please enter info");
while(true)
{
try
{
ch=System.in.read();
}
catch(Exception e)
{
e.printStackTrace();
}
if(ch=='\r' || ch=='\n')
break;
else
{
buf[pos++]=(byte)ch;
}
}
strinfo=new String(buf,0,pos);
System.out.println(strinfo);
}
}
byte[] b = new byte[4]; //假设最大4位数
int n = 0;
if (System.in.read(b) != -1) {
for (int i = b.length - 1; i > 0; --i) {
if (b[i] == '\n') {
n = i;
break;
}
}
if (n > 0) System.out.println("your input is " + Integer.parseInt(new String(b, 0, n)));
}
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
String inLine = bf.readLine();
int n = Integer.parseInt(inLine);就这样,不过要用try catch保护起来。当用户输入整数时,一切正常。当用户输入的格式不对时,最后一行代码会抛出异常。 你在catch块里可以进行相应处理,比如提醒用户重新输入。
打印出来的是输入了几位数字, 我想要的是得到一个int i=键盘输入
如果输入99, 则i=99.
好好看看java书吧。好像每本书都会讲的,一般在I/O那章
class ReadInt {
public ReadInt() {} public static int getInt() {
byte[] bs = new byte[20];
int length = 0;
try {
length = System.in.read(bs);
}
catch (IOException ex) {
}
if (length < 2) {
return 0;
}
try {
return Integer.parseInt(new String(bs, 0, length - 2));
}
catch (NumberFormatException ex1) {
return 0;
}
}
}