About The Author

This is a sample info about the author. Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Quisque sed felis.

Get The Latest News

Sign up to receive latest news

Wednesday 24 August 2011

Five Ways to Take Input in Java

Program 1: import java.io.*;
public class Program1
{
public static void main(String ar[])
{
int a,b,c;
DataInputStream ds=new DataInputStream(System.in);
try
{
System.out.printf("Enter the number : ");
a=Integer.parseInt(ds.readLine());
System.out.printf("Enter the number : ");
b=Integer.parseInt(ds.readLine());
c=a+b;
System.out.format("The sum of %d and %d is %d",a,b,c);
}
catch(Exception e)
{
}
}
}

Program 2:
import java.io.*;
public class Program2
{
public static void main(String ar[])throws IOException
{
int a,b,c;
InputStreamReader str=new InputStreamReader(System.in);
BufferedReader bfr=new BufferedReader(str);
System.out.printf("Enter the number : ");
a=Integer.parseInt(bfr.readLine());
System.out.printf("Enter the number : ");
b=Integer.parseInt(bfr.readLine());
c=a+b;
System.out.format("The sum of %d and %d is %d",a,b,c);
}
}

Program 3:
import java.io.*;
public class Program3
{
public static void main(String ar[])throws IOException
{
int a,b,c;
DataInputStream ds=new DataInputStream(System.in);
System.out.printf("Enter te number : ");
a=Integer.parseInt(ds.readLine());
System.out.printf("Enter the number : ");
b=Integer.parseInt(ds.readLine());
c=a+b;
System.out.format("The sum of %d and %d is %d",a,b,c);
}
}

Program 4:
import java.util.*;
public class Program4
{
public static void main(String ar[])
{
Scanner sc=new Scanner(System.in);
int a;
String str=new String();
float f;
System.out.print("Enter the integer value : ");
a=sc.nextInt();
System.out.print("Enter the String value : ");
str=sc.nextLine();
System.out.print("Enter the float value : ");
f=sc.nextFloat();
System.out.printf("The value 0f integer is %d \nThe value of float is %f\n The value of string is %s",a,f,str);
}
}

Program 5:
import javax.swing.*;
public class Program5
{
public static void main(String[] args)
{
String a;
a=JOptionPane.showInputDialog(null, "Enter some text : ");
JOptionPane.showMessageDialog(null,a);
}
}

0 comments:

Post a Comment