DCIT 22 – Computer Programming 1
Module 5: Java Strings, Math & Scanner
Introduction:
In this module, students will dive into essential Java functionalities that enhance data
manipulation and user interaction. They will explore the use of Java Strings for text
processing, leverage the Math class for performing various mathematical operations, and
utilize the Scanner class to enable dynamic user input.
Objectives:
In this module the student should be able to:
1. Understand and manipulate text data using Java Strings, including operations like
finding string length, changing case, finding character positions, and concatenating
strings.
2. Apply various string methods, including handling special characters with escape
sequences.
3. Use Java's Math class to perform fundamental mathematical operations, such as
finding maximum and minimum values, square roots, and generating random
numbers.
4. Implement the Scanner class to read user input in different data types (e.g., integer,
double, boolean) and handle inputs for interactive programs.
Java Strings
Strings are used for storing text. A String variable contains a collection of characters
surrounded by double quotes:
Example:
String Length
A String in Java is actually an object, which contain methods that can perform certain operations
on strings. For example, the length of a string can be found with the length() method:
Example:
Code & Output:
Cavite State University – Naic
Information Technology Department
DCIT 22 – Computer Programming 1
Module 5: Java Strings, Math & Scanner
String Methods: toUpperCase() & toLowerCase()
toUppercase() method converts all characters in a string to uppercase letters. This
method does not alter the original string but returns a new one in uppercase.
toLowerCase() converts all characters in a string to lowercase letters. Like
toUpperCase(), this method returns a new string in lowercase without changing the
original string.
Example:
Code & Output:
Finding a Character in a String
The indexOf() method returns the index (the position) of the first occurrence of a specified
text in a string (including whitespace):
Example:
Code & Output:
Cavite State University – Naic
Information Technology Department
DCIT 22 – Computer Programming 1
Module 5: Java Strings, Math & Scanner
String Concatenation
The + operator can be used between strings to combine them. This is called concatenation:
Example:
Code & Output:
Adding Numbers and Strings
Java uses the + operator for both addition and concatenation.
Numbers are added. Strings are concatenated.
If you add two numbers, the result will be a number:
Code & Output:
If you add a number and a string, the result will be a string concatenation:
Cavite State University – Naic
Information Technology Department
DCIT 22 – Computer Programming 1
Module 5: Java Strings, Math & Scanner
Code & Output:
String – Special Characters
Because strings must be written within quotes, Java will misunderstand this string, and
generate an error:
The solution to avoid this problem, is to use the backslash escape character.
Escape characters are special characters that have to be written in a certain way to
overcome the limitations of the programming language's syntax.
The backslash (\) escape character turns special characters into string characters:
Escape character Result Description
\’ ‘ Single Quote
\” “ Double Quote
\\ \ Backlash
The sequence \" inserts a double quote in a string:
Code & Output:
The sequence \' inserts a single quote in a string:
Cavite State University – Naic
Information Technology Department
DCIT 22 – Computer Programming 1
Module 5: Java Strings, Math & Scanner
Code & Output:
The sequence \\ inserts a single backslash in a string:
Code & Output:
Other common escape sequence that are valid in Java are:
Escape character Result
\n New Line
\t Tab
The sequence \n inserts a new line in a string:
The sequence \t inserts a tab space in a string:
Cavite State University – Naic
Information Technology Department
DCIT 22 – Computer Programming 1
Module 5: Java Strings, Math & Scanner
Java Math
The Java Math class has many methods that allows you to perform mathematical tasks on
numbers.
[Link](x,y)
The [Link](x,y) method can be used to find the highest value of x and y.
Sample:
Code & Output:
[Link](x,y)
The [Link](x,y) method can be used to find the lowest value of x and y
Sample:
Code & Output:
[Link](x)
The [Link](x) method returns the square root of x
Sample:
Code & Output:
Cavite State University – Naic
Information Technology Department
DCIT 22 – Computer Programming 1
Module 5: Java Strings, Math & Scanner
[Link]()
The [Link]() method in Java returns a random double value in the range [0.0, 1.0),
meaning it includes 0.0 but never reaches 1.0. Each time [Link]() is called, a new
random number is generated within this range.
Sample:
Code & Output:
To get more control over the random number, for example, if you only want a random
number between 0 and 100, you can use the following formula:
Code & Output:
Java User Input (Scanner)
The Scanner class is used to get user input, and it is found in the [Link] package. To use
the Scanner class, create an object of the class and use any of the available methods to
read different Java Data Types. In this example, we will use the nextLine() method, which is
used to read Strings:
Sample:
Cavite State University – Naic
Information Technology Department
DCIT 22 – Computer Programming 1
Module 5: Java Strings, Math & Scanner
Code & Output:
Scanner Object Methods (Input Types)
In the example above, we used the nextLine() method, which is used to read Strings. To
read other types, look at the table below:
Method Description
nextBoolean() Reads a boolean value from the user
nextByte() Reads a byte value from the user
nextDouble() Reads a double value from the user
nextFloat() Reads a float value from the user
nextInt() Reads a int value from the user
nextLine() Reads a String value from the user
nextLong() Reads a long value from the user
nextShort() Reads a short value from the user
In the example below, we use different methods to read data of various types:
Cavite State University – Naic
Information Technology Department
DCIT 22 – Computer Programming 1
Module 5: Java Strings, Math & Scanner
Note:
The program would not proceed until the user types an input and pressed enter.
If you enter wrong input (e.g. text in a numerical input), you will get an exception/error
message (like "InputMismatchException").
Cavite State University – Naic
Information Technology Department