Exercise:- Program to show use of System.in and System.
out
to take input and display output in Java.
import java.io.*;
class inoutexample {
public static void main(String[] args)
int i=0;
// prints GeeksForGeeks to the console
System.out.println("Hello World!");
try{
i=System.in.read();//returns ASCII code of 1st character
catch(Exception e)
System.out.println(e);
System.out.println(i);//prints ASCII code of 1st character
System.out.println((char)i);//will print the character
System.out.println("simple message");
System.err.println("error message");
Exercise:- Program to show use of Scanner Class to take input
in Java.
import java.util.Scanner;
class Main {
public static void main(String[] args) {
Scanner myObj = new Scanner(System.in);
System.out.println("Enter name, age and salary:");
// String input
String name = myObj.nextLine();
// Numerical input
int age = myObj.nextInt();
double salary = myObj.nextDouble();
// Output input by user
System.out.println("Name: " + name);
System.out.println("Age: " + age);
System.out.println("Salary: " + salary);
Exercise : - Program to show the use of Java Console Class.
import java.io.Console;
class ReadStringTest{
public static void main(String args[]){
Console c=System.console();
System.out.println("Enter your name: ");
String n=c.readLine();
System.out.println("Welcome "+n);
}
}