Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
44 views

Java Input Output Syste, in Out Console and Scanner Example TP

The document contains 3 Java programs that demonstrate different ways to take input and display output in Java: 1) The first program uses System.in and System.out to take character input and display output to the console. 2) The second program uses the Scanner class to take string, integer, and double inputs and display the input values. 3) The third program uses the Java Console class to take a string input and display an output message with the input string.

Uploaded by

Rajesh Kumar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views

Java Input Output Syste, in Out Console and Scanner Example TP

The document contains 3 Java programs that demonstrate different ways to take input and display output in Java: 1) The first program uses System.in and System.out to take character input and display output to the console. 2) The second program uses the Scanner class to take string, integer, and double inputs and display the input values. 3) The third program uses the Java Console class to take a string input and display an output message with the input string.

Uploaded by

Rajesh Kumar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

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);
}
}

You might also like