Skip to content

Latest commit

 

History

History
354 lines (261 loc) · 10.2 KB

c2-note.md

File metadata and controls

354 lines (261 loc) · 10.2 KB

Chapter 2

💎

Numbers, Strings, and other values are called literals.

Char 📍

Basic 📍

  • A character literal is surrounded with single quotes

Input 📍

Suppose there are 3 char variables,

char a;
char b;
char c;

a = scnr.next().charAt(0);
b = scnr.next().charAt(1);
c = scnr.next().charAt(2);

System.out.print("" + a + b + c);

The code snippet above doesn't contain any syntax errors but logical one.

💡 "" shows in front of the 3 characters, that's because Java will consider it as arithmetic, which means the program will show the sum of ASCII number(DEC) of those 3 characters. Adding "" at front can be considered as a String.

Good Practice 📍

Input:

java cpp python

Output:

jpt

Each scnr.next() is a single input, separated by whitespace(s).
First input is java, it'll take the 0 element - j.
Second input is cpp, it'll take the 1 element - p.
Third input is python, it'' take the 2 element - t.

Common Mistake 📍

Input:

hi sup yo

Output:

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range:
...

First input is hi, it'll take the 0 element - h.
Second input is sup, it'll take the 1 element - u.
Third input is yo, 2 element is not found. That is, the String index is out of range.

💡 It shows String while using char as inputs. As mentioned previously, a String is made up of multiple characters. This concepts can involve the knowledge of Array as follows.

char[] charArray = {'H', 'e', 'l', 'l', 'o'};

String word = "Hello";

// Similar right

ASCII Table 📍

