Input Method:
We are going to use Scanner for accepting values from the user . Its a simple one.
First include the package util.
import java.util.*;
Only small letters and * indicates all the files from the package and there is a semi colon in the end.
Then comes the main function
public static void main( )
{
Scanner S=new Scanner(System.in);
Remember S in red colour are capital letters. Here S is a variable can be in small or capital letter or any other variable name.
There are different statements for accepting different data type values.
int a= S.nextInt( );
float f= S.nextFloat( );
double x=S.nextDouble( );
But accepting character and string are different.
char ch=(char)System.in.read( );
String st=S.next( );
String st1=S.nextLine( );
next( ) accepts a word till a space and keeps the cursor in the same line.
nextLine( ) accepts words and moves the cursor to the next line after accepting the value.
Let us accept two numbers.
import java.util.*; ------------------------------------ 1
class ex1
{
public static void main( )
{
Scanner s=new Scanner(System.in); --------------- 2
int a, b,sum; --------------------------------------------- 3
System.out.println("Enter the first number"); ------ 4
a=s.nextInt( ); ------------------------------------------- 5
System.out.println("Enter the second number");----- 4
b=s.nextInt( );-------------------------------------------- 5
sum=a+b; -------------------------------------------------6
System.out.print("Total = " +sum);------------------- 7
}
}
1 is the first line in the program.
2 Write any variable instead of s , if you want.
3 is variable declaration.
4 is just a prompt message. A remainder to the user to enter a number.
5 Since I am accepting integer values, it should be nextInt. Remember no space after next and I in Int is capital.
6 is the calculation
7 is the output.
For other programs step 3 and step 5 will be different according to the data type you use.
We are going to use Scanner for accepting values from the user . Its a simple one.
First include the package util.
import java.util.*;
Only small letters and * indicates all the files from the package and there is a semi colon in the end.
Then comes the main function
public static void main( )
{
Scanner S=new Scanner(System.in);
Remember S in red colour are capital letters. Here S is a variable can be in small or capital letter or any other variable name.
There are different statements for accepting different data type values.
int a= S.nextInt( );
float f= S.nextFloat( );
double x=S.nextDouble( );
But accepting character and string are different.
char ch=(char)System.in.read( );
String st=S.next( );
String st1=S.nextLine( );
next( ) accepts a word till a space and keeps the cursor in the same line.
nextLine( ) accepts words and moves the cursor to the next line after accepting the value.
Let us accept two numbers.
import java.util.*; ------------------------------------ 1
class ex1
{
public static void main( )
{
Scanner s=new Scanner(System.in); --------------- 2
int a, b,sum; --------------------------------------------- 3
System.out.println("Enter the first number"); ------ 4
a=s.nextInt( ); ------------------------------------------- 5
System.out.println("Enter the second number");----- 4
b=s.nextInt( );-------------------------------------------- 5
sum=a+b; -------------------------------------------------6
System.out.print("Total = " +sum);------------------- 7
}
}
1 is the first line in the program.
2 Write any variable instead of s , if you want.
3 is variable declaration.
4 is just a prompt message. A remainder to the user to enter a number.
5 Since I am accepting integer values, it should be nextInt. Remember no space after next and I in Int is capital.
6 is the calculation
7 is the output.
For other programs step 3 and step 5 will be different according to the data type you use.
No comments:
Post a Comment