SlideShare ist ein Scribd-Unternehmen logo
1 von 13
Taking InputTaking Input
JavaJava
Md. Eftakhairul IslamMd. Eftakhairul Islam
eftakhairul@gmail.comeftakhairul@gmail.com
http://eftakhairul.wordpress.comhttp://eftakhairul.wordpress.com
Scanner ClassScanner Class
The Scanner class is a class inThe Scanner class is a class in
java.util, which allows the user tojava.util, which allows the user to
read values of various types by itsread values of various types by its
method.method.
In Java documentationIn Java documentation
Link:Link: http://java.sun.com/j2se/1.5.0/docs/api/java/util/Scanner.htmlhttp://java.sun.com/j2se/1.5.0/docs/api/java/util/Scanner.html
22http://eftakhairul.worldpress.comhttp://eftakhairul.worldpress.com
Scanner Class MethodsScanner Class Methods
BRAC University Computer ClubBRAC University Computer Club 33
Method Returns
int nextInt() Returns the next token as an int.
long nextLong() Returns the next token as a long
float nextFloat() Returns the next token as a float.
double nextDouble() Returns the next token as a long
String next() Finds and returns the next
complete token from this scanner
and returns it as a string; a token
is usually ended by whitespace
such as a blank or line break
String nextLine() Returns the rest of the current line,
excluding any line separator at the
end.
//Import the class//Import the class
import java.util.Scanner;import java.util.Scanner;
public class NumericInput{public class NumericInput{
public static void main(String[] args){public static void main(String[] args){
// Declarations// Declarations
Scanner in = new Scanner(System.in);Scanner in = new Scanner(System.in);
int integer = in.nextInt();int integer = in.nextInt();
long longInteger = in.nextLong();long longInteger = in.nextLong();
float realNumber = in.nextFloat();float realNumber = in.nextFloat();
double doubleReal = in.nextDouble();double doubleReal = in.nextDouble();
String stringLine = in.nextLine();String stringLine = in.nextLine();
String stringStoken= in.next();String stringStoken= in.next();
System.out.println("Enter an integer = “+ integer+”, a longSystem.out.println("Enter an integer = “+ integer+”, a long
integer =“+ longInteger +“,a floating-point = “+ doubleReal+”integer =“+ longInteger +“,a floating-point = “+ doubleReal+”
,and a string = “+ stringStoken+”, It will print whole line = “+,and a string = “+ stringStoken+”, It will print whole line = “+
stringLine);stringLine);
} }} }
44http://eftakhairul.worldpress.comhttp://eftakhairul.worldpress.com
Scanner Class ExampleScanner Class Example
55
BufferedReaderBufferedReader
import java.io.*;import java.io.*;
Public class Taking inputPublic class Taking input
{{
Public static void main(String [] args)Public static void main(String [] args) throws IOExceptionthrows IOException
{{
BufferedReader x = new BufferedReader(newBufferedReader x = new BufferedReader(new
InputStreamReader(System.in));InputStreamReader(System.in));
//// String box;String box;
//String box=x.readLine();//String box=x.readLine();
Converting x in IntegerConverting x in Integer
int n=Integer.parseInt(int n=Integer.parseInt(xx.readLine());.readLine());
http://eftakhairul.worldpress.comhttp://eftakhairul.worldpress.com
66
//Converting x in Float//Converting x in Float
float f=Float.parseFloat(x.readLine());float f=Float.parseFloat(x.readLine());
//Converting x in Double//Converting x in Double
double d=Double.parseDouble(x.readLine());double d=Double.parseDouble(x.readLine());
//Converting x in Long//Converting x in Long
long l=Long.parseLong(x.readLine());long l=Long.parseLong(x.readLine());
//Converting x in Byte//Converting x in Byte
byte b=Byte.parseByte(x.readLine());byte b=Byte.parseByte(x.readLine());
}}
}}
http://eftakhairul.worldpress.comhttp://eftakhairul.worldpress.com
77
JOptionPaneJOptionPane
//importing class from java//importing class from java
Import javax.swing.JOptionPane.*;Import javax.swing.JOptionPane.*;
Main()………Main()………
String s = JOptionPane.showInputDialog ("String s = JOptionPane.showInputDialog ("Enter anyEnter any
numbernumber");");
//Converting x in integer//Converting x in integer
int p = Integer.parseInt(x);int p = Integer.parseInt(x);
//Converting x in Double//Converting x in Double
double d = Double.parseDouble(x.readLine());double d = Double.parseDouble(x.readLine());
http://eftakhairul.worldpress.comhttp://eftakhairul.worldpress.com
Take input from fileTake input from file
// Declarations// Declarations
Scanner inFile = new Scanner(newScanner inFile = new Scanner(new
FileReader("File name with directories"));FileReader("File name with directories"));
//Read token by token from file//Read token by token from file
String stringStoken= inFile.next();String stringStoken= inFile.next();
//Read Line by Line from file//Read Line by Line from file
String stringLine = inFile.nextLine();String stringLine = inFile.nextLine();
88http://eftakhairul.worldpress.comhttp://eftakhairul.worldpress.com
Reading data from InternetReading data from Internet
 import java.net.*;import java.net.*;
 import java.io.*;import java.io.*;
 public class WebSiteReader {public class WebSiteReader {
 public static void main(String args[]){public static void main(String args[]){
 String nextLine;String nextLine;
 try{try{
 // Create the URL obect that points & at the default file index.html// Create the URL obect that points & at the default file index.html
 URL url = new URL("http://rainbd.freelance.com.bd/index.html" );URL url = new URL("http://rainbd.freelance.com.bd/index.html" );
 URLConnection urlConn = url.openConnection();URLConnection urlConn = url.openConnection();
 BufferedReader buff= new BufferedReader(newBufferedReader buff= new BufferedReader(new
InputStreamReader(urlConn.getInputStream()));InputStreamReader(urlConn.getInputStream()));
 // Read and print the lines from index.html// Read and print the lines from index.html
 while (true){while (true){
 nextLine =buff.readLine();nextLine =buff.readLine();
 if (nextLine !=null){if (nextLine !=null){
 System.out.println(nextLine);System.out.println(nextLine);
 }else{}else{
 break;break;
 }}
 }}
 } catch(Exception e){} catch(Exception e){
 System.out.println("Please read the exception carefully:" + e.toString());System.out.println("Please read the exception carefully:" + e.toString());
 } } }} } }
