- Captain BuggyPromoter
- Posts : 256
Points : 100442
Coins : 9802
Join date : 2013-01-08
Warning Level :
sample program in java!!
Thu Oct 24, 2013 5:51 pm
- Spoiler:
- 1 import java.io.BufferedReader;
2 import java.io.InputStreamReader;
3 import java.io.IOException;
4
5 public class GetInputFromKeyboard {
6
7 public static void main( String[] args ){
8 BufferedReader dataIn = new BufferedReader(new
9 InputStreamReader( System.in) );
10
11 String name = "";
12 System.out.print("Please Enter Your Name:");
13 try{
14 name = dataIn.readLine();
15 }catch( IOException e ){
16 System.out.println("Error!");
17 }
18 System.out.println("Hello " + name +"!");
19 }
20 }
keys to remember about this code
* line 1,2,3 thats what you call a lines package of the program..
- Spoiler:
- indicate that we want to use the classes BufferedReader,
InputStreamReader and IOException which are inside the
java.io package.
●These statements can also be written as,
import java.io.*; ilbis na ung 1 to 3 lines :)
Packages contain classes that have related purpose
- Spoiler:
- public class GetInputFromKeyboard {
means we declare a class named GetInputFromKeyboard
The next statement declares the main method
public static void main( String[] args ){
BufferedReader dataIn = new BufferedReader(new InputStreamReader(System.in));
declares a variable named dataIn, with the class type BufferedReader.
String name = "";
declares a String variable with identifier name.
System.out.print("Please Enter Your Name:");
outputs a String on the screen asking for the user's name.
The given block defines a try-catch block
try{
name = dataIn.readLine();
}catch( IOException e ){
System.out.println("Error!");
}
This assures that the possible exceptions that could occur in
the statement
name = dataIn.readLine();
the method call, dataIn.readLine(), gets input from the user
and will return a String value.
This value will then be saved to our name variable, which we
will use in our final statement to greet the user,
System.out.println("Hello " + name + "!");
- RanzerAdministrator Ranzer
- Posts : 2774
Points : 111087
Coins : 1462
Join date : 2012-07-07
Warning Level :
Re: sample program in java!!
Thu Oct 24, 2013 9:17 pm
You can use the bbcode [ code ](remove the space) when posting programming codes this way we have the convenience of time in our hands to copy and paste the codes in the compiler and you can use "//" to add the comments in the actual line :D
example:
example:
- Code:
// this is a single line comment
int guessNumber = (int) (Math.random() * 10);
- Captain BuggyPromoter
- Posts : 256
Points : 100442
Coins : 9802
Join date : 2013-01-08
Warning Level :
Re: sample program in java!!
Fri Oct 25, 2013 7:59 am
ForumBot wrote:You can use the bbcode [ code ](remove the space) when posting programming codes this way we have the convenience of time in our hands to copy and paste the codes in the compiler and you can use "//" to add the comments in the actual line :D
example:
- Code:
// this is a single line comment
int guessNumber = (int) (Math.random() * 10);
salamat po... pag aaralan ko po ung BBcode..d ko kc ganu kabisado!!
- HnCo04Dragon Balls Collector
- Posts : 171
Points : 88300
Coins : 11015
Join date : 2013-08-25
Warning Level :
Re: sample program in java!!
Sat Jan 11, 2014 9:04 am
thank you for this
it made me remember everything i learned from school hahha XD
it made me remember everything i learned from school hahha XD
Permissions in this forum:
You cannot reply to topics in this forum