java bufferedReader question, please help.?

2012-11-26 8:25 am
Sorry, let me rephrase my question

Java bufferedReader question, please help.?

I'm using bufferedReader in java, I want to say,

if next line is null, then print "helloworld" (but without the reader progress to the next line).

How can I do this? Thank u very much. Please provide me codes or syntax if u can.
Thanks.

回答 (2)

2012-11-26 10:04 pm
✔ 最佳答案
I think this is what you're asking for, but I don't know if System.in will ever return null while your app is running. You might get an empty string; i.e. "" if a user hits return with no data typed in. But, as you describe your problem, here's the code:

BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(System.in));

String nextLine=null;
while((line=bufferedReader.readLine())!=null)
{
// do whatever with non-null lines
}
System.out.println("hello world");
2012-11-26 4:43 pm
import java.util.Scanner;
Scanner in = new Scanner(System.in);
if (in.nextLine()==null) System.out.print("helloworld");


收錄日期: 2021-05-01 14:17:50
原文連結 [永久失效]:
https://hk.answers.yahoo.com/question/index?qid=20121126002553AARqu2S

檢視 Wayback Machine 備份