SlideShare uma empresa Scribd logo
1 de 43
IO Java
CST200 – Week 1: Basic Reading and Writing in
Java
Instructor: Andreea Molnar
Outline
• Aims
• Writing to the display
• Reading from the keyboard
Introduction
• Input/Ouput (IO) operations in Java are
more complex than described here.

• You will need the information presented
here to complete your assignments and
for the exams
Writing
•

System.out.println()

•

System.out.println(“Hello World”);

–prints a new line

– prints
Hello World and then moves to the new
line
Writing
You can have an expression to be printed.
Check the definition of an expression!!!
System.out.println(3+7+8);

//prints 18 and then moves to a new line
System.out.println(“My student ID is “ + 3456E);

//prints My student ID is 3456E
Writing
int ID = 3456;
System.out.println(“My student ID is “ + ID);

//prints My student ID is 3456
Writing
String firstName = “Andreea”;
String lastName = “Molnar”;
System.out.println(firstName + lastName);

//prints AndreeaMolnar
Writing
String firstName = “Andreea”;
String lastName = “Molnar”;
System.out.println(firstName + “ “ + lastName);

//prints Andreea Molnar
Writing
String firstName = “Andreea”;
String lastName = “Molnar”;
System.out.println(firstName.charAt(0) + “ “ +
lastName.substring(1, 3));

//prints A ol
Writing
String firstName = “Andreea”;

String lastName = “Molnar”;
System.out.println(firstName.charAt(0) + “ “ +
lastName.substring(1, 3));

//prints A ol
If you do not understand how the above output is generated check the
String API, Introduction to Java.ppt or Annex 1 at the end of this
presentation. If any of these do not work for you, please ask.
Writing
You can use escape sequences.
Escape
sequence

//will print:
//Andreea
//Molnar

b

backspace

t

tab

n

newline

r

System.out.println(firstName + “n" +
lastName);

Meaning

return

’’

double quote

’

single quote



backslash
Writing
•

System.out.println(“Hello World”);

•

System.out.print(“Hello World”);

– prints
Hello World and then moves to the new
line
- does the
same thing as System.out.println but it
does not move to a new line
Writing
System.out.println(“Hello World”);
System.out.println(“I am here”);

System.out.print(“Hello World”);
System.out.print(“I am here”);
Reading
This section will provide you with an
example on how to read from the
keyboard using BufferedReader.

You should find attached under this
presentation the code used.
Reading
You will need to import the following
classes, in order to make use of them.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
You may want to look again over JavaClassLibrary video from
pluralsight. I have added a few more explanations on Annex 2.
Reading
Change the main method to throw an
exception:
public static void main(String[] args) throws
IOException {
}
Exception handling is also not covered by this course. You can
find more details at:
http://pluralsight.com/training/Courses/TableOfContents/java2
Reading
Change the main method to throw an
exception:
public static void main(String[] args) throws
IOException {
}
Exception handling are also not covered by this course. You
can find more details at:
http://pluralsight.com/training/Courses/TableOfContents/java2
Reading
Use BufferedReader for reading lines of
text from standard input (i.e. keyboard)
BufferedReader reader = new BufferedReader(new
InputStreamReader(System.in));
reader.readLine();
//reads a line of text as a String
IO in Java is not covered in details in this course, and this
example is provided just to help you write algorithms (check
the definition of an algorithm)
http://pluralsight.com/training/Courses/TableOfContents/java2
Reading
Use the Integer and Double static
functions to convert a number from a
String to an Integer (int) or Double
(double).

You may want to check again the video about boxing and
unboxing:
http://pluralsight.com/training/Courses/TableOfContents/java1
Reading
To convert a String to an Integer you can
use: Integer.parseInt(String s);
int number = Integer.parseInt(“23”);
http://docs.oracle.com/javase/7/docs/api/java/lang/Integer.html
Reading
To convert a String to a Double you can
use: Integer.parseInt(String s);
int number = Integer.parseInt(“23”);
http://docs.oracle.com/javase/7/docs/api/java/lang/Integer.html
Reading
To convert a String to a Double you can use:
Double.parseDouble(String s);

