forked from dr-cs/intro-oop-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBugs2.java
More file actions
42 lines (36 loc) · 1.28 KB
/
Bugs2.java
File metadata and controls
42 lines (36 loc) · 1.28 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
public class Bugs {
public static void main(String[] args) {
String email = null;
if (args.length > 0) {
email = args[0];
} else {
System.out.println("Need command line args.");
System.exit(0);
}
// If no command line arguments, what is args.length?
//email = args[0];
// What if email not assigned a value after initialization?
int len = email.length();
// What if email, an aribitrary String, is not a valid email address?
boolean isEmailValid =
(email.endsWith(".com")
|| email.endsWith(".org")
|| email.endsWith(".edu"))
&& containsAtSymbol(email);
}
/**
* A helper method to check for the presense of the @ symbol in a String
*
* @param s a String
* @return true if s contains @, false otherwise
*/
private static boolean containsAtSymbol(String s) {
boolean containsAt = false;
// Can you spot the bugs here before we run the program?
for (int i = 0; i <= s.length(); i++) {
if (s.substring(i, i) == "@") containsAt = true;
// What if @ is early in the string?
}
return containsAt;
}
}