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 output file printwriter output; output = new printwriter(new filewriter("output.txt")); c.println("please enter sentence encrypt"); sentence = c.readline(); (int = 0; < 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 remove punctuation , spaces, , change vowels aeiou !@#$%, i'm getting error. trying output vowels replaced sentence @ bottom , reverse them.
to test strings equality, use string.equals()
. example, "a".equals(encrypt)
tests if string encrypt
capital letter a.
notice that, above, preferable put constant string first (instead of encrypt.equals("a")
) avoid possibility of null pointer exception.
if want case-insensitive matching, there's string.equalsignorecase()
.
and regarding task @ hand (e.g., remove/replace occurrences of something), might consider using regular expressions instead.
for example, replace capital letter ! use like:
encrypt.replaceall("a", "!")
or, if you'll using same regular expression pattern on , on again or want flexibility make case-insensitive patterns, then:
pattern = pattern.compile("a", pattern.case_insensitive); ... a.matcher(encrypt).replaceall("!");
Comments
Post a Comment