double number = Double.parseDouble (“23”);
http://docs.oracle.com/javase/7/docs/api/java/lang/Double.html
Reading
If you want to read multiple numbers or strings
on the same line, you can use regular
expressions to extract the numbers.
Reading
String line = reader.readLine();

//assuming that the read line provides strings
or numbers or characters separated by space
String [] input = line.split(“ “);

http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#split(ja
va.lang.String)
Reading
//+ is used as a separator
String [] line = line.split(“+“);

//* is used as a separator
String [] line = line.split(“*“);
Reading
String line = reader.readLine();

//assuming that the user introduces the
following string: “my first program”, the line
variable will contain the value my first program
Check the definition of variable if you do not
know it !!!
My first program

lin
e
Reading
String line = reader.readLine();
String [] input = line.split(“ “);

First value in an array starts at index 0!!!

inp
ut
ind
ex

my

first

program

0

1

2

my first program

lin
e
Reading
String line = reader.readLine();
String [] input = line.split(“ “);
String firstString = input[0];

inp
0
ut
ind
ex
firstStri
my

first

program

1

2

my

my first program

lin
e
Reading
String line = reader.readLine();
String [] input = line.split(“ “);
String secondString= input[1];

inp
0
ut
ind
ex
secondStr
my

first

program

1

2

first

my first program

lin
e
Reading
String line = reader.readLine();
String [] input = line.split(“ “);
String thirdString= input[2];

inp
0
ut
ind
ex
thirdStri
my

first

program

1

2

program

my first program

lin
e
Reading
String line = reader.readLine();

//assuming now that the user introduces the
following string: “45*90*78”
String [] input = line.split(“*“);

inp
ut
ind
ex

45

90

78

0

1

2

45*90*78

lin
e
Reading
String line = reader.readLine();

String [] input = line.split(“*“);
int no1 = Integer.parseInt(input[0]);
Although input looks like it contains numbers, the numbers are in fact
represented as strings, therefore they need to be converted to int.

inp
ut
ind

45

90

78

0

1

2

45*90*78

lin
Reading
String line = reader.readLine();
String [] input = line.split(“*“);
int no1 = Integer.parseInt(input[0]);

inp
ut
ind
ex

45

90

78

0

1

2

no1

45*90*78

45

lin
e
Reading
String line = reader.readLine();
String [] input = line.split(“*“);
int no1 = Integer.parseInt(input[0]);

inp
ut
ind
ex

45

90

78

0

1

2

no2

45*90*78

90

lin
e
Reading
String line = reader.readLine();
String [] input = line.split(“*“);
int no1 = Integer.parseInt(input[0]);

inp
ut
ind
ex

45

90

78

0

1

2

no3

45*90*78

78

lin
e
Summary
• Writing: use System.out.println() and
System.out.print();

• Reading: use BufferedReader class
Annex 1
A

d

r

e

e

a

0

1

2

3

4

5

6

ind
firstName.charAt(0); ex

String firstName = “Andreea”;
char firstCharacter=

n
Annex 1
A

char firstCharacter= firstName.charAt(0);

d

r

e

e

a

0

String firstName = “Andreea”;

n

1

2

3

4

5

6

ind
ex

char secondCharacter = firstName.charAt(1);

char thirdCharacter = firstName.charAt(2);
char fourthCharacter

//…

= firstName.charAt(3);
Annex 1
A

//will print A
System.out.println(secondCharacter);

//will print n

d

r

e

e

a

0

System.out.println(firstCharacter);

n

1

2

3

4

5

6

ind
ex
Annex 1
M

l

n

a

r

0

String lastName = “Molnar”;

o

1

2

3

4

5

ind
ex

String subString= lastName.substring(1, 3));
Annex 1
M

String subString= lastName.substring(1, 3));
System.out.println(lastName);

//will print oln

l

n

a

r

0

String lastName = “Molnar”;

o

