SlideShare a Scribd company logo
1 of 9
Download to read offline
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

Similar to Howto get input with java

Javaa. The mean of a list of numbers is its arithmetic average. T.pdf
Javaa. The mean of a list of numbers is its arithmetic average. T.pdfJavaa. The mean of a list of numbers is its arithmetic average. T.pdf
Javaa. The mean of a list of numbers is its arithmetic average. T.pdfRAJATCHUGH12
 
So Far I have these two classes but I need help with my persontest c.pdf
So Far I have these two classes but I need help with my persontest c.pdfSo Far I have these two classes but I need help with my persontest c.pdf
So Far I have these two classes but I need help with my persontest c.pdfarihantgiftgallery
 
java programming cheatsheet
java programming cheatsheetjava programming cheatsheet
java programming cheatsheetBD AB
 
Lab01.pptx
Lab01.pptxLab01.pptx
Lab01.pptxKimVeeL
 
Review Questions for Exam 10182016 1. public class .pdf
Review Questions for Exam 10182016 1. public class .pdfReview Questions for Exam 10182016 1. public class .pdf
Review Questions for Exam 10182016 1. public class .pdfmayorothenguyenhob69
 
I need the code for a Java programming project. Allow the user to ty.pdf
I need the code for a Java programming project. Allow the user to ty.pdfI need the code for a Java programming project. Allow the user to ty.pdf
I need the code for a Java programming project. Allow the user to ty.pdfvenkatesh24685
 
Examf cs-cs141-2-17 solution
Examf cs-cs141-2-17 solutionExamf cs-cs141-2-17 solution
Examf cs-cs141-2-17 solutionFahadaio
 
Java fundamentals
Java fundamentalsJava fundamentals
Java fundamentalsHCMUTE
 
Java questionI am having issues returning the score sort in numeri.pdf
Java questionI am having issues returning the score sort in numeri.pdfJava questionI am having issues returning the score sort in numeri.pdf
Java questionI am having issues returning the score sort in numeri.pdfforwardcom41
 
Ifi7184 lesson3
Ifi7184 lesson3Ifi7184 lesson3
Ifi7184 lesson3Sónia
 
Lab101.pptx
Lab101.pptxLab101.pptx
Lab101.pptxKimVeeL
 
Integration Project Inspection 3
Integration Project Inspection 3Integration Project Inspection 3
Integration Project Inspection 3Dillon Lee
 

Similar to Howto get input with java (20)

Javaa. The mean of a list of numbers is its arithmetic average. T.pdf
Javaa. The mean of a list of numbers is its arithmetic average. T.pdfJavaa. The mean of a list of numbers is its arithmetic average. T.pdf
Javaa. The mean of a list of numbers is its arithmetic average. T.pdf
 
Sam wd programs
Sam wd programsSam wd programs
Sam wd programs
 
BIT211_2.pdf
BIT211_2.pdfBIT211_2.pdf
BIT211_2.pdf
 
Data structures
Data structuresData structures
Data structures
 
So Far I have these two classes but I need help with my persontest c.pdf
So Far I have these two classes but I need help with my persontest c.pdfSo Far I have these two classes but I need help with my persontest c.pdf
So Far I have these two classes but I need help with my persontest c.pdf
 
java programming cheatsheet
java programming cheatsheetjava programming cheatsheet
java programming cheatsheet
 
Java interface
Java interfaceJava interface
Java interface
 
06slide.ppt
06slide.ppt06slide.ppt
06slide.ppt
 
Lab01.pptx
Lab01.pptxLab01.pptx
Lab01.pptx
 
Review Questions for Exam 10182016 1. public class .pdf
Review Questions for Exam 10182016 1. public class .pdfReview Questions for Exam 10182016 1. public class .pdf
Review Questions for Exam 10182016 1. public class .pdf
 
