SlideShare a Scribd company logo
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
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));
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);
}
}
}
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");
}
}
}
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);
}
}
}
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");
}
}
}
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" );
}
}
}
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;
}
}
}
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);
}
}

More Related Content

PPTX
Time-Based Blind SQL Injection
PPT
Time-Based Blind SQL Injection using Heavy Queries
DOCX
All Of My Java Codes With A Sample Output.docx
DOCX
Main Java[All of the Base Concepts}.docx
PPTX
JPC#8 Introduction to Java Programming
PPTX
Lecture 3 and 4.pptx
PDF
Integration Project Inspection 3
PDF
merged_document_3
Time-Based Blind SQL Injection
Time-Based Blind SQL Injection using Heavy Queries
All Of My Java Codes With A Sample Output.docx
Main Java[All of the Base Concepts}.docx
JPC#8 Introduction to Java Programming
Lecture 3 and 4.pptx
Integration Project Inspection 3
merged_document_3

Similar to Howto get input with java (20)

PPTX
Reading and writting
PDF
PSI 3 Integration
PPTX
problem based task oop
DOCX
9 11 25 14 44 6 41 15 57 9 39 16 41 2 58 8 43 12 4.docx
PDF
Question Hello, I need some assistance in writing a java pr...Hel.pdf
PDF
Sam wd programs
DOCX
PPTX
130706266060138191
PDF
Lecture 2 java.pdf
PPTX
Java Programming Tutorials Basic to Advanced 2
PPT
Introduction to Java Programming Part 2
PPTX
Reading and writting v2
PPT
Ch02 primitive-data-definite-loops
DOCX
100 Small programs
PPT
ch02-primitive-data-definite-loops.ppt
PPT
ch02-primitive-data-definite-loops.ppt
PPTX
Java notes 1 - operators control-flow
PDF
03-Primitive-Datatypes.pdf
PDF
Microsoft word java
PPTX
02slide_accessible.pptx
Reading and writting
PSI 3 Integration
problem based task oop
9 11 25 14 44 6 41 15 57 9 39 16 41 2 58 8 43 12 4.docx
Question Hello, I need some assistance in writing a java pr...Hel.pdf
Sam wd programs
130706266060138191
Lecture 2 java.pdf
Java Programming Tutorials Basic to Advanced 2
Introduction to Java Programming Part 2
Reading and writting v2
Ch02 primitive-data-definite-loops
100 Small programs
ch02-primitive-data-definite-loops.ppt
ch02-primitive-data-definite-loops.ppt
Java notes 1 - operators control-flow
03-Primitive-Datatypes.pdf
Microsoft word java
02slide_accessible.pptx
Ad

Recently uploaded (20)

PPTX
Online Work Permit System for Fast Permit Processing
PPTX
ManageIQ - Sprint 268 Review - Slide Deck
PPTX
Materi-Enum-and-Record-Data-Type (1).pptx
PPTX
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
PDF
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
PPTX
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
PPTX
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
PDF
Adobe Premiere Pro 2025 (v24.5.0.057) Crack free
PPTX
CHAPTER 12 - CYBER SECURITY AND FUTURE SKILLS (1) (1).pptx
PDF
Complete React Javascript Course Syllabus.pdf
PDF
Upgrade and Innovation Strategies for SAP ERP Customers
PDF
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
PPTX
Materi_Pemrograman_Komputer-Looping.pptx
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 41
PPTX
ISO 45001 Occupational Health and Safety Management System
PDF
Understanding Forklifts - TECH EHS Solution
PDF
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
DOCX
The Five Best AI Cover Tools in 2025.docx
PPTX
Odoo POS Development Services by CandidRoot Solutions
PDF
How Creative Agencies Leverage Project Management Software.pdf
Online Work Permit System for Fast Permit Processing
ManageIQ - Sprint 268 Review - Slide Deck
Materi-Enum-and-Record-Data-Type (1).pptx
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
Adobe Premiere Pro 2025 (v24.5.0.057) Crack free
CHAPTER 12 - CYBER SECURITY AND FUTURE SKILLS (1) (1).pptx
Complete React Javascript Course Syllabus.pdf
Upgrade and Innovation Strategies for SAP ERP Customers
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
Materi_Pemrograman_Komputer-Looping.pptx
Internet Downloader Manager (IDM) Crack 6.42 Build 41
ISO 45001 Occupational Health and Safety Management System
Understanding Forklifts - TECH EHS Solution
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
The Five Best AI Cover Tools in 2025.docx
Odoo POS Development Services by CandidRoot Solutions
How Creative Agencies Leverage Project Management Software.pdf
Ad

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: [email protected] | 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: [email protected] | 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: [email protected] | 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: [email protected] | 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: [email protected] | 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: [email protected] | 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: [email protected] | 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: [email protected] | 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: [email protected] | 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); } }