1

2

3

4

5
Annex 2
•
•
•

When you use an already implemented class from Java
framework, you are basically using a class from a package.
java.lang package is automatically imported this is why for the
HelloWorld program you didn’t need to import anything.
To import an entire package you can write import package.*. For
example: import java.util.*;
Annex 2
•

Importing a package allows you to use the class from a library
without fully using its fully qualified name. For example in case of
BufferedReader, without importing java.io.BufferedReader you
would have to write:
java.io.BufferedReader reader = new java.io.BufferedReader (…);

http://docs.oracle.com/javase/7/docs/api/java/io/BufferedReader.html

Mais conteúdo relacionado

Mais procurados

Mais procurados (20)

Java ppt
Java pptJava ppt
Java ppt
 
02basics
02basics02basics
02basics
 
09. Java Methods
09. Java Methods09. Java Methods
09. Java Methods
 
Taking User Input in Java
Taking User Input in JavaTaking User Input in Java
Taking User Input in Java
 
Java Basics
Java BasicsJava Basics
Java Basics
 
Java Tutorial
Java Tutorial Java Tutorial
Java Tutorial
 
OCA Java SE 8 Exam Chapter 3 Core Java APIs
OCA Java SE 8 Exam Chapter 3 Core Java APIsOCA Java SE 8 Exam Chapter 3 Core Java APIs
OCA Java SE 8 Exam Chapter 3 Core Java APIs
 
09. Methods
09. Methods09. Methods
09. Methods
 
DSA 103 Object Oriented Programming :: Week 4
DSA 103 Object Oriented Programming :: Week 4DSA 103 Object Oriented Programming :: Week 4
DSA 103 Object Oriented Programming :: Week 4
 
Learn Java Part 2
Learn Java Part 2Learn Java Part 2
Learn Java Part 2
 
Java Fundamentals
Java FundamentalsJava Fundamentals
Java Fundamentals
 
Introduction to java Programming
Introduction to java ProgrammingIntroduction to java Programming
Introduction to java Programming
 
Java Concurrency by Example
Java Concurrency by ExampleJava Concurrency by Example
Java Concurrency by Example
 
Python
PythonPython
Python
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
 
Java Tutorial | My Heart
Java Tutorial | My HeartJava Tutorial | My Heart
Java Tutorial | My Heart
 
Principles of the Play framework
Principles of the Play frameworkPrinciples of the Play framework
Principles of the Play framework
 
Icom4015 lecture15-f16
Icom4015 lecture15-f16Icom4015 lecture15-f16
Icom4015 lecture15-f16
 
JAVA BASICS
JAVA BASICSJAVA BASICS
JAVA BASICS
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
 

Semelhante a Reading and writting v2

Semelhante a Reading and writting v2 (20)

Learning Java 1 – Introduction
Learning Java 1 – IntroductionLearning Java 1 – Introduction
Learning Java 1 – Introduction
 
Programming with Java: the Basics
Programming with Java: the BasicsProgramming with Java: the Basics
Programming with Java: the Basics
 
CSL101_Ch1.pptx
CSL101_Ch1.pptxCSL101_Ch1.pptx
CSL101_Ch1.pptx
 
CSL101_Ch1.pptx
CSL101_Ch1.pptxCSL101_Ch1.pptx
CSL101_Ch1.pptx
 
CSL101_Ch1.ppt
CSL101_Ch1.pptCSL101_Ch1.ppt
CSL101_Ch1.ppt
 
CSL101_Ch1.ppt
CSL101_Ch1.pptCSL101_Ch1.ppt
CSL101_Ch1.ppt
 
01-ch01-1-println.ppt java introduction one
01-ch01-1-println.ppt java introduction one01-ch01-1-println.ppt java introduction one
01-ch01-1-println.ppt java introduction one
 
Lecture 2 java.pdf
Lecture 2 java.pdfLecture 2 java.pdf
Lecture 2 java.pdf
 
Java Tutorial
Java TutorialJava Tutorial
Java Tutorial
 
