Prog 1:
………………………
Save as input1.java
………………………
import java.util.Scanner;
class input1
{ public
static void main(String args[])
{
String s = scan.next();
System.out.println("Enter
input="+s);
int i = scan.nextInt();
System.out.println("Enter
input="+i);
}
}
………………………………………………………………………………………
Prog2:
………………….
Save as input2.java
……………………
import java.util.Scanner;
class input2
{
public static
void main(String args[])
{
int a;
float b;
String s;
Scanner in =
new Scanner(System.in);
System.out.println("Enter a string");
s =
in.nextLine();
System.out.println("You
entered string "+s);
System.out.println("Enter an integer");
a =
in.nextInt();
System.out.println("You entered integer "+a);
System.out.println("Enter a float");
b =
in.nextFloat();
System.out.println("You entered float "+b);
}
}
……………………………………………………………………………………
You can now read in different kinds of input data that
the user enters. The Scanner class supports getting primitives such as int,
byte, short, long in addition to getting strings. Here are some methods that
are available through the Scanner class:
- Read a byte -
nextByte()
- Read a short -
nextShort()
- Read an int -
nextInt()
- Read a long -
nextLong()
- Read a float -
nextFloat()
- Read a double -
nextDouble()
- Read a boolean -
nextBoolean()
- Read a complete line -
nextLine()
- Read a word -
next()
No comments:
Post a Comment