Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Howto get input with java
1. U s e r I n p u t I n J a v a P a g e | 1
_____________________________________________________________________________________________
Prepared By: Syed Faizan ul Hassan | Student: MCS | Preston University Islamabad | Website: syedhassan.com
Email: faizanulhassan@gmail.com | facebook.com/syedhassan01
෴اὺاশᒪاশᓇ
HOWTO GET INPUT FROM USERS
JAVA
PROGRAMMING
2. U s e r I n p u t I n J a v a P a g e | 2
_____________________________________________________________________________________________
Prepared By: Syed Faizan ul Hassan | Student: MCS | Preston University Islamabad | Website: syedhassan.com
Email: faizanulhassan@gmail.com | facebook.com/syedhassan01
SIMPLE INPUT & OUTPUT PROGRAM WITH JAVA
STEPS:
1: Import HeaderFile(s) Like C++ (line: 1)
2: Create Variable String Type (line: 5)
3: Create Class Object (line: 6)
4: Call Function for Reading inputline (line: 8)
5: Convert String Value to Integer if you need to input Integer/ numeric values (line: 9)
1. import java.io.*;
2. public class simpleInput {
3. public static void main(String[] args) throws IOException {
4. int a;
5. String strToint;
6. BufferedReader obj = new BufferedReader(new InputStreamReader(System.in));
7. System.out.println("Enter Value as Input");
8. strToint = obj.readLine();
9. a = Integer.parseInt(strToint);
10. System.out.println("Your Input is: "+a);
11. }
12. }
BufferedReader is a Parent Class with Two Child Classes and Functions
BufferedReader
OutputStreamReader InputStreamReader
System.out System.in
Syntax for Object:
Parent Class Name ObjectName = ParentClassName (new SubClassName (Function Name));
Object Creation:
BufferedReader obj = BufferedReader( new InputStreamReader( System.In));
3. U s e r I n p u t I n J a v a P a g e | 3
_____________________________________________________________________________________________
Prepared By: Syed Faizan ul Hassan | Student: MCS | Preston University Islamabad | Website: syedhassan.com
Email: faizanulhassan@gmail.com | facebook.com/syedhassan01
MAXIMUM NUMBER
import java.io.*;
import java.lang.*;
public class bigNumber {
public static void main(String[] args)throws IOException{
int a;
a=1;
int val1, val2, res;
String strToInt;
BufferedReader obj = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter First Value: ");
strToInt = obj.readLine();
val1 = Integer.parseInt(strToInt);
System.out.println("Enter Second Value: ");
strToInt = obj.readLine();
val2 = Integer.parseInt(strToInt);
System.out.println("Big Number form Value 1: "+val1+ " and value 2: " +val2);
if(val1>val2){
System.out.println( val1+" is Big then "+val2);
} else {
System.out.println(val2+" is Big then "+val1);
}
}
}
4. U s e r I n p u t I n J a v a P a g e | 4
_____________________________________________________________________________________________
Prepared By: Syed Faizan ul Hassan | Student: MCS | Preston University Islamabad | Website: syedhassan.com
Email: faizanulhassan@gmail.com | facebook.com/syedhassan01
EVEN ODD NUMBER
import java.io.*;
import java.lang.*;
public class evenOdd {
public static void main(String[] args) throws IOException{
int a;
int res;
String strToInt;
BufferedReader obj = new BufferedReader(new InputStreamReader(System.in));
strToInt = obj.readLine();
a = Integer.parseInt(strToInt);
res = a%2;
System.out.println("Even/ ODD Numbers");
if(res==0){
System.out.println("Number is: "+a+ " and " +a+ " is Even Number");
}else{
System.out.println("Number is: "+a+ " and " +a+ " is ODD Number");
}
}
}
5. U s e r I n p u t I n J a v a P a g e | 5
_____________________________________________________________________________________________
Prepared By: Syed Faizan ul Hassan | Student: MCS | Preston University Islamabad | Website: syedhassan.com
Email: faizanulhassan@gmail.com | facebook.com/syedhassan01
FACTORIAL
import java.io.*;//import Input OutPut Stream Files
import java.lang.*;// import Language Files
public class factorial {
public static void main(String[] args)throws IOException {
int i;
int num;
int fact=1;
String strToInt;//String to Integer Converting Variable
BufferedReader obj = new BufferedReader(new InputStreamReader(System.in));//Creating Object of
Parint and Child Class
strToInt = obj.readLine();//Readline for user input
num = Integer.parseInt(strToInt);// Convert Input in Integer to calculate
for(i=1;i<=num;i++){
fact = fact*i;
System.out.println(fact);
}
}
}
6. U s e r I n p u t I n J a v a P a g e | 6
_____________________________________________________________________________________________
Prepared By: Syed Faizan ul Hassan | Student: MCS | Preston University Islamabad | Website: syedhassan.com
Email: faizanulhassan@gmail.com | facebook.com/syedhassan01
GRADE CALCULATION
import java.io.*;
import java.lang.*;
public class gradeCalculation {
public static void main(String[] args)throws IOException {
int marks;
String strToInt;
BufferedReader obj = new BufferedReader(new InputStreamReader(System.in));
strToInt = obj.readLine();
marks=Integer.parseInt(strToInt);
System.out.println("Grade Calculation");
if(marks>=90 && marks<=100){
System.out.println("Invalid Number");
}else if(marks>=80 && marks<=90){
System.out.println("Your Grade Is: A");
}else if(marks>=70 && marks<=80){
System.out.println("Your Grade Is: B");
}else if(marks>=60 && marks<=70){
System.out.println("Your Grade Is: C");
}else if(marks>=50 && marks<=60){
System.out.println("Your Grade Is: D");
}else{
System.out.println("Fail");
}
}
}
7. U s e r I n p u t I n J a v a P a g e | 7
_____________________________________________________________________________________________
Prepared By: Syed Faizan ul Hassan | Student: MCS | Preston University Islamabad | Website: syedhassan.com
Email: faizanulhassan@gmail.com | facebook.com/syedhassan01
PRIME NUMBER
import java.io.*;
import java.lang.*;
public class primeNumber {
public static void main(String[] args)throws IOException{
int num;
String strToInt;
BufferedReader obj = new BufferedReader(new InputStreamReader(System.in));
strToInt = obj.readLine();
num = Integer.parseInt(strToInt);
int flag=0;
for(int i=2; i<num; i++){
if(num%i==0){
flag=1;
break;
}
}
if(flag==0){
System.out.println(num+ " is a prime number" );
}else{
System.out.println(num+ " is not a prime number" );
}
}
}
8. U s e r I n p u t I n J a v a P a g e | 8
_____________________________________________________________________________________________
Prepared By: Syed Faizan ul Hassan | Student: MCS | Preston University Islamabad | Website: syedhassan.com
Email: faizanulhassan@gmail.com | facebook.com/syedhassan01
CALCULATOR WITH SWITCH STATMENT
import java.io.*;
import java.lang.*;
public class switchStatment {
public static void main(String[] args)throws IOException {
int cho;
int val1, val2, res;
String strToInt;
BufferedReader obj = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter First Value");
strToInt = obj.readLine();
val1 = Integer.parseInt(strToInt);
System.out.println("Enter Second Value");
strToInt = obj.readLine();
val2 = Integer.parseInt(strToInt);
System.out.println("Select Operation: n 1= +n 2= -n 3= *n 4= /");
strToInt = obj.readLine();
cho = Integer.parseInt(strToInt);
switch(cho){
case 1:
res= val1+val2;
System.out.println("The Sum of " +val1+" and "+val2+" is: "+res);
break;
case 2:
res= val1-val2;
System.out.println("The subtrection of "+val1+" and "+val2+" is: "+res);
break;
case 3:
res= val1*val2;
System.out.println("The multiplication of "+val1+" and "+val2+" is: "+res);
break;
case 4:
res= val1/val2;
System.out.println("The multiplication of "+val1+" and "+val2+" is: "+res);
break;
}
}
}
9. U s e r I n p u t I n J a v a P a g e | 9
_____________________________________________________________________________________________
Prepared By: Syed Faizan ul Hassan | Student: MCS | Preston University Islamabad | Website: syedhassan.com
Email: faizanulhassan@gmail.com | facebook.com/syedhassan01
TABLE WITH FOR, WHILE, DO-WHILE LOOPS
import java.io.*;
import java.lang.*;
public class tableWithLoop {
public static void main(String[] args)throws IOException {
int num;
String strToInt;
int i;
i=1;
System.out.println("Table with For Loop");
System.out.println("Enter Number for Table");
BufferedReader obj = new BufferedReader(new InputStreamReader(System.in));
strToInt = obj.readLine();
num = Integer.parseInt(strToInt);
for(i=1; i<=10; i++){
System.out.println(num+" x "+ i +" = "+ num*i);
System.out.println("_____________________");
}
System.out.println("Table with While Loop");
System.out.println("Enter Number for Table");
strToInt = obj.readLine();
num = Integer.parseInt(strToInt);
i=1;
while(i<=10){
System.out.println(num+" x "+ i +" = "+ num*i);
System.out.println("_____________________");
i++;
}
i=1;
System.out.println("Table with DO While Loop");
System.out.println("Enter Number for Table");
strToInt = obj.readLine();
num = Integer.parseInt(strToInt);
do{
System.out.println(num+" x "+ i +" = "+ num*i);
System.out.println("_____________________");
i++;
}while(i<=10);
}
}