Unit I Advanced Java Programming Course
Unit I   Advanced Java Programming CourseUnit I   Advanced Java Programming Course
Unit I Advanced Java Programming Course
 
Java Programming for Designers
Java Programming for DesignersJava Programming for Designers
Java Programming for Designers
 
Chapter 2 java
Chapter 2 javaChapter 2 java
Chapter 2 java
 
Java
JavaJava
Java
 
Hello java
Hello java   Hello java
Hello java
 
Hello Java-First Level
Hello Java-First LevelHello Java-First Level
Hello Java-First Level
 
Hello java
Hello java  Hello java
Hello java
 
Java tutorial PPT
Java tutorial PPTJava tutorial PPT
Java tutorial PPT
 
Java tutorial PPT
Java tutorial  PPTJava tutorial  PPT
Java tutorial PPT
 
Java fundamentals
Java fundamentalsJava fundamentals
Java fundamentals
 
Bring the fun back to java
Bring the fun back to javaBring the fun back to java
Bring the fun back to java
 

Mais de ASU Online

Midterm common mistakes
Midterm common mistakesMidterm common mistakes
Midterm common mistakesASU Online
 
Lists, queues and stacks 1
Lists, queues and stacks 1Lists, queues and stacks 1
Lists, queues and stacks 1ASU Online
 
Lists, queues and stacks
Lists, queues and stacksLists, queues and stacks
Lists, queues and stacksASU Online
 
Classes revision
Classes revisionClasses revision
Classes revisionASU Online
 
For loop java 2
For loop java 2For loop java 2
For loop java 2ASU Online
 
Common errors v2
Common errors v2Common errors v2
Common errors v2ASU Online
 
Common missunderestandings
Common missunderestandingsCommon missunderestandings
Common missunderestandingsASU Online
 
Introduction to java
Introduction to javaIntroduction to java
Introduction to javaASU Online
 
Lecture 14 tourism in europe
Lecture 14   tourism in europeLecture 14   tourism in europe
Lecture 14 tourism in europeASU Online
 
Lecture 13 tourism in the south pacific
Lecture 13   tourism in the south pacificLecture 13   tourism in the south pacific
Lecture 13 tourism in the south pacificASU Online
 
Lecture 12 tourism in australia and new zealand
Lecture 12   tourism in australia and new zealandLecture 12   tourism in australia and new zealand
Lecture 12 tourism in australia and new zealandASU Online
 
Lecture 11 tourism in east asia
Lecture 11   tourism in east asiaLecture 11   tourism in east asia
Lecture 11 tourism in east asiaASU Online
 
Lecture 9 tourism in south asia
Lecture 9   tourism in south asiaLecture 9   tourism in south asia
Lecture 9 tourism in south asiaASU Online
 
Lecture 9 tourism in south asia-1
Lecture 9   tourism in south asia-1Lecture 9   tourism in south asia-1
Lecture 9 tourism in south asia-1ASU Online
 
Lecture 8 tourism in central asia
Lecture 8   tourism in central asiaLecture 8   tourism in central asia
Lecture 8 tourism in central asiaASU Online
 
Lecture 7 tourism in the middle east and north africa
Lecture 7   tourism in the middle east and north africaLecture 7   tourism in the middle east and north africa
Lecture 7 tourism in the middle east and north africaASU Online
 
Lecture 6 tourism in sub-saharan africa
Lecture 6   tourism in sub-saharan africaLecture 6   tourism in sub-saharan africa
Lecture 6 tourism in sub-saharan africaASU Online
 
Lecture 5 tourism in latin america
Lecture 5   tourism in latin americaLecture 5   tourism in latin america
Lecture 5 tourism in latin americaASU Online
 
Lecture 4 tourism in the caribbean
Lecture 4   tourism in the caribbeanLecture 4   tourism in the caribbean
Lecture 4 tourism in the caribbeanASU Online
 
Lecture 3 political context of international tourism
Lecture 3   political context of international tourismLecture 3   political context of international tourism
Lecture 3 political context of international tourismASU Online
 