http://eftakhairul.worldpress.comhttp://eftakhairul.worldpress.com 99
1010
Difference between println & printDifference between println & print
System.out.println(“java”);System.out.println(“java”);
System.out.println(“world”);System.out.println(“world”);
>>
JavaJava
worldworld
>>
(Different line )(Different line )
http://eftakhairul.worldpress.comhttp://eftakhairul.worldpress.com
1111
System.out.print()System.out.print()
System.out.print(“java”);System.out.print(“java”);
System.out.print(“world”);System.out.print(“world”);
>>
javaworld>javaworld>
(Same line)(Same line)
http://eftakhairul.worldpress.comhttp://eftakhairul.worldpress.com
1212
out.println( );out.println( );
import static java.lang.System.out;import static java.lang.System.out;
main()…….main()…….
out.println(“shortcut of println”);out.println(“shortcut of println”);
out.print(“shortcut of print”);out.print(“shortcut of print”);
http://eftakhairul.worldpress.comhttp://eftakhairul.worldpress.com
1313
…….Thank You…..Thank You….
http://eftakhairul.worldpress.comhttp://eftakhairul.worldpress.com

Weitere ähnliche Inhalte

Was ist angesagt?

Understanding java streams
Understanding java streamsUnderstanding java streams
Understanding java streamsShahjahan Samoon
 
L21 io streams
L21 io streamsL21 io streams
L21 io streamsteach4uin
 
java: basics, user input, data type, constructor
java:  basics, user input, data type, constructorjava:  basics, user input, data type, constructor
java: basics, user input, data type, constructorShivam Singhal
 
Java scanner, everything you need to know about Java Scanner
Java scanner, everything you need to know about Java ScannerJava scanner, everything you need to know about Java Scanner
Java scanner, everything you need to know about Java ScannerEdward Nyang'ali
 
Chapter 12 - File Input and Output
Chapter 12 - File Input and OutputChapter 12 - File Input and Output
Chapter 12 - File Input and OutputEduardo Bergavera
 
