Solved

stop the loop when enter nothing

Posted on 2001-06-18
6
190 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
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 5 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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Receive file in Servlet 1 36
strCopies  challenge 17 73
Securing Jmx Console and web console 2 48
Java Loop 4 22
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

747 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now