Java

Moderators: zibadian
Number of threads: 7836
Number of posts: 18235

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Input from keyboard Posted by rashmigalagali on 30 May 2010 at 10:23 PM
Can anyone please explain how to read an input to a java code from keyboard.Please explain with an example.

Thanks & Regards,

Rashmi
Report
Re: Input from keyboard Posted by paycity on 30 May 2010 at 10:42 PM
You can get input from keyboard with two methods.

1nd method (System.in):
import java.io.*;
public class input {
public static void main(String args[]){
try{
System.out.print("What's your name? ");
DataInputStream in=new DataInputStream(System.in);
String name = in.readLine();
System.out.println("Your name is " + name + "!");
}catch(IOException e){}
}
}

Compile the code and run it. The program will ask you about your name. Type your name and press Enter key and program will show your name.

2nd method (arguments):
public class args {
public static void main(String args[]){
if(args.length == 0) {
System.out.println("Type your name as: java args John\n or java args \"John Doe\"");
}
else {
System.out.println("Your name is " + args[0] + "!");
}
}
}

Compile the code and run it. The program will not ask you about your name. Type your name as: java args name and press Enter key and program will show your name.



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.