Mais de ASU Online (20)

Midterm common mistakes
Midterm common mistakesMidterm common mistakes
Midterm common mistakes
 
Lists, queues and stacks 1
Lists, queues and stacks 1Lists, queues and stacks 1
Lists, queues and stacks 1
 
Lists, queues and stacks
Lists, queues and stacksLists, queues and stacks
Lists, queues and stacks
 
Classes revision
Classes revisionClasses revision
Classes revision
 
For loop java 2
For loop java 2For loop java 2
For loop java 2
 
Common errors v2
Common errors v2Common errors v2
Common errors v2
 
Common missunderestandings
Common missunderestandingsCommon missunderestandings
Common missunderestandings
 
Introduction to java
Introduction to javaIntroduction to java
Introduction to java
 
Lecture 14 tourism in europe
Lecture 14   tourism in europeLecture 14   tourism in europe
Lecture 14 tourism in europe
 
Lecture 13 tourism in the south pacific
Lecture 13   tourism in the south pacificLecture 13   tourism in the south pacific
Lecture 13 tourism in the south pacific
 
Lecture 12 tourism in australia and new zealand
Lecture 12   tourism in australia and new zealandLecture 12   tourism in australia and new zealand
Lecture 12 tourism in australia and new zealand
 
Lecture 11 tourism in east asia
Lecture 11   tourism in east asiaLecture 11   tourism in east asia
Lecture 11 tourism in east asia
 
Lecture 9 tourism in south asia
Lecture 9   tourism in south asiaLecture 9   tourism in south asia
Lecture 9 tourism in south asia
 
Lecture 9 tourism in south asia-1
Lecture 9   tourism in south asia-1Lecture 9   tourism in south asia-1
Lecture 9 tourism in south asia-1
 
Lecture 8 tourism in central asia
Lecture 8   tourism in central asiaLecture 8   tourism in central asia
Lecture 8 tourism in central asia
 
Lecture 7 tourism in the middle east and north africa
Lecture 7   tourism in the middle east and north africaLecture 7   tourism in the middle east and north africa
Lecture 7 tourism in the middle east and north africa
 
Lecture 6 tourism in sub-saharan africa
Lecture 6   tourism in sub-saharan africaLecture 6   tourism in sub-saharan africa
Lecture 6 tourism in sub-saharan africa
 
Lecture 5 tourism in latin america
Lecture 5   tourism in latin americaLecture 5   tourism in latin america
Lecture 5 tourism in latin america
 
Lecture 4 tourism in the caribbean
Lecture 4   tourism in the caribbeanLecture 4   tourism in the caribbean
Lecture 4 tourism in the caribbean
 
Lecture 3 political context of international tourism
Lecture 3   political context of international tourismLecture 3   political context of international tourism
Lecture 3 political context of international tourism
 

Último

DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenHervé Boutemy
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek SchlawackFwdays
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):comworks
 
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo Day
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo DayH2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo Day
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo DaySri Ambati
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Mattias Andersson
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationSlibray Presentation
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Mark Simos
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...Fwdays
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024Lorenzo Miniero
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii SoldatenkoFwdays
 
Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Manik S Magar
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupFlorian Wilhelm
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostLeverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostZilliz
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionDilum Bandara
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebUiPathCommunity
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brandgvaughan
 
DSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningDSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningLars Bell
 

Último (20)

DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache Maven
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):
 
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo Day
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo DayH2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo Day
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo Day
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck Presentation
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
 
DMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special EditionDMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special Edition
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko
 
Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project Setup
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostLeverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An Introduction
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio Web
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brand
 
DSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningDSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine Tuning
 

