0% found this document useful (0 votes)
5 views

70 Slides Control Flow Parsing Values and Reading Input Using System - Console

Uploaded by

Ines Masmoudi
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

70 Slides Control Flow Parsing Values and Reading Input Using System - Console

Uploaded by

Ines Masmoudi
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Parsing Values and Reading Input using System.

console()

In the last video, I talked about static and instance fields, and methods on the class.

I also talked about a concept called instantiating a class, which creates an object or
instance.
I'll be using both of these features in this video.
I'm going to create an interactive application where a user will enter their name and
year of birth, and then the application will calculate the current age of the user.
Before we start though, let's talk about parsing data.

COMPLETE JAVA MASTERCLASS


Parsing Values and Reading Input using System.console()
Parsing Values and Reading Input using System.console()

When we read data in from either a file or from user input, it's common for the data
to be initially stored as a String, which we'll need to convert to a numeric value.
Let's review what happens when our numeric data is really a String.
You might remember I talked about this previously when I talked about operators in
Java, and how the plus symbol means something different for numeric values than it
does for Strings.
You might also remember that many of the other operators aren't applicable to
Strings.
Let's look at a slide we've seen before.

COMPLETE JAVA MASTERCLASS


Parsing Values and Reading Input using System.console()
Summary of Operators

Operator Numeric types char boolean String


Concatenatio
+ Addition Addition n/a
n
- Subtraction Subtraction n/a n/a

* Multiplication Multiplication n/a n/a

/ Division Division n/a n/a


Remainder Remainder
% n/a n/a
(Modulus) (Modulus)

COMPLETE JAVA MASTERCLASS


Parsing Values and Reading Input using System.console()
Wrapper methods to parse strings to numeric
values
If you recall, I used the wrapper classes to get min and max values.
In this case, I'm going to use a static method on the wrapper class to let that class
do the transformation for us.

Wrapper Wrapper Method


Integer parseInt(String)
Double parseDouble(String)

COMPLETE JAVA MASTERCLASS


Parsing Values and Reading Input using System.console()
Reading data from the console
Technique Description
Like System.out, Java provides System.in which can read input
System.in from the console or terminal. It’s not easy to use for beginners,
and lots of code has been built around it, to make it easier.
This is Java’s solution for easier support for reading a single line
and prompting user for information. Although this is easy to use, it
System.console doesn’t work with IDE’s because these environments disable it.
You’re only able to run it using Intellij Terminal session : java
src/Main.java
This is calling the Java program and specifying data in the call.
Command Line
This is very commonly used but doesn’t let us create an
Arguments
interactive application in a loop in Java.
The Scanner class was built to be a common way to read input,
Scanner either using System.in or a file. For beginners, it’s much easier to
understand than the bare bones System.in
COMPLETE JAVA MASTERCLASS
Parsing Values and Reading Input using System.console()

You might also like