DEC CHAR - DEC CHAR - DEC CHAR
32 Space - 64 @ - 96 `
33 ! - 65 A - 97 a
34 " - 66 B - 98 b
35 # - 67 C - 99 c
36 $ - 68 D - 100 d
37 % - 69 E - 101 e
38 & - 70 F - 102 f
39 ' - 71 G - 103 g
40 ( - 72 H - 104 h
41 ) - 73 I - 105 i
42 * - 74 J - 106 j
43 + - 75 K - 107 k
44 , - 76 L - 108 l
45 - - 77 M - 109 m
46 . - 78 N - 110 n
47 / - 79 O - 111 o
48 0 - 80 P - 112 p
49 1 - 81 Q - 113 q
50 2 - 82 R - 114 r
51 3 - 83 S - 115 s
52 4 - 84 T - 116 t
53 5 - 85 U - 117 u
54 6 - 86 V - 118 v
55 7 - 87 W - 119 w
56 8 - 88 X - 120 x
57 9 - 89 Y - 121 y
58 : - 90 Z - 122 z
59 ; - 91 [ - 123 {
60 < - 92 \ - 124 |
61 = - 93 ] - 125 }
62 > - 94 ^ - 126 ~
63 ? - 95 _ - ... ...
  • More ASCII if interested.

Escape Sequences 📍

Escape Sequence CHAR
\t Inserts a tab
\b Inserts a backspace
\n Inserts a newline
\r Inserts a carriage return
\f Inserts a form feed
\' Inserts a single quote
\" Inserts a double quote
\ Inserts a backslash

String 📍

Basic 📍

  • A String literal is surrounded with double quotes.
  • A string is a sequence of individual characters.

String Input 📍

Review the animation on Z-2.12.7 Mixing next() and nextLine().

📑 Check out the following code snippet

...
int year = scnr.nextInt();
String itemName = scnr.nextLine();

System.out.println("Year: " + year);
System.out.println("Item name: " + itemName);

➡️ Input:

2023
Cola

⬅️ Output:

Year: 2023
Item name: 

❗ This is a Scanner bug.

                nextLine() reads only \n
                   |
                   V
--------------------------------------
| 2 | 0 | 2 | 3 | \n | C | o | l | a |
--------------------------------------

✅ Solution:

...
int year = scnr.nextInt();
scnr.nextLine(); // read the newline
String itemName = scnr.nextLine();

System.out.println("Year: " + year);
System.out.println("Item name: " + itemName);

Operations 📍

indexOf() 📍

  • indexOf(ch, index)
  • ch is the target to be searched.
  • index is an optional parameter, and indicates where in the string to start looking.
  • Return the index of the first occurrence of the character in the string.
String flower = "poppy";
int index1 = flower.indexOf('p'); // 0
int index2 = flower.indexOf('p', 1); // 2, because it starts to search from index 1 
String quote = "Pleasant words are like a honeycomb";
int index1 = quote.indexOf('e'); // 2
int index2 = quote.indexOf('e', 17); // 2, because it starts to search from index 5 

lastIndexOf() 📍

  • lastIndexOf(ch)
  • ch is the target to be searched.
  • Return the index of the last occurrence of the character in the string.
String quote = "Honesty is the best policy";
int index = quote.lastIndexOf('t'); // 21

substring() 📍

  • substring(beginIndex, endIndex)
  • endIndex is not included, and it is an optional parameter.
  • Return a new String that is a substring of this String.
String quote = "Sweetness to the soul and health to the bones";

String quote_split1 = quote.substring(26); // health to the bones
String quote_split2 = quote.substring(0, 8); // Sweetness, the index 8 element is not included

charAt() 📍

  • charAt(index)
  • index is the position of the character.
  • Return the character at the specified index.
String city = "Melbourne";
char firstLetter = city.charAt(0); // Store first element of city
char fifthLetter = city.charAt(4); // Store fifth element of city

concat() 📍

  • concat(str)
  • str is the String that is concatenated to the end of this String.
  • Return a new String that represents the concatenation of this String and the str argument.
  • The + operator can also be used to concatenate two strings.
String str1 = "Hello";
System.out.println(str1.concat(" World")); // Hello World

length() 📍

  • length()
  • Return the length of a String.
System.out.println("Loveleh".length()); // 7

replace() 📍

  • replace(oldChar, newChar)
    • oldChar is the old character.
    • newChar is the new character.
    • Return a new String resulting from replacing all occurrences of oldChar in this string with newChar.
String str1 = "I need to drink some wa'er";
System.out.println(str1.replace('\'', 't')); // I need to drink some water
  • replace(oldstr, newstr)
    • oldstr is the old string.
    • newstr is the new string.
    • Return a new String resulting from replacing all occurrences of oldstr in this string with newstr.
String str1 = "Today's late night snack is cookie";
System.out.println(str1.replace("cookie", "ramen")); // Today's late night snack is ramen

+= operator 📍

  • += operator is used to concatenate another string itself.
String str1 = "Hello";
str1 += "!!!"; // Hello!!!

Integer 📍

Readable Syntax 📍

If an integer value contains multiple digits, undercores can be added to divide the digit into blocks for more readability, such as 1_000_000_000.

Double 📍

Division by Zero 📍

In mathematics, the division by zero can be expressed as $\frac{a}{b}$, where $b = 0$. The expression has no meaning and it is also considered as undefined. In Java, the program will print a floating-point number NaN (Not a Number).

Formatted Output 📍

double pi = 3.14159265358979323846;
System.out.printf("pi = %.2f\n", pi); // pi = 3.14 (with a new line)

Q & A 📍

Q1: String quote_split1 = quote.substring(26); what does the 26 represent? Confused with no endIndex there.

A1: 26 means where it starts, and endindex is optional, so if that is quote.substring(26), suppose quote is a infinite length string, it starts from 26, and end at infinite length -1. You may wonder why it has to be minus 1, e.g "hello".substring(2), hello has a length of 5 and the index of last letter o is 4. Thus, that would be the length - 1.

Q2: Is whitespace considered as a character?

A2: Of course. Check ASCII and look for 32-47 (DEC number), those are space and symbols. 0-31: Non Printable Characters | 32-127: Printable Characters.