I need the code for a Java programming project. Allow the user to ty.pdf
I need the code for a Java programming project. Allow the user to ty.pdfI need the code for a Java programming project. Allow the user to ty.pdf
I need the code for a Java programming project. Allow the user to ty.pdf
 
Examf cs-cs141-2-17 solution
Examf cs-cs141-2-17 solutionExamf cs-cs141-2-17 solution
Examf cs-cs141-2-17 solution
 
07-Basic-Input-Output.ppt
07-Basic-Input-Output.ppt07-Basic-Input-Output.ppt
07-Basic-Input-Output.ppt
 
Java fundamentals
Java fundamentalsJava fundamentals
Java fundamentals
 
54240326 (1)
54240326 (1)54240326 (1)
54240326 (1)
 
54240326 copy
54240326   copy54240326   copy
54240326 copy
 
Java questionI am having issues returning the score sort in numeri.pdf
Java questionI am having issues returning the score sort in numeri.pdfJava questionI am having issues returning the score sort in numeri.pdf
Java questionI am having issues returning the score sort in numeri.pdf
 
Ifi7184 lesson3
Ifi7184 lesson3Ifi7184 lesson3
Ifi7184 lesson3
 
Lab101.pptx
Lab101.pptxLab101.pptx
Lab101.pptx
 
Integration Project Inspection 3
Integration Project Inspection 3Integration Project Inspection 3
Integration Project Inspection 3
 

Recently uploaded

%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in sowetomasabamasaba
 
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...WSO2
 
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfonteinmasabamasaba
 
WSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go PlatformlessWSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go PlatformlessWSO2
 
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...masabamasaba
 
8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech studentsHimanshiGarg82
 
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...masabamasaba
 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplatePresentation.STUDIO
 
%in Harare+277-882-255-28 abortion pills for sale in Harare
%in Harare+277-882-255-28 abortion pills for sale in Harare%in Harare+277-882-255-28 abortion pills for sale in Harare
%in Harare+277-882-255-28 abortion pills for sale in Hararemasabamasaba
 
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburgmasabamasaba
 
The title is not connected to what is inside
The title is not connected to what is insideThe title is not connected to what is inside
The title is not connected to what is insideshinachiaurasa2
 
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...WSO2
 
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
Direct Style Effect Systems -The Print[A] Example- A Comprehension AidDirect Style Effect Systems -The Print[A] Example- A Comprehension Aid
Direct Style Effect Systems - The Print[A] Example - A Comprehension AidPhilip Schwarz
 
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park %in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park masabamasaba
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...panagenda
 
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...SelfMade bd
 
%in Benoni+277-882-255-28 abortion pills for sale in Benoni
%in Benoni+277-882-255-28 abortion pills for sale in Benoni%in Benoni+277-882-255-28 abortion pills for sale in Benoni
%in Benoni+277-882-255-28 abortion pills for sale in Benonimasabamasaba
 
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024VictoriaMetrics
 

Recently uploaded (20)

%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto
 
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
 
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
 
WSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go PlatformlessWSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go Platformless
 
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
 
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
 
8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students
 
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation Template
 
%in Harare+277-882-255-28 abortion pills for sale in Harare
%in Harare+277-882-255-28 abortion pills for sale in Harare%in Harare+277-882-255-28 abortion pills for sale in Harare
%in Harare+277-882-255-28 abortion pills for sale in Harare
 
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
 
The title is not connected to what is inside
The title is not connected to what is insideThe title is not connected to what is inside
The title is not connected to what is inside
 
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
 
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
 
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
Direct Style Effect Systems -The Print[A] Example- A Comprehension AidDirect Style Effect Systems -The Print[A] Example- A Comprehension Aid
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
 
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park %in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
 
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
 
%in Benoni+277-882-255-28 abortion pills for sale in Benoni
%in Benoni+277-882-255-28 abortion pills for sale in Benoni%in Benoni+277-882-255-28 abortion pills for sale in Benoni
%in Benoni+277-882-255-28 abortion pills for sale in Benoni
 
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
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); } }