Java Practical File

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 99

SESSION 2022-23

SUBJECT:
JAVA

SUBMITTED BY: - SUBMITTED TO: -

HARSHDEEP KAUR MRS. JASDEEP

BCA 3RD Year MAM

201503
2

Index
S.NO. TOPIC PAGE NO.

1 Print message 5
2 Import package lang in java 7
3 Arithmetic operators 9
4 Relational operators 11
5 Increment operators 13
6 Conditional operators 15
7 Casting Operators 17
8 If Statement 19
9 If else 21
10 Else if 23
11 Nested if 25
12 Switch statement 27
13 While statement 29
14 Do while statement 31
15 For loop 33
16 Break statement and continue 35
17 Method Declaration 37
18 Constructor 39
19 Method overloading 41
20 String member 43
21 Nesting of methods 45
22 Method Overriding in inheritance 47
23 Sorting a list of number 49
24 Two dimensional array 51
3

25 Alphabetical ordering of strings 53


26 Workings with vectors and array 55
27 Wrapper class 57
28 Using a package 59
29 Implementing Multiple inheritance 61
30 Implementing interface 63
31 Creating threads using the thread class 65
32 Using thread method 67
33 Using of Priority in method 69
34 Using Runnable interface 71
35 Run time error 73
36 Catch for exception handling 75
37 Multiple catch block 77
38 Throwing our own exception 79
39 Applet 81
40 Applet with changed font color 83
41 Polygons and Arc 85
42 Drawing line and rectangle 87
43 Line Graph 89
44 Label,TextField,Button Control and Radio Button 91
45 Checkbox 93
46 User input through BufferedReader 95
47 Use of Scanner class to read Standard Input 97
48 Use of DataInputStream Class to input Values 99
Program 1: Print Message in Java

Output:
4

Input:

class Harsh

{
5

public static void main(String args[])

System.out.println("Hello");

Program 2: Import Package lang in java

Output:
6

Input:

import java.lang.Math;

class bca
7

public static void main(String args[])

double x=10,y;

y=Math.sqrt(x);

System.out.println("y= "+y);

</body>

</html>

Program 3: Arithmetic Operators

Output:
8

Input:

class test

{
9

public static void main(String args[])

int a=10,b=20;

System.out.println("a= "+a);

System.out.println("b= "+b);

System.out.println("a+b= "+(a+b));

System.out.println("a-b= "+(a-b));

System.out.println("a*b= "+(a*b));

System.out.println("a/b= "+(a/b));

Program 4: Relational Operator


10

Output:

Input:

classRelationaloperators
11

public static void main (String args[ ])

float a = 20.0f, b = 20.75f, c = 15.0f;

System.out.println(" a = " + a);

System.out.println(" b = " + b);

System.out.println(" c = " + c);

System.out.println(" a < b is " + (a<b));

System.out.println(" a > b is " + (a>b));

System.out.println(" a == c is " + (a==b));

System.out.println(" a <= c is " + (a<=c));

System.out.println(" a >= b is " + (a>=b));

System.out.println(" a != c is " + (b!=c));

}
}

Program 5: Incremental Operator


12

Output:

Input:

classIncrementoperator
13

public static void main(String args [ ])

int m = 10, n = 20;

System.out.println(" m = " + m);

System.out.println(" n = " + n);

System.out.println(" ++m = " + ++m);

System.out.println(" n++ = " + n++);

System.out.println(" m = " + m);

System.out.println(" n = " + n);

Program 6: Conditional Operator

Output:
14

Input:
15

class Arth

public static void main(String[] args)

int num1 = 13, num2 = 40, sum = 0;

System.out.println("num1 = " + num1);

System.out.println("num2 = " + num2);

sum = num1 + num2;

System.out.println("The sum = " + sum);

Program 7: Casting Operator


16

Output:

Input:

class Casting
17

public static void main(String args [ ])

float sum;

int i;

sum = 0.0f;

for(i = 1; i <= 10; i++)

sum = sum + 1/ (float)i;

System.out.println(" i = " + i);

System.out.println(" sum = " + sum);

Program 8: If Statement
18

Output:

Input:

public class Casting

