try
BlockHomework 7 is not due this Sunday.
Instead it is due the Sunday after the Mid-term Exam.
This will give you time to study for the exam.
You will find the assignment here.
If you have a problem or a question, make a post on the Class Discussion Area.
I have posted homework 6 here.
It is due this coming Sunday at 11:59 PM.
The mid-term exam will be given on Monday, March 23rd.
It will consist of questions like those on the quizzes along with questions asking you to write short segments of Java code.
60% of the points on this exam will consist of questions from the Ungraded Class Quizzes.
The other 40% will come from four questions that ask you to write a short segment of code.
The last class before the exam, Friday, March 13th, will be a review session.
You will only be responsible for the material in the Class Notes for that class on the exam.
The Mid-term is a closed book exam.
The due date for homework assignments 2 - 6 is Friday, March 20th at 11:59 PM.
It is also the due date for Class Exercises 2 - 17
That is the Friday of the Spring Break.
Any of these assignments submitted after that date will receive a score of 0.
s1 = "dog"
s2 = "dog"
s1 = "cat"
s1 = s1 + "food"
String special_characters = "~!@#$%^&*()_+"
Positive | The first string comes after the second string in dictionary order |
Zero | The two strings are the same |
Negative | The first string comes beforethe second string in dictionary order |
import PACKAGE_NAME.CLASSNAME;
import PACKAGE_NAME.*;
import java.util.CLASS_NAME
Scanner SCANNER_VARIABLE = new Scanner(System.in);
Method | Description |
---|---|
next() | Reads and returns the next token as a String |
nextDouble() | reads and returns a double value |
nextInt() | reads and returns an int value |
nextLine() | reads and returns the next line of input as a String |
public class SyntaxError1 {
public static void main (String[] args){
System.out.println("Hello world!")
}
}
$ javac SyntaxError1.java
SyntaxError1.java:3: error: ';' expected
System.out.println("Hello world!")
^
1 error
public class SyntaxError2 {
public static void main (String[] args){
String greeting = "Hello world!";
System.out.println(greet);
}
}
$ javac SyntaxError2.java
SyntaxError2.java:4: error: cannot find symbol
System.out.println(greet);
^
symbol: variable greet
location: class SyntaxError2
1 error
public class SyntaxError3 {
public static void main (String[] args){
String greeting = "Hello world!
System.out.println(greet);
}
}
$ javac SyntaxError3.java
SyntaxError3.java:3: error: unclosed string literal
String greeting = "Hello world!
^
1 error
public class SyntaxError4 {
public static void main (String[] args){
String greeting = "Hello world!";
System.out.println(greet;
}
}
$ javac SyntaxError4.java
SyntaxError4.java:4: error: ')' expected
System.out.println(greet;
^
1 error
public class AverageBad { public static void main (String[] args){ int num_1 = Integer.parseInt(args[0]); int num_2 = Integer.parseInt(args[1]); double average = num_1 + num_2 / 2; System.out.println("Average: " + average); } } $ java AverageBad 2 3 Average: 3.0
public class Average { public static void main (String[] args){ double total = 0; for (int i = 0; i < args.length; i++){ total += Integer.parseInt(args[i]); } System.out.println("The average is " + total/args.length); } }
$ java Average 1 2 3 4 5 The average is 3.0
$ java Average 1 2 3 4 five
Exception in thread "main" java.lang.NumberFormatException: For input string: "five"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:580)
at java.lang.Integer.parseInt(Integer.java:615)
at Average.main(Average.java:5)
Segmentation fault: Core dumped
try
/catch
statement which has the following form
try{
STATEMENT;
STATEMENT;
...
} catch (EXCEPTION_TYPE_NAME EXCEPTION VARIABLE){
STATEMENT;
STATEMENT;
...
}
try
code block
execute normally unless a runtime error occurs
try
blockcatch
code block and executes those statements
$ java Average 1 2 3 4 five
Exception in thread "main" java.lang.NumberFormatException: For input string: "five"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:580)
at java.lang.Integer.parseInt(Integer.java:615)
at Average.main(Average.java:5)
total += Integer.parseInt(args[i]);
try
block
of a try
/catch
statement
catch
blockpublic class Average2 { public static void main (String[] args){ double total = 0; try { for (int i = 0; i < args.length; i++){ total += Integer.parseInt(args[i]); } System.out.println("The average is " + total/args.length); } catch (NumberFormatException e) { System.out.println("Could not convert argument to integer"); } } }
public class Add2Numbers { public static void main (String[] args){ int num_1 = Integer.parseInt(args[0]); int num_2 = Integer.parseInt(args[1]); System.out.println(num_1 + num_2); } }
$ java Add2Numbers 4 5 9
$ java Add2Numbers 4 five Exception in thread "main" java.lang.NumberFormatException: For input string: "five" at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65) at java.lang.Integer.parseInt(Integer.java:580) at java.lang.Integer.parseInt(Integer.java:615) Add2Numbers.main(Add2Numbers.java:4)
try
code_block
catch
that prints an error message
public class Average2 {
public static void main (String[] args){
double total = 0;
try {
for (int i = 0; i < args.length; i++){
total += Integer.parseInt(args[i]);
}
System.out.println("The average is " + total/args.length);
} catch (NumberFormatException e) {
System.out.println("Could not convert argument to integer");
}
}
}
$ java Average2 1 2 3 4 5 The average is 3.0
$ java Average2 1 2 3 4 five Could not convert argument to integer
try
Blocktry
block
try { for (int i = 0; i < args.length; i++){ total += Integer.parseInt(args[i]); } System.out.println("The average is " + total/args.length); }
total += Integer.parseInt(args[i]);
try
block
public class Average3 { public static void main (String[] args){ double total = 0; for (int i = 0; i < args.length; i++){ try { total += Integer.parseInt(args[i]); } catch (NumberFormatException e) { System.out.println("Cannot convert " + args[i] + " to integer"); } } System.out.println("The average is " + total/args.length); } }
$ java Average3 1 2 3 4 5 The average is 3.0
Cannot convert five to integer The average is 2.0
catch
block
System.exit(0)
public class Average4 { public static void main (String[] args){ double total = 0; for (int i = 0; i < args.length; i++){ try { total += Integer.parseInt(args[i]); } catch (NumberFormatException e) { System.out.println("Cannot convert " + args[i] + " to integer"); System.exit(0); } } System.out.println("The average is " + total/args.length); } }
$ java Average4 1 2 3 4 five Cannot convert five to integer
Garbage in Garbage out
while
loops are often used to validate user entered data
get value from user
while value is not greater than 0:
print error message
get a new value from the user
import java.util.Scanner; public class AboveZero { public static void main (String[] args){ Scanner console = new Scanner(System.in); int value = get_value_above_zero(console); System.out.println(value); } public static int get_value_above_zero(Scanner con){ System.out.print("Integer greater than zero: "); String input = con.next(); int number = Integer.parseInt(input); while (number <= 0){ System.out.println(number + " is not greater than 0"); System.out.print("Integer greater than zero: "); number = con.nextInt(); } return number; } } $ java AboveZero Integer greater than zero: -1 is not greater than 0 Integer greater than zero: 0 0 is not greater than 0 Integer greater than zero: 5 5user input appears in blue
input
statement if we make sure the while loop always runsFalse
True
true
if the input is good
set a variable to false
while variable is not true
get input from the user and store in a variable
if the input value is good
set the variable to true
else:
print an error message
import java.util.Scanner; public class AboveZero2 { public static void main (String[] args){ Scanner console = new Scanner(System.in); int value = get_value_above_zero(console); System.out.println(value); } public static int get_value_above_zero(Scanner con){ System.out.print("Integer greater than zero: "); String input; boolean done = false; int number = 0; while (! done){ System.out.print("Integer greater than zero: "); number = con.nextInt(); if (number > 0){ done = true; } else { System.out.println("The integer must be greater than 0"); } } return number; } }
$ java AboveZero2 Integer greater than zero: Integer greater than zero: -5 The integer must be greater than 0 Integer greater than zero: 0 The integer must be greater than 0 Integer greater than zero: 7 7
while
loopif
statements
or while
loops later in the code
while
looptrue
return
statement inside the loopimport java.util.Scanner; public class AboveZero3 { public static void main (String[] args){ Scanner console = new Scanner(System.in); int value = get_value_above_zero(console); System.out.println(value); } public static int get_value_above_zero(Scanner con){ System.out.print("Integer greater than zero: "); String input; int number = 0; while (true){ System.out.print("Integer greater than zero: "); number = con.nextInt(); if (number > 0){ return number; } else { System.out.println("The integer must be greater than 0"); } } } } $ java AboveZero3 Integer greater than zero: Integer greater than zero: -67 The integer must be greater than 0 Integer greater than zero: 0 The integer must be greater than 0 Integer greater than zero: 31 31