• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Need help for a doubt..Using BufferedReader.

 
Ishani Mehta
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 1625
5
Android Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

 
Rob Spoor
Sheriff
Pie
Posts: 20611
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Swastik Dey wrote:

Please change that into If you don't you will get a certain NullPointerException if all lines have been read.
 
Swastik Dey
Rancher
Posts: 1625
5
Android Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks,

I should have included that == null check too.
 
Ishani Mehta
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 1625
5
Android Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much.

I have used two dimensional array..

Its working fine now.

Thanks
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic