Our task for the last few weeks has been to create a simple program that replaces vowels in a String input with something else (I've chosen the letter "z", because, you know, "zzzz..."). You can find my code below:
import java.util.Scanner;
class VowelReplacer {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String string = scanner.nextLine();
char[] vowels = {'a', 'e', 'i', 'o', 'u'};
for (char c : vowels) {
// System.out.println("Value of c: " + c);
string = string.replace(c, 'z');
// System.out.println(string);
}
System.out.println(string);
}
}
And here's a program that removes all occurrences of a user-specified String from a String input:
import java.util.Scanner;
import java.util.regex.Pattern;
import java.util.regex.Matcher;
public class StringRemover {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Please type a word or sentence:");
String stringToChange = scanner.nextLine();
Pattern pattern = Pattern.compile(stringToChange);
System.out.println("Please type a String:");
String stringToRemove = scanner.nextLine();
Matcher matcher = pattern.matcher(stringToChange);
if (matcher.find()) {
stringToChange = stringToChange.replaceAll(stringToRemove, "");
}
System.out.println("Resulting String is \"" + stringToChange + "\"");
}
}
Please note that the second of these two programs has one major limitation - it's case-sensitive.
When you're coding around in circles and editing line after line of mystifying code in dismay, when you're exhausted, mind numb and flickering in and out of consciousness, from a tenuous session of extreme programming, this is the one place to turn to for that little bit of inspiration, that spark which sets off a genius idea, and the occasional chuckle from a terrible joke or weird phrase.
Friday, 20 September 2013
Wednesday, 18 September 2013
Here we go again!
Woo! Hello everyone! After a depressing, boring, totally unsatisfying summer holiday, I'm back on the roll - and this year, we're doing JAVA!
I have to tell you, I have not been so excited in a very, very long time. Java is my all-time favourite language (well, out of the three I know, my knowledge of two of which is extremely limited). Sure, it's strict, but that's what I love about it - you simply won't let you go in completely the wrong direction and potentially make a huge coding blunder. It's object-oriented, its syntax is simple and consistent, and it pioneered portability with its "Write Once, Run Anywhere" coding philosophy.
As I said in the header thing at the top of this blog, I'm currently making a "top-secret" GUI program (except just about everyone in my class knows what it is). So far, I've incorporated various Java and OO features into the program, including class inheritance, static utility methods, abstract classes, inner classes, implementation of interfaces... Yeah, snazzy stuff!
Stay tuned for updates on the progress of Project TestMaker (you can probably guess what it does now), and maybe a few posts on various bits of Java.
I have to tell you, I have not been so excited in a very, very long time. Java is my all-time favourite language (well, out of the three I know, my knowledge of two of which is extremely limited). Sure, it's strict, but that's what I love about it - you simply won't let you go in completely the wrong direction and potentially make a huge coding blunder. It's object-oriented, its syntax is simple and consistent, and it pioneered portability with its "Write Once, Run Anywhere" coding philosophy.
As I said in the header thing at the top of this blog, I'm currently making a "top-secret" GUI program (except just about everyone in my class knows what it is). So far, I've incorporated various Java and OO features into the program, including class inheritance, static utility methods, abstract classes, inner classes, implementation of interfaces... Yeah, snazzy stuff!
Stay tuned for updates on the progress of Project TestMaker (you can probably guess what it does now), and maybe a few posts on various bits of Java.
Subscribe to:
Posts (Atom)