Java4teachus

Saturday, May 11, 2019

Write a java program by Using Scanner Class

Reading the values from keyboard In java By Scanner Class


Q. Write a Java program which will read the 2 integer values Dynamically and add them.

Program:-


import java.util.Scanner;

class  Sum
{
public static void main(String[] args)
{
Scanner s=new Scanner(System.in);
System.out.println("Enter the First Value");
int x1=s.nextInt();
System.out.println("Enter the Second Value");
int x2=s.nextInt();
int x3=x1+x2;
System.out.println("sum="+x3);
}
}

Output:-

D:\java>javac Sum.java ↵

D:\java>Java Sum 
Enter the First value
12
Enter the Second value
12
Sum = 24

Scanner-Class-Programs-In-Java.jpeg


Q. Write a Java program which will read the 2 integer values Dynamically and Multiply them.


Program:-


import java.util.Scanner;

class  Multiply
{
public static void main(String[] args)
{
Scanner s=new Scanner(System.in);
System.out.println("Enter the First Value");
int x1=s.nextInt();
System.out.println("Enter the Second Value");
int x2=s.nextInt();
int x3=x1*x2;
System.out.println("Multiply="+x3);
}
}

Output:-

D:\java>javac  Mutliply.java ↵

D:\java>Java Mutliply
Enter the First value
2
Enter the Second value
2
Multiply = 4

Q. Write a Java program which will read the 2 String values Dynamically and concat them.


import java.util.Scanner;
class Names
{
public static void main(String[] args) 
{
Scanner s=new Scanner(System.in);
System.out.println("Enter the first name");
String a=s.nextLine();
System.out.println("Enter the Second name");
String b=s.nextLine();
String c=a+b;
System.out.println("Concat="+c);
}
}

Output:-

D:\java>javac Names .java ↵
D:\java>Java Names
Enter the First Name
Lucky
Enter the Second Name
Bujji
Concat=LuckyBujji

Q. Write a java program to display the student details with dynamically .


Program:-


import java.util.Scanner;

class Student
{
int studentid;
String name;
float marks;
String classname;
void studentInfo()
{
Scanner s=new Scanner(System.in);
System.out.println("Enter the Student ID");
studentid=Integer.parseInt(s.nextLine());
System.out.println("Enter the Student name");
name=s.nextLine();
System.out.println("Enter the Student Marks");
marks=Float.parseFloat(s.nextLine());
System.out.println("Enter the Student classname");
classname=s.nextLine();
}
void displayStudentInfo()
{
System.out.println("Enter the Student ID="+studentid);
System.out.println("Enter the Student Name="+name);
System.out.println("Enter the Student marks="+marks);
System.out.println("Enter the Student class="+classname);
}
}
class  StudentInfo
{
public static void main(String[] args) 
{
Student s1=new Student();
s1.studentInfo();
s1.displayStudentInfo();
}
}

No comments:

Post a Comment

Thanks for visiting my site..