[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

stop the loop when enter nothing

Posted on 2001-06-18
6
Medium Priority
?
202 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 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This video teaches viewers about errors in exception handling.
Suggested Courses
Course of the Month19 days, 17 hours left to enroll

873 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