Stream problem or loop problem
hello friends!
i tried this program from one of the reference book
while executing with for loop, its working fine
but with do while loop, its not working fine , giving null pointer exception
plz help me
import java.io.*;
class BRReadLine
{
public static void main(String[] args) throws IOException,NullPointerException
{String str[] = new String[100];
BufferedReader br = new BufferedReader( new InputStreamReader( System.in));
System.out.println("Enter the line of text");
System.out.println("Enter stop to quit");
//int i=0;
/*do
{
str=br.readLine();
i++;
}while(str.equals("stop"));*/
for(int i=0;i<100;i++)
{
str=br.readLine();
if(str.equals("stop")) break;
}
//int i=0;
System.out.println("The line u entered:");
/*do
{
System.out.println(str);
i++;
}while(str.equals("stop"));*/
for(int i=0;i<100;i++)
{
if(str.equals("stop")) break;
System.out.println(str);
}
}
}
[1168 byte] By [
vasu_gba] at [2007-11-26 18:31:22]

That code doesn't even compile. You cannot assign a String to a String[].
The reason why you get a nullpointer is that you increment your index and then go check if the value in the array is not stop. If you enter "something" in your array you have:
01 2 3 ...
somethingnull nullnull
You increment i and call equals on str[1] which is null... Either check for !str[i++].equals("stop");
and remove the i++ from within your loop or check for !str[i-1].equals("stop");
Also notice the '!' in front of the equals test also notice that you have to use str[ i ] instead of just str...
> import java.io.*;
>
> class BRReadLine
> {
> public static void main(String[] args) throws
> s IOException,NullPointerException
> {String str[] = new String[100];
> BufferedReader br = new BufferedReader( new
> ew InputStreamReader( System.in));
> System.out.println("Enter the line of text");
> System.out.println("Enter stop to quit");
> //int i=0;
> /*do
> {
>str=br.readLine();
>i++;
U increment i here
> }while(str.equals("stop"));*/
here u print str[i ] but i is incremented now and str is null. that causes the exception
try with str[i-1]
> for(int i=0;i<100;i++)
> str=br.readLine();
> if(str.equals("stop")) break;
> }
>//int i=0;
two variables with same name in the same scope
use j or something
> m.out.println("The line u entered:");
> /*do
> {
>System.out.println(str);
>i++;
> }while(str.equals("stop"));*/
>for(int i=0;i<100;i++)
> (str.equals("stop")) break;
> System.out.println(str);
> }
> }
> }
u have done another mistakes here
while(str[i].equals("stop"))
it should be while(!str[i].equals("stop"))
This should work
public static void main(String[] args) throws IOException,NullPointerException
{ String str[] = new String[100];
BufferedReader br = new BufferedReader( new InputStreamReader( System.in));
System.out.println("Enter the line of text");
System.out.println("Enter stop to quit");
int i=0,j=0;
do
{
str[i]=br.readLine();
i++;
}while(!str[i-1].equals("stop"));
/*for(int i=0;i<100;i++)
{
str[i]=br.readLine();
if(str[i].equals("stop")) break;
}*/
System.out.println("The line u entered:");
do
{
System.out.println(str[j]);
j++;
}while(!str[j].equals("stop"));
/*for(int i=0;i<100;i++)
{
if(str[i].equals("stop")) break;
System.out.println(str);
}*/
}
first of all the code has to use the str , also the while loop checks for the array element after the index is incremented...and it will be true only if the input is "stop"...so a negation has to be added there...
import java.io.*;
class BRReadLine
{
public static void main(String[] args) throws IOException,NullPointerException
{ String str[] = new String[100];
BufferedReader br = new BufferedReader( new InputStreamReader( System.in));
System.out.println("Enter the line of text");
System.out.println("Enter stop to quit");
int i=0;
do
{
str[i]=br.readLine();
i++;
}while(!str[i-1].equals("stop"));
/*for(int i=0;i<100;i++)
{
str[i]=br.readLine();
if(str[i].equals("stop")) break;
}*/
i=0;
System.out.println("The line u entered:");
do
{
System.out.println(str[i]);
i++;
}while(!str[i-1].equals("stop"));
/*
for(int i=0;i<100;i++)
{
if(str[i].equals("stop")) break;
System.out.println(str[i]);
}*/
}
}