Handling inputs via scanner class
Handling inputs via scanner classHandling inputs via scanner class
Handling inputs via scanner classsimarsimmygrewal
 
java.io - streams and files
java.io - streams and filesjava.io - streams and files
java.io - streams and filesMarcello Thiry
 
Input output files in java
Input output files in javaInput output files in java
Input output files in javaKavitha713564
 
IO In Java
IO In JavaIO In Java
IO In Javaparag
 
Java Input Output (java.io.*)
Java Input Output (java.io.*)Java Input Output (java.io.*)
Java Input Output (java.io.*)Om Ganesh
 
Java Course 8: I/O, Files and Streams
Java Course 8: I/O, Files and StreamsJava Course 8: I/O, Files and Streams
Java Course 8: I/O, Files and StreamsAnton Keks
 

Was ist angesagt? (20)

Understanding java streams
Understanding java streamsUnderstanding java streams
Understanding java streams
 
JAVA BASICS
JAVA BASICSJAVA BASICS
JAVA BASICS
 
L21 io streams
L21 io streamsL21 io streams
L21 io streams
 
Java Streams
Java StreamsJava Streams
Java Streams
 
java: basics, user input, data type, constructor
java:  basics, user input, data type, constructorjava:  basics, user input, data type, constructor
java: basics, user input, data type, constructor
 
Java scanner, everything you need to know about Java Scanner
Java scanner, everything you need to know about Java ScannerJava scanner, everything you need to know about Java Scanner
Java scanner, everything you need to know about Java Scanner
 
Java stream
Java streamJava stream
Java stream
 
Java I/O
Java I/OJava I/O
Java I/O
 
Streams in Java 8
Streams in Java 8Streams in Java 8
Streams in Java 8
 
Chapter 12 - File Input and Output
Chapter 12 - File Input and OutputChapter 12 - File Input and Output
Chapter 12 - File Input and Output
 
Handling inputs via scanner class
Handling inputs via scanner classHandling inputs via scanner class
Handling inputs via scanner class
 
java.io - streams and files
java.io - streams and filesjava.io - streams and files
java.io - streams and files
 
Input output files in java
Input output files in javaInput output files in java
Input output files in java
 
IO In Java
IO In JavaIO In Java
IO In Java
 
Java IO
Java IOJava IO
Java IO
 
Java Input Output (java.io.*)
Java Input Output (java.io.*)Java Input Output (java.io.*)
Java Input Output (java.io.*)
 
Handling I/O in Java
Handling I/O in JavaHandling I/O in Java
Handling I/O in Java
 
02 basic java programming and operators
02 basic java programming and operators02 basic java programming and operators
02 basic java programming and operators
 
Java I/O
Java I/OJava I/O
Java I/O
 
Java Course 8: I/O, Files and Streams
Java Course 8: I/O, Files and StreamsJava Course 8: I/O, Files and Streams
Java Course 8: I/O, Files and Streams
 

Andere mochten auch

Andere mochten auch (6)

Java lesson khmer
Java lesson khmerJava lesson khmer
Java lesson khmer
 
Learn Java Part 5
Learn Java Part 5Learn Java Part 5
Learn Java Part 5
 
Learn Java Part 4
Learn Java Part 4Learn Java Part 4
Learn Java Part 4
 
RUP
RUPRUP
RUP
 
Real Numbers
Real NumbersReal Numbers
Real Numbers
 
Real numbers
Real numbersReal numbers
Real numbers
 

Ähnlich wie Taking User Input in Java

Java Notes by C. Sreedhar, GPREC
Java Notes by C. Sreedhar, GPRECJava Notes by C. Sreedhar, GPREC
Java Notes by C. Sreedhar, GPRECSreedhar Chowdam
 
The definitive guide to java agents
The definitive guide to java agentsThe definitive guide to java agents
The definitive guide to java agentsRafael Winterhalter
 
Java Advance 4deleteQuery.PNGJava Advance 4deleteQuerySucc.docx
Java Advance 4deleteQuery.PNGJava Advance 4deleteQuerySucc.docxJava Advance 4deleteQuery.PNGJava Advance 4deleteQuerySucc.docx
Java Advance 4deleteQuery.PNGJava Advance 4deleteQuerySucc.docxpriestmanmable
 
