Solved

stop the loop when enter nothing

Posted on 2001-06-18
6
191 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Printing to a specific printer tray (HP 806dn printer) 3 83
Groovy:unable to resolve class error 2 69
stackato and cloud 4 69
eclipse shortcuts 9 45
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
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:
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

911 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

23 Experts available now in Live!

Get 1:1 Help Now