| Author |
Need help for a doubt..Using BufferedReader.
|
Ishani Mehta
Greenhorn
Joined: Oct 16, 2006
Posts: 17
|
|
Hello,
Can anyone please help me in this? I am doing some practice problems. Suppose user enters the value like this:
2
1 7
2 8
//null line
and from this line onwards, i want to display output.
I have store value of 2 using, BufferedReader br =new BufferedReader(new InputStreamReader(System.in));
String[] input=br.readLine().split(" ");
int n1=Integer.parseInt(input[index]);
int n2=Integer.parseInt(input[index+1]);
//index=0;
index=index+2;
I want to put here a condition that when that null line comes, all the above lines before that null and after that first line (2 is written over there), I want to put all those values in an array. want to put a condition for that. if i write br.readline()!=null then it doesnt work. can you please tell me whats that condition should be inserted before n1's initialization??
Thanks
|
 |
Swastik Dey
Ranch Hand
Joined: Jan 08, 2009
Posts: 1196
|
|
Even if the user does not enter anything , but after pressing enter the input does not remain null, but the length of the input is 0, so probably you need to check for the length
|
Swastik
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19216
|
|
Swastik Dey wrote:
Please change that into If you don't you will get a certain NullPointerException if all lines have been read.
|
SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
|
 |
Swastik Dey
Ranch Hand
Joined: Jan 08, 2009
Posts: 1196
|
|
Thanks,
I should have included that == null check too.
|
 |
Ishani Mehta
Greenhorn
Joined: Oct 16, 2006
Posts: 17
|
|
Thanks for your replies.
But the problem is:
2 // No. of lines
1 6 //diff. values.
5 9
//Empty Line
7 // Sum of 1 and 6
14 // 5+9
So first I want to store 2 in one variable
then 1 and 6 in n1[0] and n1[1]
after that 5 and 9 in n1[2] and n1[3]
after that an empty line
and from line 5 onwards, output should be displayed in separate lines.
I have used while(true){ ...... //code........} but it goes in continuous loop.
|
 |
Ishani Mehta
Greenhorn
Joined: Oct 16, 2006
Posts: 17
|
|
Sorry..
--------------------------------------------
then 1 and 6 in n1[0] and n1[1]
after that 5 and 9 in n1[2] and n1[3]
---------------------------------------------
here input[0] and input[1] instead of n1..
|
 |
Swastik Dey
Ranch Hand
Joined: Jan 08, 2009
Posts: 1196
|
|
So probably you need two conditions here, either the maximum number of lines have been entered by the user or the users enters a blank line in between
Let us know if I am getting wrong
|
 |
Ishani Mehta
Greenhorn
Joined: Oct 16, 2006
Posts: 17
|
|
Thank you very much.
I have used two dimensional array..
Its working fine now.
Thanks
|
 |
 |
|
|
subject: Need help for a doubt..Using BufferedReader.
|
|
|