Skip to Main Content

Java Development Tools

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

skipping the statement in the code

945595Aug 11 2012 — edited Aug 11 2012
Hi all, I have doubt in following java code , Bolded line is not executing(instead its skipping) I mean i cant enter name.

Code :
import java.io.*;
public class ManyInputs {
public static void main(String args[])throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));


System.out.print("Enter your id:");
int n=Integer.parseInt(br.readLine());

System.out.print("Enter your sex M/F:");
char c=(char)br.read();
System.out.print("Enter your name:");
String name=br.readLine();




System.out.println("Your Id:"+n);
System.out.println("Your sex:"+c);
System.out.println("Your name:"+name);
}

}


Output:
Enter your id:504
Enter your sex M/F:m
Enter your name:Your Id:504
Your sex:m
Your name:

But if i change the order of the statements its working perfectly, like below

COde:

import java.io.*;
public class ManyInputs {
public static void main(String args[])throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));


System.out.print("Enter your id:");
int n=Integer.parseInt(br.readLine());
System.out.print("Enter your name:");
String name=br.readLine();
System.out.print("Enter your sex M/F:");
char c=(char)br.read();





System.out.println("Your Id:"+n);
System.out.println("Your sex:"+c);
System.out.println("Your name:"+name);
}

}

Output:
Enter your name:Reddy
Enter your sex M/F:m
Your Id:504
Your sex:m
Your name:Reddy


Regards,
Uvaraja
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Sep 8 2012
Added on Aug 11 2012
1 comment
462 views