Solved

stop the loop when enter nothing

Posted on 2001-06-18
6
194 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 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
Technology Partners: 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!

 
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: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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

Suggested Solutions

Title # Comments Views Activity
maven disable workspace resolution 1 41
printf performancy 11 86
Need help aligning JRadioButton that put in a box using GridBagLayout 2 27
java mysql insert application 14 46
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 …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
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:

733 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