What's wrong with this code?
Hello I am not a java expert yet, that's why dont know what's wrong with this code. all i want to do is ask user if he wants to enter a paragraph. If the ans is y or Y the console should wait for the user's data but the prompt does not wait and programs ends up.
import java.io.*;
class A1Q1Main1
{
publicstaticvoid main(String str[])throws IOException
{
String name;
char c=0;
int tmp=0;
String strg;
System.out.println("University's Misconduct Poilcy.");
System.out.print("\n");
System.out.print("\n");
System.out.print("\n");
System.out.print("Do you want to enter a paragraph (Y or y for yes. N or n for no)? ");
try
{
tmp=System.in.read();
c=(char) tmp;
}
catch(IOException e){}
System.out.println("The character pressed is:"+c);
if(c=='y' || c=='Y')
System.out.print("Enter your paragraph: ");
InputStreamReader tempReader=new InputStreamReader(System.in);
BufferedReader reader=new BufferedReader(tempReader);
name=reader.readLine();
}
}
[2045 byte] By [
fuzzyiea] at [2007-11-26 16:48:28]

> name=reader.readLine();
Try replacing the line above with the following:
while((name=reader.readLine()) != null) {
System.out.println("You entered: "+name);
}
> Shouldn't reader.readLine() block program execution
> until it finds end of line?
That's what I thought, but apparently that System.in.read() is messing things up.
@OP:
Try not using that System.in.read() but use your BufferedReader to get the Y or N from the user.
Something like this:import java.io.*;
public class Test {
public static void main(String[] args) throws IOException {
InputStreamReader tempReader=new InputStreamReader(System.in);
BufferedReader reader=new BufferedReader(tempReader);
String input;
System.out.println("University's Misconduct Poilcy.");
System.out.print("\n");
System.out.print("\n");
System.out.print("\n");
System.out.print("Do you want to enter a paragraph (Y or y for yes. N or n for no)? ");
input = reader.readLine();
if(input.equalsIgnoreCase("y")) {
// your code ...
}
}
}