0% found this document useful (0 votes)
168 views36 pages

Oracle Java Academy 5.1

1. Scanner can be used to read input from the keyboard or files, but a common mistake is forgetting to include input prompts which can cause a program to appear "hung" while waiting. 2. Files like System.in are associated with standard input sources like keyboards, and Scanner can read from File objects as well as input streams. 3. Programs need code to handle invalid data types, called "bulletproofing", to prevent crashes from unexpected input.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
168 views36 pages

Oracle Java Academy 5.1

1. Scanner can be used to read input from the keyboard or files, but a common mistake is forgetting to include input prompts which can cause a program to appear "hung" while waiting. 2. Files like System.in are associated with standard input sources like keyboards, and Scanner can read from File objects as well as input streams. 3. Programs need code to handle invalid data types, called "bulletproofing", to prevent crashes from unexpected input.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

1

2
3
When you are new to programming you will sometimes forget this and then wonder why the program is
"hung" when it is just waiting for user input.

4
Technically, [Link] is a file that is associated with the keyboard or "standard input". Scanner can be
used with File objects to read from a text file.

5
6
As a programmer you will need to write code that will handle situations where invalid data types are
entered, i.e., "twenty" when an int value of 20 is expected. Professional programs will have methods
that test data for obvious and not so obvious invalid data. This is sometimes referred to as "bullet
proofing" the code.

7
Not closing Scanner objects or other files can lead to tied up resources or even to corrupted files.
Always close Scanner objects as soon as possible.

8
In Java, a common syntax error is to use = when == was intended. You should read = not as "equals" but
rather, "is assigned".
For more information on these operators as well as those on slide 12, review the precedence table at
[Link]

9
10
11
12
13
14
15
You have already came across the if-else statement in both the Alice and Greenfoot sections.

16
17
If there is more than one statement for the if or else, they must be enclosed in {braces}. The following is
a common logic error:
if( false )
[Link]("I never print");
[Link]("I always print");

18
19
20
21
22
The default:, like the optional else in an if-else statement, is also optional.
Remember that you use a : for the case statements not a ; as that represents the end of a statement.
The switch is prefered over nested if-else because it is easier to read. However, the switch does not
work well with ranges:
if(age >=13 && <=19)
[Link]("Teenager");

as opposed to:
switch(age)
case 13:
case 14:
case 15:
case 16:
case 17:
case 18:
case 19:
[Link]("Teenager");
break;

23
24
25
26
27
28
29
30
case 2: takes into account leap year rules implemented with the Gregorian calendar. A leap year every
4 years gives about 3 too many leap days every 400 years. Thus, while 2000 was a leap year, 1800 and
1900 were not and 2100 will not be.
For fun, go to [Link] and enter the year 1752 and the month of September.
This was the year that the United Kingdom converted from the Julian calendar to the Gregorian
calendar. The missing days were to get the seasons back into alignment with the calendar. Other
countries adopted the Gregorian calendar in different years with Greece being one of the last to change
in 1923.

31
The ternary operator is sometimes called the conditional operator. It is the only operator in Java to
have 3 operands.

32
33
34
35
36

You might also like