import java.io.*;
import hsa.Console;
import java.awt.*;
public static void main(String[] args) throws IOException {
c = new Console();
String sentence;
String encrypt = "";
String vowels = "AEIOUaeiou";
final String PUNCTAUTION = ".,;?!\"\\/\' -";
StringBuffer removePunctation = new StringBuffer();
StringBuffer thirdLetters = new StringBuffer();
char tempChar;
//Open The Output File
PrintWriter output;
output = new PrintWriter(new FileWriter("output.txt"));
c.println("Please enter the sentence you would like to encrypt");
sentence = c.readLine();
for (int i = 0; i < sentence.length(); i++) {
tempChar = sentence.charAt(i);
if (PUNCTAUTION.indexOf(tempChar) == -1) {
encrypt = encrypt + tempChar;
}
}
if (encrypt == 'A') {
sentence.replace('A', '!');
} else if (encrypt == 'I') {
sentence.replace('I', '#');
} else if (encrypt == 'E') {
sentence.replace('E', '@');
} else if (encrypt == 'O') {
sentence.replace('O', '$');
} else if (encrypt == 'U') {
sentence.replace('U', '%');
}
c.println(encrypt.toString().toUpperCase());
output.println(encrypt.toString().toUpperCase());
}
I'm trying to remove all punctuation and spaces, and change the vowels AEIOU to !@#$%, but I'm getting an error. I am also trying to output the vowels I replaced from the sentence at the bottom and reverse them.
To test strings for equality, use String.equals()
. For example, "A".equals(encrypt)
tests if the string encrypt
is the capital letter A.
Notice that, as above, it is preferable to put the constant string first (instead of encrypt.equals("A")
) to avoid the possibility of a null pointer exception.
If you want case-insensitive matching, there's also String.equalsIgnoreCase()
.
And regarding your task at hand (e.g., remove/replace all occurrences of something), you might consider using regular expressions instead.
For example, to replace all capital letter A with ! you could use something like:
encrypt.replaceAll("A", "!")
Or, if you'll be using the same regular expression pattern over and over again or want the flexibility to make case-insensitive patterns, then:
Pattern a = Pattern.compile("A", Pattern.CASE_INSENSITIVE);
...
a.matcher(encrypt).replaceAll("!");