It’snoteasyfornewcomertolearnjava-application’siostream.
NowfromwhatIlearn,IshouldcreataReadertoreadwhattheusertype,thencreateaBufferedReadertostorethekeywords.AfterdoingthatIcancreateStringtoreadLine().
Hereisthemethods:
publicstaticStringinputLine(){
BufferedReaderbr=null;
Readerr=newInputStreamReader(System.in);
br=newBufferedReader(r);
Stringstr="";
try{
str=br.readLine();
}catch(IOExceptione){
e.printStackTrace();
}
returnstr;
}
Bytheway,WhenIwanttogetanint,Ishouleconvertthestring.
Integer.parseInt(inputLine());
本文介绍了一个简单的Java程序示例,展示了如何使用InputStreamReader和BufferedReader从标准输入读取一行文本,并将其转换为整数。这对于初学者理解Java的基本输入输出操作非常有帮助。

被折叠的 条评论
为什么被折叠?