{
19

public static void main(String args [ ])

int a=10;

if(a==10)

System.out.println("true");

Program 9: If-Else Statement

Output:
20

Input:

class ifelse

{
21

public static void main(String args[]){

int number [] = { 30, 40, 50, 47};

int even = 0, odd = 0;

for (int i = 0; i < number.length; i++){

if ((number[i] % 2) == 0){

even += 1; }

else

odd += 1;

System.out.println(" Even numbers : " + even + "odd numbers : " + odd);

Program 10: Else If Statement

Output:
22

Input:

class Elseifladder

{
23

public static void main (String args [])

int rollnumber[] = {12, 46, 60};

int marks[] = {23,68, 70, 78};

for (int i = 0; i<rollnumber.length; i++)

if (marks[i] > 48)

System.out.println(rollnumber[i] + " Honours");

else if (marks[i] > 67)

System.out.println(rollnumber[i] + " I Division");

else if (marks[i] > 69)

System.out.println(rollnumber[i] + " II Division");

else

System.out.println(rollnumber[i] + " FAIL");

Program 11: Nested If


24

Output:

Input:

class Nesting{

int m, n;
25

Nesting (int x, int y)

m = x;

n = y;

int largest( )

if (m >= n)

return(m);

else

return(n);

void display( ){

int large = largest ( );

System.out.println("Largest value = " + large);

class Nesting1{

public static void main(String[] args) {

Nesting next = new Nesting(60, 80);

next.display();

Program 12: Switch Statement


26

Output:

Input:

class swi

{
27

public static void main(String args[]) {

int ch=2;

System.out.println("select your choice");

System.out.println(" M -> Madras");

System.out.println(" B -> Bombay");

System.out.println(" c -> calcutta");

switch (ch )

case 1: System.out.println("Madras : Booklet 5");

break;

case 2: System.out.println("Bombay : Booklet 9");

break;

case 3: System.out.println("calcutta : Booklet 8");

break;

default: System.out.println("Invalid : Booklet 5");

Program 13: While Loop

Output:
28

Input:

class whiletest

{
29

public static void main(String args[])

StringBuffer string = new StringBuffer();

char c;

System.out.println("enter a string");

try

while ( (c = (char) System.in.read() ) != '\n')

string.append(c);

catch (Exception e)

System.out.println("Error in input");

System.out.println("you have entered...");

System.out.println(string);

Program 14: Do While Loop


30

Output:

Input:

public class Dowhile

{
31

public static void main(String args[])

int row, column, y;

System.out.println("Multiplication Table \n");

row = 1;

do

column = 1;

do

y = row * column;

System.out.print(" " + y);

column = column + 1;

while (column <= 3);

System.out.println("\n");

row = row + 1;

while(row <= 3);

Program 15: For Loop

Output:
32

Input:

class forloop

{
33

public static void main(String args[])

long p;

int n;

double q;

System.out.println("2 to power -n n 2 to power n");

p = 1;

for (n = 0; n < 10; n++)

if(n==0)

p = 1;

else

p = p * 2;

q = 1.0 / (double)p;

System.out.println(" " + q + " " + n + " " + p);

Program 16: Break Statement and Continue

Output:
34

Input:

class ContinueBreak

{
35

public static void main(String args[])

Loop1 : for (int i = 1; i <100; i++)

System.out.println(" ");

if (i>=10) break;

for (int j=1; j<100; j++)

System.out.print( " * ");

if(j == i)

continue Loop1;

System.out.println("Termination by BREAK");

Program 17: Method Declaration


36

Output:

Input:

class access{
37

int x;

void m1(){

int y;

x=10;

y=x;

System.out.println("x= "+x);

System.out.println("y= "+y);

class met

public static void main(String args[]){

access a=new access();

a.m1();

Program 18: Constructor


38

Output:

Input:
class rect
39

rect()

rect(int x,int y)

int z=x*y;

System.out.println("Area is "+z);

class ar

public static void main(String args[])

rect r= new rect(10,20);}}

Program 19: Method Overloading


40

Output:

Input:

class Adder{
41

static int add(int a,int b){return a+b;}

static int add(int a,int b,int c){return a+b+c;}

class over1{

public static void main(String[] args){

System.out.println(Adder.add(11,11));

System.out.println(Adder.add(11,11,11));

}}

Program 20: Static Member


42

Output:

Input:

class Mathoperation
43

static int mul(int x, int y)

return (x*y);

static int divide (int c, int d)

return c/d;

class Math

public static void main(String args[])

float a = Mathoperation.mul (2,3);

float b = Mathoperation.divide (6,2);

System.out.println("b = " + b);

System.out.println("a = " + a);}}

Program 21: Nesting of Methods


44

Output:

Input:
45

class Nesting
{
int m, n;
Nesting (int x, int y)
{
m = x;
n = y;
}
int largest( )
{
if (m >= n)
return(m);
else
return(n);
}
void display( )
{
int large = largest ( );
System.out.println("Largest value = " + large);
}
}
class Nestingtest
{
public static void main(String[] args)
{
Nesting next = new Nesting(60, 80);
next.display();
}

Program 22: Method Overriding in Inheritance


46

Output:

Input:
47

class Super
{
int x;
Super(int x)
{
this.x = x;
}
void display()
{
System.out.println("Super x = " + x);
}
}
class Sub extends Super
{
int y;
Sub (int x, int y)
{
super (x);
this.y = y;
}
void display()
{
System.out.println("Super x = " + x);
System.out.println("Sub y = " + y);
}
}
class Over
{
public static void main(String[] args) {
Sub s1 = new Sub(100,200);
s1.display();
}
}
Program 23: Sorting a List of Numbers
48

Output:

Input
49

class sort
{
public static void main (String args [])
{
int i,j;
int number[] = {57, 80, 78};
int n = number.length;
System.out.print("given list : ");
for (i=0; i<n; i++){
System.out.print(" " + number[i]);
}
System.out.println("\n");
//Sorting begins
for (i = 0; i < n; i++)
{
for ( j = i+1; j < n; j++){
if (number [i] < number[j]){

// Interchange values
int temp = number[i];
number[i] = number[j];
number[j] = temp;
}
}
}
System.out.print ("Sorted list");
for ( i = 0; i < n; i++)
{
System.out.print (" " + number[i]);
}
System.out.println(" ");
}
}
50

Program 24: Two Dimensional Arrays

Output:
51

Input:
class TwodimensionalStandard

public static void main(String args[])

int[][] a={{10,20},{30,40}};//declaration and initialization

System.out.println("Two dimensional array elements are");

System.out.println(a[0][0]);

System.out.println(a[0][1]);

System.out.println(a[1][0]);

System.out.println(a[1][1]);

}
52

Program 25: Alphabetic Order of Strings

Output:
53

Input:

class string{

static String name[] = {"Madras", "Delhi", "Chandigarh"};

public static void main (String args[ ]){

int size = name.length;

String temp = null;

for(int i=0;i<size;i++){

for(int j=i+1;j<size;j++)

if(name[j].compareTo(name[i]) <0)

temp = name[i];

name[i] = name[j];

name[j] = temp;

for(int i=0;i<size; i++)

System.out.println(name[i]);

}
54

Program 26: Working with Vectors and Array

Output:
55

Input:

import java.util.*;

public class array_to_vector {

public static void main(String[] args)

String[] arr = { "I", "love", "geeks", "for", "geeks" };

Vector<String> v = new Vector<String>();

Collections.addAll(v, arr);

System.out.println("The vector is");

System.out.println(v);

}
56

Program 27: Wrapper Class

Output:
57

Input:

class Main {

public static void main(String[] args) {

int a = 5;

double b = 5.65;

Integer aObj = Integer.valueOf(a);

Double bObj = Double.valueOf(b);

if(aObj instanceof Integer) {

System.out.println("An object of Integer is created.");

if(bObj instanceof Double) {

System.out.println("An object of Double is created.");

}
58

Program 28: Using a Package

Output:
59

Input:

package mypack;
class Balance
{
String name;
double bal;
Balance(String n,double b)
{
name=n;
bal=b;
}
void show()
{
if(bal<0)
System.out.print("-->");
System.out.println(name + ": $" + bal);
}
}
class accb
{
public static void main(String args[])
{
Balance current[]=new Balance[3];
current[0]=new Balance("K. J. Fielding", 123.23);
current[1]=new Balance("Will Tell", 157.02);
current[2]=new Balance("Tom Jackson", -12.33);
for(int i=0;i<3;i++)
current[i].show();
}
}
}
60

Program 29: Implementing Multiple Inheritance

Output:
61

Input:

class box{ {
private double width; double cost;
private double height; ship(ship s)
private double depth; {
box(box b1){ super(s);
width=b1.width; cost=s.cost;
height=b1.height; }
depth=b1.depth; ship(double w,double h,double d,double
} m,double c)
box(double w,double h,double d){ {
width=w; super(w,h,d,m);
height=h; cost=c;
depth=d; }
} ship()
box(){ {
width=-1; super();
height=-1; cost=-1;
depth=-1; }
} ship(double len,double m,double c)
box(double len){ {
width=height=depth=len; super(len,m);
} cost=c;
double volume(){ }
return width*height*depth; }
} class ships
} {
class boxw extends box{ public static void main(String args[])
double weight; {
boxw(boxw b){ ship s1=new ship(10,1,1);
super(b); ship s2=new ship(20,2,2);
weight=b.weight; double vol;
} vol=s1.volume();
boxw(double w,double h,double d,double System.out.println("Volume of s1 is
m) "+vol);
{ System.out.println("Weight of s1 is
super(w,h,d); "+s1.weight);
weight=m; System.out.println("Shipping cost
} "+s1.cost);
boxw() System.out.println();
{ vol=s2.volume();
super(); System.out.println("Volume of s2 is
weight=-1; "+vol);
} System.out.println("Weight of s2 is
boxw(double len,double m){ "+s2.weight);
super(len); System.out.println("Shipping cost
weight=m; "+s2.cost);
} System.out.println();
} }
class ship extends boxw }
62

Program 30: Implementing Interface

Output:
63

Input:

interface in

final int a=10;

static void disp()

System.out.println("Hello,I am static method");

default void disp2()

System.out.println("Hello,I am default method");

class in1 implements in

public static void main(String args[])

in i=new in1();

in.disp();

i.disp2();

}
64

Program 31: Creating Threads using Thread Class

Output:
65

Input:

class A extends Thread


{
public void run()
{
for (int i=1; i<=5; i++)
{
System.out.println("\t From ThreadA : i = " +i) ;
}
System.out.println("Exit From A ");
}
}
class B extends Thread
{
public void run()
{
for(int j=1;j<=5; j++)
{
System.out.println("\tFrom Thread B := " + 5);
}
System.out.println("Exit from B ");
}
}
class C extends Thread
{
public void run()
{
for (int k=1; k<=5; k++)
{
System.out.println("\tFrom Thread C : = " + k);
}
System.out.println("Exit from C ");
}
}
class ThreadTest
{
public static void main(String args[])
{
new A( ).start();
new B( ).start();
new C( ).start();
}
}
66

Program 32: Thread Method

Output:
67

Input:

class A extends Thread{


public void run(){
for (int i=1; i<=5; i++){
if(i==1) yield( );
System.out.println("\t From Thread A : i = " +i);
}
System.out.println("exit from A") ;
}
}
class B extends Thread {
public void run(){
for (int j=1; j<=5; j++){
System.out.println("\tFrom Thread B: j = " +j);
if (j==3) stop();
}
System.out.println("Exit from B. ");
}
}
class C extends Thread {
public void run(){
for (int k=1; k<=5; k++){
System.out.println("\tFrom Thread C: k = " +k) ;
if (k==1)
try{
sleep (1000);
}
catch (Exception e)
{
}
}
System.out.println( "Exit from C ");}
}
class time
{
public static void main(String args[ ]){
A threadA = new A( );
B threadB =new B( );
C threadC =new C( );
System.out.println("Start thread A");
threadA.start( );
System.out.println("Start thread B");
threadB.start( );
System.out.println("Start thread C");
threadC.start( );
System.out.println("End of main thread");
}
68

Program 33: Priority in Thread

Output:
69

Input:

class A extends Thread{


public void run(){
System.out.println("threadA started");
for (int i=1; i<=4; i++){
System.out.println("\tFrom Thread A : i = " +i);
}
System.out.println("Exit from A ");
}
}
class B extends Thread{
public void run(){
System.out.println("threads started");
for (int j=1; j<=4; j++) {
System.out.println("\tFrom Thread B: j = " +j) ;
}
System.out.println("Exit from B");
}
}
class C extends Thread{
public void run()
{
System.out.println("thread started");
for (int k=1; k<=4; k++)
{
System.out.println("\tFrom Thread C : k = " + k);
}
System.out.println("Exit from C");
}
}
class ThreadPriority{
public static void main(String args[]){
A threadA = new A( );
B threadB = new B( );
C threadC = new C( );
threadC.setPriority(Thread.MAX_PRIORITY);
threadB.setPriority(threadA.getPriority( )+1);
threadA.setPriority(Thread.MIN_PRIORITY);
System.out.println("Start thread A");
threadA.start();
System.out.println("Start thread B");
threadB.start();
System.out.println("Start thread C");
threadC.start();
System.out.println("End of main thread");
}
}
70

Program 35: Using Runnable Interface

Output:
71

Input:
class X implements Runnable{

public void run(){

for (int i = 1; i<=10; i++)

System.out.println("\tThreadX " +i);

System.out.println("End of ThreadX");

class RunnableTest

public static void main(String args[])

X runnable = new X( );

Thread threadX = new Thread ( runnable);

threadX. start();

System.out.println("End of main Thread");

}
72

Program 35: Run Time Error

Output:
73

Input:

class exe

public static void main(String args[])

int c;

try

c=10/0;

System.out.println(c);

catch(ArithmeticException e)

System.out.println("Division by zero not allowed" +e);

finally

System.out.println("Do it again");

}
74

Program 36: Catch for exception handling

Output:
75

Input:

class exe2
{
public static void main(String args[])
{
int a[]={10,10,10};
int b=2;
try
{
int x=a[3]/b;
x=a[3];
b=Integer.parseInt("aa");
}
catch(ArithmeticException e)
{
System.out.println("Division by zero");
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("Array index exception");
}
catch(NumberFormatException e)
{
System.out.println("Number Format Exception");
}
finally
{
System.out.println("Finally is executed");
}
int y=a[1]/10;
System.out.println("Y="+y);
}
}
76

Program 37: Multiple Catch Block

Output:
77

Input:

class exe3

public static void main(String args[])

int a[]={10,10};

int b=10;

try

try{

int x=a[0]/(b-a[0]);

catch(ArithmeticException e){

System.out.println("Division by zero");

int x=a[2]/b-a[1];

catch(ArrayIndexOutOfBoundsException e)

System.out.println("Finally is executed");

int y=a[1]/a[0];

System.out.println("Y="+y);

}
78

Program 38: Throwing our own Exception

Output:
79

Input:
import java.io.*;

class throw1

public static void main(String args[]) throws Exception

displayMethod();

static void displayMethod() throws Exception

BufferedReader br=

new BufferedReader(new InputStreamReader(System.in));

System.out.println("Enter your Name :");

String name=br.readLine();

System.out.println("Your Name is :" +name);

}
80

Program 39: Applet

Output:
81

Input:

import java.awt.*;

import java.applet.Applet;

public class app3 extends Applet

public void paint(Graphics g)

g.setColor(Color.red);

g.setFont(new Font("CASTELLAR",Font.BOLD,24));

g.drawString("Welcome to the word applets",5,25);

Font font=new Font("Verdana",Font.BOLD+Font.ITALIC,18);

g.setColor(new Color(192,182,200));

g.setFont(font);

g.drawString("The Graphical Interface",5,60);

}
82

Program 40: Changing Font Color in Applet

Output:
83

Input:
import java.awt.*;

import javax.swing.*;

public class App extends JApplet

private int x;

public void paint(Graphics g)

// simple text displayed on applet

g.setColor(Color.white);

g.fillRect(0, 0, 200, 100);

g.setColor(Color.black);

g.drawString("Sample Applet", 20, 20);

g.setColor(Color.blue);

g.drawString("created by BlueJ", 20, 40);

}
84

Program 41: Polygon and Arc

Output:
85

Input:

import java.awt.*; import java.awt.*;

import java.applet.*;
import java.applet.*;

public class Poly extends Applet


public class Face extends Applet{
{
public void paint (Graphics g)
int x1 [] = {20, 120, 220, 201};

{
int y1 [] = {20, 129, 20, 20};

int n1=4; g.drawOval (40, 40, 120, 150);

int x2 [ ] = {120, 220, 220, 120};

int y2 [ ] = {120, 20, 220, 120};


g.fillOval (68, 81, 10, 10);
int n2 = 4;
g.fillOval (121, 81, 10, 10);
public void paint (Graphics g)

g.drawOval (85, 100, 30, 30);


{

g.drawPolygon (x1, y1, n1); g.fillArc(60, 125, 80, 40, 180,180);

g.fillPolygon (x2, y2 , n2);


g.drawOval (25, 92, 15, 30);

}
g.drawOval (160, 92, 15, 30);
}
}

}
86

Program 42: Draw Line and Rectangle

Output:
87

Input:

import java.applet.Applet;

import java.awt.Graphics;

import java.awt.Color;

/*

* <applet code="app3.class" width="200" height="50">

* <param name="Name" value="Student!">

* <param name="Text" value="Welcome to Applet Parameters!">

* </applet>

public class app4 extends Applet

String s1,s2;

public void init()

s1=getParameter("Name");

public void paint(Graphics g)

g.setColor(Color.red);

s2=getParameter("Text");

g.drawString(s1,10,20);

g.drawString(s2,10,35);

}
88

Program 43: Line Graph

Output:
89

Input:

import java.awt.*;

import java.applet.*;

public class TableGraph extends Applet

int x [ ] = {0, 60, 120, 180, 240, 300, 360, 400};

int y [ ] = {400, 280, 220, 140, 60, 60, 100, 220};

int n = x.length;

public void paint (Graphics g)

g.drawPolygon(x, y, n);

}
90

Program 44: Label,TextField,Button Control and Radio Button

Output:
91

Input:

import java.awt.*;
import java.applet.*;

/*
* <applet Code=UserValid.class width=400 height=150>
* </applet>
*/
public class app6 extends Applet
{
Label lblName,lblPasswd;
TextField txtName,txtPasswd,txt3;
Button btnLogin,btnCancel;
public void init()
{
String msg="";
Checkbox c1,c2,c3,c4,c5;
CheckboxGroup cg;
lblName=new Label("User Name");
lblPasswd=new Label("Password");
txtName=new TextField(10);
txtPasswd=new TextField(10);
txtPasswd.setEchoChar('*');
btnLogin=new Button("Login");
btnCancel=new Button("Cancel");
cg=new CheckboxGroup();
c1=new Checkbox("10th",cg,true);
c2=new Checkbox("12th",cg,false);
c3=new Checkbox("Graduate",cg,false);
c4=new Checkbox("Post Graduate",cg,false);
c5=new Checkbox("Ph.D",cg,false);
add(lblName);
add(txtName);
add(lblPasswd);
add(txtPasswd);
add(btnLogin);
add(btnCancel);
add(c1);
add(c2);
add(c3);
add(c4);
add(c5);

}
}
92

Program 45: Checkbox

Output:
93

Input:

public void actionPerformed(ActionEvent e1)

if(e1.getSource()==b1)

setBackground(Color.red);

if(e1.getSource()==b2)

setBackground(Color.yellow);

if(e1.getSource()==b3)

setBackground(Color.blue);

if(e1.getSource()==b4)

setBackground(Color.green);

}
94

Program 46: User input through BufferReader

Output:
95

Input:

import java.io.*;

class MyUserInput

public static void main(String[] args) throws IOException

{String str1,str2,str3;

InputStreamReader isr1 = new InputStreamReader(System.in);

BufferedReader br1 = new BufferedReader(isr1);

System.out.println("Enter String value");

str1=br1.readLine();

System.out.println("The string is: "+ str1);

System.out.println("Enter a Character");

char ch1=(char)br1.read();

System.out.println( "The character value is: "+ch1);

System.out.println("\n Enter Numeric value");

str2=br1.readLine();

str2=br1.readLine();

int ino=Integer.parseInt(str2);

System.out.println("The numeric value is: "+ ino);

System.out.println("\n Enter Float value");

str3= br1.readLine();

float fno=Float.parseFloat(str3);

System.out.println("The float value is: "+ fno);

}
96

Program 47: User of Scanner class to read Standard Input

Output:
97

Input:

import java.util.Scanner;
class ScannerInput
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Write a number: ");
int number=sc.nextInt();
String newline1=sc.nextLine(); // to consume the in character
System.out.println("It is to consume newline character after number "+number);
System.out.print( "Text 1: ");
String txt1=sc.nextLine();
System.out.print("Text2: ");
String txt2=sc.nextLine();
System.out.println("Write an integer: ");
int number1=sc.nextInt();
System.out.println("Write a float value");
float num=sc.nextFloat();
System.out.println("Write a double value: ");
double numl=sc.nextDouble();
System.out.println("The int value is number1+ float value is "+num+
"double value is "+num);
Scanner sc1= new Scanner("23,34,56");
sc1.useDelimiter(",");
System.out.println("number is "+sc1.nextInt());
System.out.println("number is "+sc1.nextInt());
System.out.println("number is "+sc1.nextInt());
}
}
98

Program 48: User of DataInputStream to Input Values

Output:

Input:
99

import java.io.*;

public class DISDemo

public static void main(String[] s) throws Exception

DataInputStream dis = new DataInputStream(System.in);

System.out.println("Enter your name: ");

String str1 = dis.readLine();

System.out.println("Entered name is " + str1);

System.out.println("Roll No: ");

String str2= dis.readLine();

int x=Integer.parseInt(str2);

System.out.println("Enter marks: ");

String str3=dis.readLine();

double y=Double.parseDouble(str3);

System.out.println("Marks of Roll No "+str2+" having name "+ str1+ " is " + str3);

dis.close();

You might also like