Reading and writting v2

  • 1. IO Java CST200 – Week 1: Basic Reading and Writing in Java Instructor: Andreea Molnar
  • 2. Outline • Aims • Writing to the display • Reading from the keyboard
  • 3. Introduction • Input/Ouput (IO) operations in Java are more complex than described here. • You will need the information presented here to complete your assignments and for the exams
  • 4. Writing • System.out.println() • System.out.println(“Hello World”); –prints a new line – prints Hello World and then moves to the new line
  • 5. Writing You can have an expression to be printed. Check the definition of an expression!!! System.out.println(3+7+8); //prints 18 and then moves to a new line System.out.println(“My student ID is “ + 3456E); //prints My student ID is 3456E
  • 6. Writing int ID = 3456; System.out.println(“My student ID is “ + ID); //prints My student ID is 3456
  • 7. Writing String firstName = “Andreea”; String lastName = “Molnar”; System.out.println(firstName + lastName); //prints AndreeaMolnar
  • 8. Writing String firstName = “Andreea”; String lastName = “Molnar”; System.out.println(firstName + “ “ + lastName); //prints Andreea Molnar
  • 9. Writing String firstName = “Andreea”; String lastName = “Molnar”; System.out.println(firstName.charAt(0) + “ “ + lastName.substring(1, 3)); //prints A ol
  • 10. Writing String firstName = “Andreea”; String lastName = “Molnar”; System.out.println(firstName.charAt(0) + “ “ + lastName.substring(1, 3)); //prints A ol If you do not understand how the above output is generated check the String API, Introduction to Java.ppt or Annex 1 at the end of this presentation. If any of these do not work for you, please ask.
  • 11. Writing You can use escape sequences. Escape sequence //will print: //Andreea //Molnar b backspace t tab n newline r System.out.println(firstName + “n" + lastName); Meaning return ’’ double quote ’ single quote backslash
  • 12. Writing • System.out.println(“Hello World”); • System.out.print(“Hello World”); – prints Hello World and then moves to the new line - does the same thing as System.out.println but it does not move to a new line
  • 13. Writing System.out.println(“Hello World”); System.out.println(“I am here”); System.out.print(“Hello World”); System.out.print(“I am here”);
  • 14. Reading This section will provide you with an example on how to read from the keyboard using BufferedReader. You should find attached under this presentation the code used.
  • 15. Reading You will need to import the following classes, in order to make use of them. import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; You may want to look again over JavaClassLibrary video from pluralsight. I have added a few more explanations on Annex 2.
  • 16. Reading Change the main method to throw an exception: public static void main(String[] args) throws IOException { } Exception handling is also not covered by this course. You can find more details at: http://pluralsight.com/training/Courses/TableOfContents/java2
  • 17. Reading Change the main method to throw an exception: public static void main(String[] args) throws IOException { } Exception handling are also not covered by this course. You can find more details at: http://pluralsight.com/training/Courses/TableOfContents/java2
  • 18. Reading Use BufferedReader for reading lines of text from standard input (i.e. keyboard) BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); reader.readLine(); //reads a line of text as a String IO in Java is not covered in details in this course, and this example is provided just to help you write algorithms (check the definition of an algorithm) http://pluralsight.com/training/Courses/TableOfContents/java2
  • 19. Reading Use the Integer and Double static functions to convert a number from a String to an Integer (int) or Double (double). You may want to check again the video about boxing and unboxing: http://pluralsight.com/training/Courses/TableOfContents/java1
  • 20. Reading To convert a String to an Integer you can use: Integer.parseInt(String s); int number = Integer.parseInt(“23”); http://docs.oracle.com/javase/7/docs/api/java/lang/Integer.html
  • 21. Reading To convert a String to a Double you can use: Integer.parseInt(String s); int number = Integer.parseInt(“23”); http://docs.oracle.com/javase/7/docs/api/java/lang/Integer.html
  • 22. Reading To convert a String to a Double you can use: Double.parseDouble(String s); double number = Double.parseDouble (“23”); http://docs.oracle.com/javase/7/docs/api/java/lang/Double.html
  • 23. Reading If you want to read multiple numbers or strings on the same line, you can use regular expressions to extract the numbers.
  • 24. Reading String line = reader.readLine(); //assuming that the read line provides strings or numbers or characters separated by space String [] input = line.split(“ “); http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#split(ja va.lang.String)
  • 25. Reading //+ is used as a separator String [] line = line.split(“+“); //* is used as a separator String [] line = line.split(“*“);
  • 26. Reading String line = reader.readLine(); //assuming that the user introduces the following string: “my first program”, the line variable will contain the value my first program Check the definition of variable if you do not know it !!! My first program lin e
  • 27. Reading String line = reader.readLine(); String [] input = line.split(“ “); First value in an array starts at index 0!!! inp ut ind ex my first program 0 1 2 my first program lin e
  • 28. Reading String line = reader.readLine(); String [] input = line.split(“ “); String firstString = input[0]; inp 0 ut ind ex firstStri my first program 1 2 my my first program lin e
  • 29. Reading String line = reader.readLine(); String [] input = line.split(“ “); String secondString= input[1]; inp 0 ut ind ex secondStr my first program 1 2 first my first program lin e
  • 30. Reading String line = reader.readLine(); String [] input = line.split(“ “); String thirdString= input[2]; inp 0 ut ind ex thirdStri my first program 1 2 program my first program lin e
  • 31. Reading String line = reader.readLine(); //assuming now that the user introduces the following string: “45*90*78” String [] input = line.split(“*“); inp ut ind ex 45 90 78 0 1 2 45*90*78 lin e
  • 32. Reading String line = reader.readLine(); String [] input = line.split(“*“); int no1 = Integer.parseInt(input[0]); Although input looks like it contains numbers, the numbers are in fact represented as strings, therefore they need to be converted to int. inp ut ind 45 90 78 0 1 2 45*90*78 lin
  • 33. Reading String line = reader.readLine(); String [] input = line.split(“*“); int no1 = Integer.parseInt(input[0]); inp ut ind ex 45 90 78 0 1 2 no1 45*90*78 45 lin e
  • 34. Reading String line = reader.readLine(); String [] input = line.split(“*“); int no1 = Integer.parseInt(input[0]); inp ut ind ex 45 90 78 0 1 2 no2 45*90*78 90 lin e
  • 35. Reading String line = reader.readLine(); String [] input = line.split(“*“); int no1 = Integer.parseInt(input[0]); inp ut ind ex 45 90 78 0 1 2 no3 45*90*78 78 lin e
  • 36. Summary • Writing: use System.out.println() and System.out.print(); • Reading: use BufferedReader class
  • 37. Annex 1 A d r e e a 0 1 2 3 4 5 6 ind firstName.charAt(0); ex String firstName = “Andreea”; char firstCharacter= n
  • 38. Annex 1 A char firstCharacter= firstName.charAt(0); d r e e a 0 String firstName = “Andreea”; n 1 2 3 4 5 6 ind ex char secondCharacter = firstName.charAt(1); char thirdCharacter = firstName.charAt(2); char fourthCharacter //… = firstName.charAt(3);
  • 39. Annex 1 A //will print A System.out.println(secondCharacter); //will print n d r e e a 0 System.out.println(firstCharacter); n 1 2 3 4 5 6 ind ex
  • 40. Annex 1 M l n a r 0 String lastName = “Molnar”; o 1 2 3 4 5 ind ex String subString= lastName.substring(1, 3));
  • 41. Annex 1 M String subString= lastName.substring(1, 3)); System.out.println(lastName); //will print oln l n a r 0 String lastName = “Molnar”; o 1 2 3 4 5
  • 42. Annex 2 • • • When you use an already implemented class from Java framework, you are basically using a class from a package. java.lang package is automatically imported this is why for the HelloWorld program you didn’t need to import anything. To import an entire package you can write import package.*. For example: import java.util.*;
  • 43. Annex 2 • Importing a package allows you to use the class from a library without fully using its fully qualified name. For example in case of BufferedReader, without importing java.io.BufferedReader you would have to write: java.io.BufferedReader reader = new java.io.BufferedReader (…); http://docs.oracle.com/javase/7/docs/api/java/io/BufferedReader.html