Labels

Thursday, February 4, 2016

Accept user data using Scanner class;

Using Scanner Class in Java to input user data;

import java.util.Scanner;

public class scan {
public static void main(String[] args){
int a;
String b;
double c;
long e;
float f;
short g;
Scanner s= new Scanner(System.in);
System.out.print("Enter a integer: ");
a= s.nextInt();
System.out.print("Enter a string: ");
b= s.nextLine();
System.out.print("Enter a double: ");
c= s.nextDouble();
System.out.print("Enter a long: ");
e= s.nextLong();
System.out.print("Enter a float: ");
f= s.nextFloat();
System.out.print("Enter a short: ");
g= s.nextShort();
System.out.println(a+"\n"+b+"\n"+c+"\n"+e+"\n"+f+"\n"+g);
}

}

To compile the following code do following things:-

  • Save the above code with a file name scan and with extension .java
  • Now, open your terminal or cmd and move to the location where your file is being saved using cd command.
  • Compile java code using javac . Here file name is scan so type javac scan.java
  • Run the code using java . Here file name is scan so type java scan


No comments:

Post a Comment