-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConsoleIoLecture.java
More file actions
140 lines (84 loc) · 4.06 KB
/
Copy pathConsoleIoLecture.java
File metadata and controls
140 lines (84 loc) · 4.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import java.util.Scanner;
public class ConsoleIoLecture{
public static void main(String[] args) {
/* ***************************************
print() and println()
****************************************/
// >> this:
// System.out.println("here");
// System.out.println("there");
// >> is equivalent to this:
// System.out.print("here2\n");
// System.out.print("there2\n");
// >> without the newline characters, print outputs this:
// System.out.print("here");
// System.out.print("there");
// >> to concatenate, just like JS:
// System.out.println("Hello" + " " + "World");
/* ***************************************
printf()/.format
****************************************/
//TODO TOGETHER: Print a formatted string using the following... printf(formatString, data)
System.out.printf("Hello%n");
System.out.printf("World%n");
//TODO TOGETHER:
// (1) Create a string variable
// (2) print using printf() with placeholder %s
String name = "kristen";
System.out.printf("User name is: %s. \n", name);
// TODO: print using printf() with placeholder %S
int age = 30;
System.out.printf("Age of %S: %d.\n", name, age);
//TODO: Remove the %n from the first printf. What do you notice?
// doesn't print to next line, stays on same line
// >>>> Multiple Variables
//TODO TOGETHER:
// (1) Create 2 variables - int and string
// (2) Print using printf() and placeholders
// note: d : decimal integer [byte, short, int, long]
int three = 3;
String typeOfPet = "cats";
System.out.printf("I have %d %s%n", three, typeOfPet);
//TODO:
// (1) Print using printf(), multiple variables, and placeholders
String name2 = "Kristen";
int age2 = 30;
String typeOfPet2 = "dog";
System.out.printf("Hello %S! You have entered you are %d years old and have a %s.", name2, age2, typeOfPet2);
// >>>> Currency
// %f => float/double formatting
//We can control the precision of the decimal using .___
//TODO TOGETHER:
// (1) Create int variable currencyPennies
// (2) Print variable using currency formatting
int currencyPennies = 1000;
System.out.printf("I'll sell you land for $%d.2f of an acre!%n", currencyPennies/100);
/* ***************************************
USER INPUT
****************************************/
// The Scanner class allows us to get data input that the user enters into the console.
// Scanner Docs ==> http://www.cs.utexas.edu/users/ndale/Scanner.html
// >>>> .next() method
// .next() captures each input usually signified by whitespace. The input is returned as a string
// >>>> .nextInt() method
// .nextInt() captures the first valid int value
// >>>> .nextLine() method
// .nextLine() Returns the rest of the current line
/* ****************** NOTE ********************
*Quirk of using next() then nextLine()...
*https://stackoverflow.com/questions/13102045/scanner-is-skipping-nextline-after-using-next-or-nextfoo
*
*That's because the Scanner.nextInt method does not read the newline character in your input created by hitting "Enter,"
*the call to Scanner.nextLine returns after reading that newline.
*You will encounter the similar behaviour when you use Scanner.nextLine() after Scanner.next()
*or any Scanner.nextFoo method (except nextLine itself).
*/
Scanner sc = new Scanner(System.in);
System.out.print("Please enter your favorite number: ");
int num = sc.nextInt();
System.out.println(num);
System.out.print("Please enter your favorite words: ");
String words = sc.nextLine();
System.out.println(words);
}
}