Reading a paragraph from the command line on java console program -
the while loop in following program not terminate can't output in last line tries print variable paragraph console. there similar problems solutions not practicle , not them. please suggest solution.
import java.io.bufferedreader; import java.io.ioexception; import java.io.inputstreamreader; public class test { public static void main(string[] args) throws ioexception { string line = ""; string paragraph = ""; system.out.println("enter text: "); inputstreamreader isr = new inputstreamreader(system.in); bufferedreader bufferedreader = new bufferedreader(isr); while ((line = bufferedreader.readline()) != null) { paragraph = paragraph + line + " "; } isr.close(); bufferedreader.close(); system.out.println(paragraph); }//method main ends here }
the code
while ((line = bufferedreader.readline()) != null)
will never true if dont assign null line , , cant assign null object through console , means have code , enter key or other character end input.
like
while(!(line.equals("exit"))) { //whatever }
this means when type exit @ end , program terminate , print paragraph.
you can try snippet
import java.io.bufferedreader; import java.io.ioexception; import java.io.inputstreamreader; public class test { public static void main(string[] args) throws ioexception { string line = ""; string paragraph = ""; system.out.println("enter text: "); inputstreamreader isr = new inputstreamreader(system.in); bufferedreader bufferedreader = new bufferedreader(isr); { line = bufferedreader.readline(); paragraph = paragraph + line + " "; }while(!line.equals("exit")); isr.close(); bufferedreader.close(); system.out.println(paragraph); }//method main ends here }
Comments
Post a Comment