More on Operators and Assignments
PLUS: Reading Input from the
Keyboard!!
If you can't write it down in English, you can't code it.
-- Peter Halpern
If you lie to the computer, it will get you.
-- Peter Farrar
One person's constant is another person's variable.
-- Alan Perlis
But
First... More on Operators
Operator precedence
- highest to lowest
- Parentheses
- * / % <--- performed in
order from left to
right
- + -
<--- performed in
order from left to right
Note: Recall that + is an
operator that adds 2 numbers, but it also is the string concatenation
operator when one or both operands are Strings.
Examples:
int result = 2 * ((16 - 4) / 2); // result is set to 12
String s = 4 + "6" + 2; // s is set to "462"
result = 13 % 2 - 1; // result is set to 0
result = 13 % 2 * 4; // result is set to 4
Increment
and Decrement Operators
- The increment operator ++ adds 1 to
a number
- result ++; // The value stored in
result is
increased by 1
- The decrement operator -- subtracts 1 from
a number
- result --; // value stored in
result is decreased by
1
- These operators can come before or after
the variable/value:
Example:
int count = 1;
count ++;
System.out.println(count);
-- count;
System.out.println(count);
Question: What is the output?
Example: Try out the following
code:
int count = 1;
int n = 2 + count++;
And then try:
int count = 1;
int n = 2 + ++count;
Assignment
Operators
- Combine assignment with an operation
Ex:
count += 5;
is equivalent to
count = count + 5;
Ex:
int result = 2;
result *= 4; // result is 8
Ex:
int count1 = 3;
int count2 = 6;
int product *= count1 + count2; // what value is stored in
product?
is equivalent to
int product = product * (count1 + count2);
Ex:
String s = "hello";
s +=
"
world";
// s is "hello world"
is equivalent to
s = s + " world";
Input from the Keyboard
- Read data from the user during program
execution
- The Scanner class is in the java.util
package
- To read input from the user (via the
keyboard), we must
create a Scanner object associated with the keyboard.
- In Java, we create objects using the new operator.
Scanner scan = new Scanner(System.in);
- This Scanner object, scan, is created by the new
operator
and a call to a constructor,
which sets up an object.
- System.in represents the standard input stream (the
keyboard)
- Whitespace (spaces, tabs, newlines)
separate the input
elements, called tokens, from
each other.
- Useful Scanner methods:
- String next() - returns the next String
entered by the user
- String nextLine() = returns all input
remaining on the
current line
- double nextDouble() - returns the next
input (token). If
the next input is not of type double, an exception will
be thrown.
- Exceptions are Java's way of
indicating that an error
has occurred during program execution. More on this later.
- int nextInt() - returns the next input
token as an int (or
throws an exception if next token is not an int).
Example:
Scanner scan = new
Scanner(System.in);
// Prompt the user to enter an
integer
System.out.print("Please enter an
integer: ");
// Read and print the integer
int value = scan.nextInt();
System.out.println(value);
Example: Write a program that
asks the user to enter a line of text, and then prints the first word
in the line to the screen.
Example: Write a program that
asks the user to enter a line of text, and then prints the line to the
screen.
Example: Write a program that
asks the user to enter 3 (integer) exam scores, and then prints the
average.