File Input and output.pptx
File Input  and output.pptxFile Input  and output.pptx
File Input and output.pptxcherryreddygannu
 
Frequency .java Word frequency counter package frequ.pdf
Frequency .java  Word frequency counter  package frequ.pdfFrequency .java  Word frequency counter  package frequ.pdf
Frequency .java Word frequency counter package frequ.pdfarshiartpalace
 
Reading and writting
Reading and writtingReading and writting
Reading and writtingandreeamolnar
 
Reading and writting v2
Reading and writting v2Reading and writting v2
Reading and writting v2ASU Online
 
4CS4-25-Java-Lab-Manual.pdf
4CS4-25-Java-Lab-Manual.pdf4CS4-25-Java-Lab-Manual.pdf
4CS4-25-Java-Lab-Manual.pdfamitbhachne
 
Java ppt Gandhi Ravi (gandhiri@gmail.com)
Java ppt  Gandhi Ravi  (gandhiri@gmail.com)Java ppt  Gandhi Ravi  (gandhiri@gmail.com)
Java ppt Gandhi Ravi (gandhiri@gmail.com)Gandhi Ravi
 
Java IO Streams V4
Java IO Streams V4Java IO Streams V4
Java IO Streams V4Sunil OS
 
Packages and inbuilt classes of java
Packages and inbuilt classes of javaPackages and inbuilt classes of java
Packages and inbuilt classes of javakamal kotecha
 
Java Program I keep receiving the following error in my code- Can you.pdf
Java Program I keep receiving the following error in my code- Can you.pdfJava Program I keep receiving the following error in my code- Can you.pdf
Java Program I keep receiving the following error in my code- Can you.pdfRyanF2PLeev
 

Ähnlich wie Taking User Input in Java (20)

CORE JAVA-1
CORE JAVA-1CORE JAVA-1
CORE JAVA-1
 
Java Notes by C. Sreedhar, GPREC
Java Notes by C. Sreedhar, GPRECJava Notes by C. Sreedhar, GPREC
Java Notes by C. Sreedhar, GPREC
 
Java Notes
Java Notes Java Notes
Java Notes
 
Basic of Javaio
Basic of JavaioBasic of Javaio
Basic of Javaio
 
Files io
Files ioFiles io
Files io
 
Java practical
Java practicalJava practical
Java practical
 
The definitive guide to java agents
The definitive guide to java agentsThe definitive guide to java agents
The definitive guide to java agents
 
java input & output statements
 java input & output statements java input & output statements
java input & output statements
 
Java Advance 4deleteQuery.PNGJava Advance 4deleteQuerySucc.docx
Java Advance 4deleteQuery.PNGJava Advance 4deleteQuerySucc.docxJava Advance 4deleteQuery.PNGJava Advance 4deleteQuerySucc.docx
Java Advance 4deleteQuery.PNGJava Advance 4deleteQuerySucc.docx
 
File Input and output.pptx
File Input  and output.pptxFile Input  and output.pptx
File Input and output.pptx
 
Frequency .java Word frequency counter package frequ.pdf
Frequency .java  Word frequency counter  package frequ.pdfFrequency .java  Word frequency counter  package frequ.pdf
Frequency .java Word frequency counter package frequ.pdf
 
Reading and writting
Reading and writtingReading and writting
Reading and writting
 
Reading and writting v2
Reading and writting v2Reading and writting v2
Reading and writting v2
 
4CS4-25-Java-Lab-Manual.pdf
4CS4-25-Java-Lab-Manual.pdf4CS4-25-Java-Lab-Manual.pdf
4CS4-25-Java-Lab-Manual.pdf
 
Java Language fundamental
Java Language fundamentalJava Language fundamental
Java Language fundamental
 
Java ppt Gandhi Ravi (gandhiri@gmail.com)
Java ppt  Gandhi Ravi  (gandhiri@gmail.com)Java ppt  Gandhi Ravi  (gandhiri@gmail.com)
Java ppt Gandhi Ravi (gandhiri@gmail.com)
 
Java IO Streams V4
Java IO Streams V4Java IO Streams V4
Java IO Streams V4
 
Packages and inbuilt classes of java
Packages and inbuilt classes of javaPackages and inbuilt classes of java
Packages and inbuilt classes of java
 
Lab4
Lab4Lab4
Lab4
 
