parent
40092ab92a
commit
4fd94efc85
6 changed files with 135 additions and 0 deletions
@ -0,0 +1,11 @@ |
||||
<?xml version="1.0" encoding="UTF-8"?> |
||||
<module type="JAVA_MODULE" version="4"> |
||||
<component name="NewModuleRootManager" inherit-compiler-output="true"> |
||||
<exclude-output /> |
||||
<content url="file://$MODULE_DIR$"> |
||||
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" /> |
||||
</content> |
||||
<orderEntry type="inheritedJdk" /> |
||||
<orderEntry type="sourceFolder" forTests="false" /> |
||||
</component> |
||||
</module> |
@ -0,0 +1,54 @@ |
||||
import java.util.Scanner; |
||||
|
||||
/** |
||||
* Created by noah on 6/2/2016. |
||||
*/ |
||||
public class Exceptions { |
||||
|
||||
public static void main(String... args) { |
||||
Exceptions e = new Exceptions(); |
||||
e.Run(); |
||||
} |
||||
|
||||
private void Run() { |
||||
Scanner scanner = new Scanner(System.in); |
||||
|
||||
System.out.print("Please enter the letter n: "); |
||||
String s = scanner.nextLine(); |
||||
if (s.isEmpty()) |
||||
s = null; |
||||
try { |
||||
if (!s.equals("n")) { |
||||
throw new NotNException("NOT n!"); |
||||
} |
||||
if (!s.equalsIgnoreCase("N")) { |
||||
throw new OtherException("NOT N!"); |
||||
} |
||||
} catch (NotNException e) { |
||||
System.out.println("That's not an 'n'."); |
||||
Run(); |
||||
} catch (OtherException oe) { |
||||
System.out.println("That's not an n, but that's alright."); |
||||
} catch (NullPointerException np) { |
||||
System.out.println("You didn't enter anything"); |
||||
} |
||||
|
||||
System.out.println("Done"); |
||||
|
||||
} |
||||
|
||||
|
||||
//Inner classes yay!
|
||||
private class NotNException extends Exception { |
||||
public NotNException(String s) { |
||||
super(s); |
||||
} |
||||
} |
||||
|
||||
private class OtherException extends Exception { |
||||
public OtherException(String message) { |
||||
super(message); |
||||
} |
||||
} |
||||
|
||||
} |
@ -0,0 +1,11 @@ |
||||
<?xml version="1.0" encoding="UTF-8"?> |
||||
<module type="JAVA_MODULE" version="4"> |
||||
<component name="NewModuleRootManager" inherit-compiler-output="true"> |
||||
<exclude-output /> |
||||
<content url="file://$MODULE_DIR$"> |
||||
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" /> |
||||
</content> |
||||
<orderEntry type="inheritedJdk" /> |
||||
<orderEntry type="sourceFolder" forTests="false" /> |
||||
</component> |
||||
</module> |
@ -0,0 +1,36 @@ |
||||
import java.util.Scanner; |
||||
|
||||
/** |
||||
* Created by noah on 6/2/2016. |
||||
*/ |
||||
public class LetterCounter { |
||||
|
||||
public static void main(String... args) { |
||||
Scanner scanner = new Scanner(System.in); |
||||
System.out.print("Please enter a string: "); |
||||
String input = scanner.nextLine(); |
||||
input = input.toUpperCase(); |
||||
int[] count = new int[26]; |
||||
//loops through all characters using ASCII (starting at 65)
|
||||
for (int ascii = 65; ascii < 90; ascii++) { |
||||
int i = 0; |
||||
do { |
||||
char c = (char) ascii; |
||||
if (i == 0) { |
||||
i = input.indexOf(c, i); |
||||
} else { |
||||
i = input.indexOf(c, i + 1); |
||||
} |
||||
if (i == 0) |
||||
i++; |
||||
if (i > -1) |
||||
count[ascii - 65]++; |
||||
} while (i > -1); |
||||
} |
||||
for (int i = 0; i < 26; i++) { |
||||
System.out.format("%5c%5d\n", (char) i + 65, count[i]); |
||||
} |
||||
|
||||
} |
||||
|
||||
} |
@ -0,0 +1,21 @@ |
||||
/** |
||||
* Created by noah on 6/2/2016. |
||||
*/ |
||||
public class PhoneTokenizor { |
||||
|
||||
public static void main(String... args) { |
||||
String testPhoneNumber = "(555) 555-5555"; |
||||
|
||||
String[] split = testPhoneNumber.split("[)]"); |
||||
String areaCode = split[0].substring(1); //Seen as I can only use .split() I can't use .find() to select the first instance of numbers [1-9] to make this easier
|
||||
String[] secondSplit = split[1].split("[-]"); |
||||
String first3 = secondSplit[0].substring(1); |
||||
String last4 = secondSplit[1]; |
||||
|
||||
String phoneNumber = first3 + last4; |
||||
System.out.println("Area code: " + areaCode); |
||||
System.out.println("Phone number: " + phoneNumber); |
||||
System.out.println("Done"); |
||||
} |
||||
|
||||
} |
Loading…
Reference in new issue