Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

stop the loop when enter nothing

Posted on 2001-06-18
6
Medium Priority
?
198 Views
Last Modified: 2010-03-31
hai, i just need to implement a loop that can be stop when i enter "nothing"
by using the following incorrect statement:

  BufferedReader br=new BufferedReader(new InputStreamReader (System.in));
  do{
  System.out.print("Enter any string: ");
  String temp;
  temp=br.readLine();
  }while (br.readLine()!=null);
  //this loop is working wrongly,it doesn't stop!

output suppose to be like below:
Enter any string: abc
Enter any string: cds
Enter any string:
//when nothing(empty string) is being enter, the loop stop.

how the above coding can be modified?
0
Comment
Question by:JYnet
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 9

Expert Comment

by:Ovi
ID: 6205228
You will never be able to enter a null string. The readLine will wait until the enter caracter (at least) is entered. You have two solutions :
1. to provide an exit string (in my example "exit")
2. to implement a Timer which will count for an noumber of seconds and if nothing happends, force the exit from loop.

1.
import java.io.*;

public class x {
public static void main(String[] args) {
BufferedReader br=new BufferedReader(new InputStreamReader (System.in));
try {
  String line = null;
  System.out.print("Enter any string : ");
  while((line = br.readLine()) != null) {
    System.out.println("Entered : " + line);
    if(line.equalsIgnoreCase("exit"))
      break;
    System.out.print("Enter any string : ");
  }
} catch(Exception e) {System.out.println(e.toString());}
}
}
0
 

Accepted Solution

by:
Amarnath earned 20 total points
ID: 6205276
u can try in this way

BufferedReader br=new BufferedReader(new InputStreamReader (System.in));
               System.out.print("Enter any string: ");
               String temp = br.readLine();
               while(temp!=null && !temp.equals(""))
               {
                     System.out.print("Enter any string: ");
                     temp=br.readLine();
               }
0
 
LVL 3

Expert Comment

by:rjackman
ID: 6205394
try this 1
import java.io.*;

public class TestReader
{
     public static void main(String h[])
     {
          try
          {
          String temp = null;
          BufferedReader br=new BufferedReader(new InputStreamReader (System.in));
           do{
           System.out.print("Enter any string: ");
           
           temp=br.readLine();
          System.out.println("U Enter ------->"+temp);
          if(temp.length()==0)
               return;
           }while (temp!=null);
          }
          catch(IOException ioe)
          {
               ioe.printStackTrace();
          }

     }
}

cheers
RJ
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 3

Expert Comment

by:superschlonz
ID: 6206193
Another solution is:

String line;
while( (line = br.readLine()) != null )
{
  if( line.ttrim().length() == 0 )
    break;
  // process line here...
}
0
 
LVL 1

Expert Comment

by:scientist060700
ID: 6208721
put an if statement
if (br.readLine ()=null) {
system.exit(1);
}

OR

if (br.readLine()=null)
break;

this should work
--scientist
0
 

Author Comment

by:JYnet
ID: 6209091
thanks for providing solution, i will see which one is most efficient, thanks!
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question