-1
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.

Makoto
  • 104,088
  • 27
  • 192
  • 230
user2353211
  • 13
  • 1
  • 2
  • A few general remarks while reformatting your code: You don't need `StringBuffer`, your `Console` collides with `java.io.Console`, and `String` is immutable, so calling `sentence.replace()` will create a new `String`, not modify the existing one. – Makoto May 06 '13 at 02:56
  • I did indeed, deleted the comment. – greedybuddha May 06 '13 at 03:01
  • I tried what they told me But nothing worked – user2353211 May 06 '13 at 03:06

3 Answers3

0

As the compiler is trying to tell you, you can't use == to compare a string with a character. The == operator works differently for primitives (such as a char) and reference types (such as a String) so conditions like if(encrypt == 'U') are nonsensical.

Matt Ball
  • 354,903
  • 100
  • 647
  • 710
  • Then what can i replace it with? – user2353211 May 06 '13 at 02:43
  • Not sure, because the code doesn't make much sense to me. The string `encrypt` presumably ends up longer than 1 character, given the body of the `for` loop. Why would it ever be exactly 1 character? What are you trying to check? Additionally, strings are immutable, so `sentence.replace(...)` does not mutate `sentence`. It returns a new string, which your code discards. Like I said, the code does not make much sense to me. – Matt Ball May 06 '13 at 02:45
0

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("!");
Community
  • 1
  • 1
quietmint
  • 13,885
  • 6
  • 48
  • 73
  • I am trying to Look for any A's in my Sentence that is inputted by the user and replace it with ! what do i do? – user2353211 May 06 '13 at 02:56
0

A string is an object, while a char is a privative data type.

What you're basically asking the computer to do is a lot like asking it to compare a number to a list of numbers. It can't, simply because those are two entirely separate things.

As stated below, you want to use something like

  if(encrypt.equals("a")){ 

to campare a String to a char.

r0nk
  • 793
  • 2
  • 6
  • 7