Java Program I keep receiving the following error in my code- Can you.pdf
Java Program I keep receiving the following error in my code- Can you.pdfJava Program I keep receiving the following error in my code- Can you.pdf
Java Program I keep receiving the following error in my code- Can you.pdf
 

Kürzlich hochgeladen

psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docxPoojaSen20
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfagholdier
 
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Shubhangi Sonawane
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptxMaritesTamaniVerdade
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsTechSoup
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxRamakrishna Reddy Bijjam
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...Nguyen Thanh Tu Collection
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...christianmathematics
 
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-IIFood Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-IIShubhangi Sonawane
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.pptRamjanShidvankar
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxnegromaestrong
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfChris Hunter
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDThiyagu K
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxDenish Jangid
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin ClassesCeline George
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfAyushMahapatra5
 
Role Of Transgenic Animal In Target Validation-1.pptx
Role Of Transgenic Animal In Target Validation-1.pptxRole Of Transgenic Animal In Target Validation-1.pptx
Role Of Transgenic Animal In Target Validation-1.pptxNikitaBankoti2
 

Kürzlich hochgeladen (20)

psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docx
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-IIFood Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdf
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdf
 
Role Of Transgenic Animal In Target Validation-1.pptx
Role Of Transgenic Animal In Target Validation-1.pptxRole Of Transgenic Animal In Target Validation-1.pptx
Role Of Transgenic Animal In Target Validation-1.pptx
 

