• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

small IO problem

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi ..
This IO program is getting compiled without any error.
But I couldn't run it.
Here it is:-
import java.io.*;
class inpu2{
public static void main(String args[]){
BufferedReader rb = new BufferedReader(new InputStreamReader(System.in));
PrintWriter pw = new PrintWriter(System.out,true);
String str="rahul";
pw.println("enter lines to write." + "enter 'stop' to quit");
try{
while(!str.equals("stop"));
{
str = rb.readLine();
pw.println(str);
}
}catch(IOException e){pw.println ("kaya he?");}
}
}
I think its getting in to infinite loop .but why?
When I done it using do-while loop it ran properly .
But why not this way?
Plz help.
rahul
 
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi ra,
Move the try{}catch{} construct inside the while loop

Hope that helps.
------------------
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
import java.io.*;
class inpu2{
public static void main(String args[]){
BufferedReader rb = new BufferedReader(new InputStreamReader(System.in));
PrintWriter pw = new PrintWriter(System.out,true);
String str="rahul";
pw.println("enter lines to write." + "enter 'stop' to quit");
try{
while(!str.equals("stop"))//;
{
str = rb.readLine();
pw.println(str);
}
}catch(IOException e){pw.println ("kaya he?");}
}
}
Just comment out the semi colon(That is the reason for the infinite loop)
reply
    Bookmark Topic Watch Topic
  • New Topic