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]
# 1

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...

Peetzorea at 2007-7-9 6:05:30 > top of Java-index,Java Essentials,New To Java...
# 2

> 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);

}*/

}

kalania at 2007-7-9 6:05:30 > top of Java-index,Java Essentials,New To Java...
# 3

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]);

}*/

}

}

ganesh_renganathana at 2007-7-9 6:05:30 > top of Java-index,Java Essentials,New To Java...
# 4
You'll also have an IndexOutOfBoundsException if your user enters more than 100 strings before entering stop. You should also check this in your while loop test.
Peetzorea at 2007-7-9 6:05:31 > top of Java-index,Java Essentials,New To Java...
# 5

out of the topic , i just noticed something... if I type something like str [ i ] without spaces and without the [ code ] [ /code ] tag...it displays only the str does not display the [ i ] in my text...it will be helpful whenever someone posts a piece of code, enclose them within the code tags...

ganesh_renganathana at 2007-7-9 6:05:31 > top of Java-index,Java Essentials,New To Java...
# 6
> notice that you have to use str[ i ] instead of just> str...actually he has not used str instead of str[i]. I saw it when I quoted the original. it was in his code and [i] is missing in the post, however Vasu please use code tags when u post
kalania at 2007-7-9 6:05:31 > top of Java-index,Java Essentials,New To Java...