Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Answers Viewqa Java-Beginners readline Error 2
Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


sella gompel
readline Error 2
1 Answer(s)      4 years and 8 months ago
Posted in : Java Beginners


Hi sir,
can you look again please.
The error is hard to notice, but if you execute the code,
1. enter a string the first input request (eg hello world),
2. ENTER

3a. ENTER n for the next input request
The code will not terminated after the 'n'. This is because the first char is missing and from the readLine(). You would have to to enter 'nn'
for the code to exit. I think the problem is caused by the read() statement, just don't know why.

3b. ENTER another string for the next input request (eg bye world)
The code will output text "ye world", again the first char (b) is missing.

Could you please check again. I have to use the System.in.read() for this home work.

Thanks for the quick response
thanks sella

Advertisement
View Answers

April 27, 2010 at 5:21 PM


Hi Friend,

Use br.readLine instead of System.in.read, your code will work.

Your modified code:

import java.io.*;
class TestAdvt{
public static void main (String[] args){
boolean done=false;
while (!done){
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String str = null;
try{
System.out.println ("Please enter the advertisement message to be displayed (enter 'n' to exit)");
str = br.readLine();
if (!str.equalsIgnoreCase("n")){
System.out.println ("str1 = " + str);
Thread advt = new Thread(new Advertisement(str));
advt.start();
br.readLine();
advt.interrupt();
}
else{
System.out.println ("str2 = " + str);
done = true;
}
}
catch (IOException e){
System.out.println("IOException caught: " + e.getMessage());
}
}
}
}
class Advertisement extends Thread{
private String message;
Advertisement (String message) { this.message=message;}
public void run(){
while (!message.equals("")){
try{
System.out.print(message+".. ");
Thread.sleep(1000);
}
catch (InterruptedException e){
this.message="";
break;
}
}
}
}

Thanks


Related Tutorials/Questions & Answers:
readline Error 2 - Java Beginners
readline Error 2  Hi sir, can you look again please. The error... request (eg hello world), 2. ENTER 3a. ENTER n for the next input request... is missing and from the readLine(). You would have to to enter 'nn' for the code
readline Error - Java Beginners
readline Error  Hi sir, please helpme with my code. Mycode does not work. The readLine () method seems to loose the first character of the string...; Hi Friend, Your code works fine here. Thanks  The error
Advertisements
Struts 2 File Upload error
Struts 2 File Upload error  Hi! I am trying implement a file upload using Struts 2, I use this article, but now the server response the error: ognl.MethodFailedException: Method "setArquivo" failed for object
Struts 2 : Http Status Error 404 - Struts
Struts 2 : Http Status Error 404  Hi All, I'm facing the below... error as shown below. see below error for the details code... --------------------------------------------------------------------------- ERROR(404) : "The requested
struts 2 Application starting error - Struts
struts 2 Application starting error  Hello Sir/Madam, Recently i have installed tomcat 5.5.23 on my window machine with XP, then i make an small application which i have attached with name Struts2Application, but after
HTTP Status 404 - /web/login/showLogin.action Error in Struts 2 - Struts
HTTP Status 404 - /web/login/showLogin.action Error in Struts 2  Hi... Eclipse 3.4 Struts 2 Now my question is I am trying to implement the login... the doLogin action . when I click on the link I get the following error type Status
ReadLine(String fmt,Object... args) of Console class - Java Beginners
ReadLine(String fmt,Object... args) of Console class  How to reload... = System.console(); if(console != null) { String formatString = "%1$4s %2$10s...;, "100"); console.printf(formatString, "2", "20"
this code gives addition of 2 numbers in j2me..but the code is not executing ..can u pls find out the error...??
this code gives addition of 2 numbers in j2me..but the code is not executing ..can u pls find out the error...??  import javax.microedition.midlet.*; import javax.microedition.lcdui.*; public class add1 extends MIDlet implements
error
error  while iam compiling iam getting expected error
Error-
Error-   Hello, I would like to know about XSD file. I try to print XML file but I am getting error SAXException-- says Content is not allowed in prolog. Please help me
error
error  whats the error.............. import java.util.Scanner...(); switch(a) { case 1:add(); break; case 2:sub(); break...."); System.out.println("2 For Subtract."); a=s.nextInt(); switch(a) { case
error
error  i have 404 error in my program plz tell me yhe solution about
error
error  i have 404 error in my program plz tell me yhe solution about
error
/ServletUserEnquiryForm.shtml getting an error given below SQLException caught: [Microsoft][ODBC SQL Server Driver]COUNT field incorrect or syntax error please suggest
Error
Error  I have created ajax with php for state and city. When I change state then city will not come in dropdown list and it give me error as 'Unknown Runtime Error'. This error come only in IE browser, but in other brower
error
error
error
error  java.lang.unsupportedclassversionerror:bad major version at offset 6 how to solve this????   Hi, Please check the version of framework used and also the JDK version. This type error also comes when java file
error
error  When I deploye the example I have this message cannot Deploy HelloWorld Deployment Error for module: HelloWorld: Error occurred during deployment: Exception while deploying the app [HelloWorld
error!!!!!!!!!
error!!!!!!!!!   st=con.createStatement(); int a=Integer.parseInt(txttrno.getText()); String b=txttname.getText(); String c=txtfrom.getText(); String d=txtto.getText
error!!!!!!!!!
error!!!!!!!!!   st=con.createStatement(); int a=Integer.parseInt(txttrno.getText()); String b=txttname.getText(); String c=txtfrom.getText(); String d=txtto.getText
error!!!!!!!!!
error!!!!!!!!!   st=con.createStatement(); int a=Integer.parseInt(txttrno.getText()); String b=txttname.getText(); String c=txtfrom.getText(); String d=txtto.getText
new 2
new 2  <%@page import="java.util.ArrayList"%> <%@page...; @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@2 sevelet @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@2 import java.io.IOException; import java.io.PrintWriter; import
Error
Error  Hi. I am getting error in the following code after the line I have commented as ERROR. How to solve this. Thanks in advance. package...;"); // ERROR out.println(" var pattern
Error
Error  Hi. I am getting error in the following code after the line I have commented as ERROR. How to solve this. Thanks in advance. package...;"); // ERROR out.println(" var pattern
Error
Error  Hi. I am getting error in the following code after the line I have commented as ERROR. How to solve this. Thanks in advance. package...;"); // ERROR out.println(" var pattern
Error
Error  Hi. I am getting error in the following code after the line I have commented as ERROR. How to solve this. Thanks in advance. package...;"); // ERROR out.println(" var pattern = /^\d{3,5
error
is"+sum); } } this is my program i need to print the series (1+2/12)+(1+2
error
"+it); } } this is my program i am getting an error saying cannot find symbol class string
use is of 2
use is of 2  why we use 2 in "Integer.parseInt(str,2);" for binary to decimal conversion..??   ya i got it... because binary contain only two o and 1...so here use 2...same as octal has 8
Magicbox 2
Magicbox 2  how to build this? import java.util.*; class magicbox...; } } } } v15=a[0][0]+a[0][1]+a[0][2]==15 && a[1][0]+a[1][1]+a[1][2]==15 && a[2][0]+a[2][1]+a[2][2]==15 &&
Struts 2
Struts 2  we can extend DispatchAction class to implement a common session validation in struts 1.x. how to do the same in the struts2
Advertisements
 

 

 

DMCA.com