Lecture07 Arrays and Exceptions
Lecture07 Arrays and Exceptions
Programming
Arrays and exceptions
• Arrays
• Exceptions
Array
• s programming construct
An array is a simple but powerful
• Consider this cumbersome expression
0 1 2 3 4 5 6 7 8 9
scores 79 87 94 82 67 98 87 81 74 91
scores[2]
scores 79 87 94 82 67 98 87 81 74 91
Array
• s
An array stores multiple values of the same type
• That type can be primitive types or objects
• Therefore, we can create
– an array of integers
– an array of doubles
– an array of characters, or
– an array of String objects etc.
• In Java, the array itself is an object
Declaring
• The scores Arrays
array could be declared in either of
the following ways:
index++)
System.out.print (list[index] + " ");
0 }10 20 0 999 20
}
Practic
• e
Create an array that hold 5 integers
• Write a For loop to loop 5 times, each time prompting the
user to enter an integer
• Store each integer in the array
scores.length
public class ReverseNumbers
Array size = 3
{
Enter number 1:
public static void main 1
(String[] args)
Enter number 2:
{ 2
double[] numbers = new Enter number 3:
double[3 ]; String 3
inputNum;
System.out.println (“Array size = " + The numbers in
numbers.length); reverse: 3.0 2.0
for (int index = 0; index < numbers.length; 1.0
index++)
{
inputNum
=JOptionPane.showInputDi
alog ("Enter number " +
(index+1) + ": ");
numbers[index] =
Double.parseDouble(inputNum);
}
• Examples:
int[ ] units = {147, 323, 89, 933, 540, 269, 97, 114};
• Using an array
– Very similar to java code
try
{
int num = Integer.parseInt(“doh”);
}
catch (NumberFormatException e)
{
System.out.println("Error: Not an
integer");
}
Valid data
• entry
A robust program will not allow erroneous data input by a
user to cause it to crash
– If invalid data is entered it needs to be caught
do
{
numStr
=
JOption
Pane.sh
owInpu
tDialog
("Enter
an
integer
whilebetwee
(num < 1 ||
Sentinels of
• Flags
Another name for a sentinel variable used for an event
controlled loop is a “flag”
• A flag indicates something of interest for program
control
• In exceptions if there is no valid number or range of
numbers – you can use a boolean variable to flag a valid or
invalid state
while (invalidNumber)
this is of course equivalent to and better programming style than:
while (invalidNumber == true)
boolean invalidNumber = true;
do
{
String userInputStr = JOptionPane.showInputDialog
("Enter an integer: ");
try
{
userInput =
Integer.parseInt(userInputStr);
invalidNumber = false;
}
catch (NumberFormatException e)
{
JOptionPane.showMessageDialog
(null, “That is not an integer");
Pseudocod
• e with indentation and END etc as
Show try and catch blocks
with loops and decisions
• Naming the end eg ENDTRY etc is only useful if there are
many lines of code in between
DO
numStr prompt user to "Enter an integer
between 1 and 10" TRY
num = Integer.parseInt(numStr);
END
CATCH
(NumberFormatExcepti
on) display error
messge
END
WHILE (num < 1 OR num >
10)
Lecture
Outcomes
Today we have covered:
– arrays
– initializer lists
– passing individual array elements to methods
– passing arrays to methods
– exceptions
• Questions?