Taking User Input in Java

  • 1. Taking InputTaking Input JavaJava Md. Eftakhairul IslamMd. Eftakhairul Islam eftakhairul@gmail.comeftakhairul@gmail.com http://eftakhairul.wordpress.comhttp://eftakhairul.wordpress.com
  • 2. Scanner ClassScanner Class The Scanner class is a class inThe Scanner class is a class in java.util, which allows the user tojava.util, which allows the user to read values of various types by itsread values of various types by its method.method. In Java documentationIn Java documentation Link:Link: http://java.sun.com/j2se/1.5.0/docs/api/java/util/Scanner.htmlhttp://java.sun.com/j2se/1.5.0/docs/api/java/util/Scanner.html 22http://eftakhairul.worldpress.comhttp://eftakhairul.worldpress.com
  • 3. Scanner Class MethodsScanner Class Methods BRAC University Computer ClubBRAC University Computer Club 33 Method Returns int nextInt() Returns the next token as an int. long nextLong() Returns the next token as a long float nextFloat() Returns the next token as a float. double nextDouble() Returns the next token as a long String next() Finds and returns the next complete token from this scanner and returns it as a string; a token is usually ended by whitespace such as a blank or line break String nextLine() Returns the rest of the current line, excluding any line separator at the end.
  • 4. //Import the class//Import the class import java.util.Scanner;import java.util.Scanner; public class NumericInput{public class NumericInput{ public static void main(String[] args){public static void main(String[] args){ // Declarations// Declarations Scanner in = new Scanner(System.in);Scanner in = new Scanner(System.in); int integer = in.nextInt();int integer = in.nextInt(); long longInteger = in.nextLong();long longInteger = in.nextLong(); float realNumber = in.nextFloat();float realNumber = in.nextFloat(); double doubleReal = in.nextDouble();double doubleReal = in.nextDouble(); String stringLine = in.nextLine();String stringLine = in.nextLine(); String stringStoken= in.next();String stringStoken= in.next(); System.out.println("Enter an integer = “+ integer+”, a longSystem.out.println("Enter an integer = “+ integer+”, a long integer =“+ longInteger +“,a floating-point = “+ doubleReal+”integer =“+ longInteger +“,a floating-point = “+ doubleReal+” ,and a string = “+ stringStoken+”, It will print whole line = “+,and a string = “+ stringStoken+”, It will print whole line = “+ stringLine);stringLine); } }} } 44http://eftakhairul.worldpress.comhttp://eftakhairul.worldpress.com Scanner Class ExampleScanner Class Example
  • 5. 55 BufferedReaderBufferedReader import java.io.*;import java.io.*; Public class Taking inputPublic class Taking input {{ Public static void main(String [] args)Public static void main(String [] args) throws IOExceptionthrows IOException {{ BufferedReader x = new BufferedReader(newBufferedReader x = new BufferedReader(new InputStreamReader(System.in));InputStreamReader(System.in)); //// String box;String box; //String box=x.readLine();//String box=x.readLine(); Converting x in IntegerConverting x in Integer int n=Integer.parseInt(int n=Integer.parseInt(xx.readLine());.readLine()); http://eftakhairul.worldpress.comhttp://eftakhairul.worldpress.com
  • 6. 66 //Converting x in Float//Converting x in Float float f=Float.parseFloat(x.readLine());float f=Float.parseFloat(x.readLine()); //Converting x in Double//Converting x in Double double d=Double.parseDouble(x.readLine());double d=Double.parseDouble(x.readLine()); //Converting x in Long//Converting x in Long long l=Long.parseLong(x.readLine());long l=Long.parseLong(x.readLine()); //Converting x in Byte//Converting x in Byte byte b=Byte.parseByte(x.readLine());byte b=Byte.parseByte(x.readLine()); }} }} http://eftakhairul.worldpress.comhttp://eftakhairul.worldpress.com
  • 7. 77 JOptionPaneJOptionPane //importing class from java//importing class from java Import javax.swing.JOptionPane.*;Import javax.swing.JOptionPane.*; Main()………Main()……… String s = JOptionPane.showInputDialog ("String s = JOptionPane.showInputDialog ("Enter anyEnter any numbernumber");"); //Converting x in integer//Converting x in integer int p = Integer.parseInt(x);int p = Integer.parseInt(x); //Converting x in Double//Converting x in Double double d = Double.parseDouble(x.readLine());double d = Double.parseDouble(x.readLine()); http://eftakhairul.worldpress.comhttp://eftakhairul.worldpress.com
  • 8. Take input from fileTake input from file // Declarations// Declarations Scanner inFile = new Scanner(newScanner inFile = new Scanner(new FileReader("File name with directories"));FileReader("File name with directories")); //Read token by token from file//Read token by token from file String stringStoken= inFile.next();String stringStoken= inFile.next(); //Read Line by Line from file//Read Line by Line from file String stringLine = inFile.nextLine();String stringLine = inFile.nextLine(); 88http://eftakhairul.worldpress.comhttp://eftakhairul.worldpress.com
  • 9. Reading data from InternetReading data from Internet  import java.net.*;import java.net.*;  import java.io.*;import java.io.*;  public class WebSiteReader {public class WebSiteReader {  public static void main(String args[]){public static void main(String args[]){  String nextLine;String nextLine;  try{try{  // Create the URL obect that points & at the default file index.html// Create the URL obect that points & at the default file index.html  URL url = new URL("http://rainbd.freelance.com.bd/index.html" );URL url = new URL("http://rainbd.freelance.com.bd/index.html" );  URLConnection urlConn = url.openConnection();URLConnection urlConn = url.openConnection();  BufferedReader buff= new BufferedReader(newBufferedReader buff= new BufferedReader(new InputStreamReader(urlConn.getInputStream()));InputStreamReader(urlConn.getInputStream()));  // Read and print the lines from index.html// Read and print the lines from index.html  while (true){while (true){  nextLine =buff.readLine();nextLine =buff.readLine();  if (nextLine !=null){if (nextLine !=null){  System.out.println(nextLine);System.out.println(nextLine);  }else{}else{  break;break;  }}  }}  } catch(Exception e){} catch(Exception e){  System.out.println("Please read the exception carefully:" + e.toString());System.out.println("Please read the exception carefully:" + e.toString());  } } }} } } http://eftakhairul.worldpress.comhttp://eftakhairul.worldpress.com 99
  • 10. 1010 Difference between println & printDifference between println & print System.out.println(“java”);System.out.println(“java”); System.out.println(“world”);System.out.println(“world”); >> JavaJava worldworld >> (Different line )(Different line ) http://eftakhairul.worldpress.comhttp://eftakhairul.worldpress.com
  • 12. 1212 out.println( );out.println( ); import static java.lang.System.out;import static java.lang.System.out; main()…….main()……. out.println(“shortcut of println”);out.println(“shortcut of println”); out.print(“shortcut of print”);out.print(“shortcut of print”); http://eftakhairul.worldpress.comhttp://eftakhairul.worldpress.com