File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Need help for a doubt..Using BufferedReader. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Need help for a doubt..Using BufferedReader." Watch "Need help for a doubt..Using BufferedReader." New topic
Author

Need help for a doubt..Using BufferedReader.

Ishani Mehta
Greenhorn

Joined: Oct 16, 2006
Posts: 21
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

Joined: Jan 08, 2009
Posts: 1449
    
    6

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: 19651
    
  18

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 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Swastik Dey
Rancher

Joined: Jan 08, 2009
Posts: 1449
    
    6

Thanks,

I should have included that == null check too.
Ishani Mehta
Greenhorn

Joined: Oct 16, 2006
Posts: 21
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: 21
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

Joined: Jan 08, 2009
Posts: 1449
    
    6

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: 21
Thank you very much.

I have used two dimensional array..

Its working fine now.

Thanks
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Need help for a doubt..Using BufferedReader.
 
Similar Threads
Doubt about StringTokenizer !!
travel agent java -- problem when i put the input prompt it does not go to the next promp
getting NullPointerException
why the program keep looping
passing values from one method to other