Ways to read input from console in Java
Program to Read and Print the name of the user
package javaprogrammingdemo;
import java.util.Scanner;
public class javalabclass{
public static void main(String args[])
{
Scanner input = new Scanner(System.in);
System.out.println("Enter the String");
String name = input.nextLine();
System.out.println(name);
}
}
Program Output
Enter the String
satish
satish
Program to Read and Display an Integer Value to the User.
package javaprogrammingdemo;
import java.util.Scanner;
public class javalabclass{
public static void main(String args[])
{
Scanner input = new Scanner(System.in);
System.out.println("Enter the number");
int value = input.nextInt();
System.out.println(value);
}
}
Program to Read a Character and Display the character to the User
package javaprogrammingdemo;
import java.util.Scanner;
public class javalabclass{
public static void main(String args[])
{
Scanner input = new Scanner(System.in);
System.out.println("Enter the character");
char inputChar = input.next().charAt(0);
System.out.println(inputChar);
}
}
Program Output
Enter the character
b
b