1

In this program, I want to not only print the results but to save the results as a string. How do I do that? Also, how do I 'return' a string value that can be printed? (This program is removes all the vowels in the 'ad' string except the first letter of the word)

public class RemoveVowels {

    public static void main(String[] args) {
        String a = "";
        remove("Remove all vowels from string except the first letters");
        System.out.println();

    }

    static String remove(String a) {

        char c = '\0';
        String ss = "";
        String s = ("" + a.charAt(0));
        String sent = ("" + s);
        boolean vowel = false;
        boolean first = false;

        System.out.print(s);

        for (int i = 1; i <= (a.length() - 1); i++) {

            c = a.charAt(i);

            if (a.charAt(i - 1) == ' ')
                first = true;
            else
                first = false;

            if (((c == 'a' || c == 'e') || (c == 'i' || c == 'o')) || (c == 'u' || c == 'y'))
                vowel = true;
            else
                vowel = false;

            if ((first == true) || (vowel == false)) {
                s = ("" + c);
                System.out.print(s);

            }

        }
        return s;
    }

}
Sarah
  • 19
  • 1
  • 3
  • which result you want to print? – SpringLearner Dec 12 '17 at 09:34
  • I'm already printing it out with System.out.print(s) - but I want to be able to save that string that prints out (since it prints out one character at a time as it's looping) I also want to know how do I return the value of that entire printed string, so that I can print it in the main method. – Sarah Dec 12 '17 at 09:37
  • change s = ("" + c); to s+=("" + c); but i suggest you use StringBuilder – Sandip Solanki Dec 12 '17 at 09:40

4 Answers4

1

Simplest way to append is just using + as in s = s + c. This isn't the best way to do it performance wise since a new String is created every time (due to Strings being immutable). Instead you may use a StringBuilder e.g.

//Create outside of loop
StringBuilder sb = new StringBuilder();
//Append in loop
sb.append(c);
//Print when done
System.out.println(sb);
//Return the String to the caller
return sb.toString();
Jotunacorn
  • 496
  • 1
  • 4
  • 12
  • I like the s=s+c option. How do I use that in my code and then print out the result of the saved string? – Sarah Dec 12 '17 at 09:45
  • As I said, the StringBuilder way is better, but if you REALLY wanna use s = s + c you may simply change s = ("" + c); into s = s + c;. The result is stored in s and you may print it and return it just as you do now. – Jotunacorn Dec 12 '17 at 09:53
0

change s = ("" + c); to s+=("" + c); but i suggest you use StringBuilder

Sandip Solanki
  • 704
  • 1
  • 8
  • 15
0

First thing : you add many things not required : parenthesis, empty String as you concatenate, explicit == false, == true, etc.. It makes unecessary verbosity.

Then about your question, that :

 s = ("" + c);

could be replaced by :

 s+ = c;

It doesn't overwrite any longer s but it concatenates the c value to the previous value stored in s.

You can also use a StringBuilder :

    StringBuilder content = new StringBuilder();

    for (int i = 1; i <= (ad.length() - 1); i++) {
         ...
        c = ad.charAt(i);

        if (first  || !vowel) {
            content.append(c);
            System.out.print(content.toString());
        }
         ...
    }

Note that no one of these solutions are really better.
StringBuilder is theorically more efficient at runtime from a some number of concatenation but String concatenations in source code may be optimized both at compilation time and at runtime.
So just use the way that makes your code more readable.

davidxxx
  • 125,838
  • 23
  • 214
  • 215
-1

You might want to take a look at StringBuilder. Append the string that you're printing inside System.out.print(s); i.e. the string s to the StringBuilder object.

Return the String generated from StringBuilder object. The following code illustrates this.

    String modify(String ad) {
        StringBuilder resultBuilder = new StringBuilder();
        // part before loop goes here.        
        for(int i = 1; i <= (ad.length() - 1); i++) {
            // inside loop computation
            System.out.print(s);  // optional
            resultBuilder.append(s);
        }
        return resultBuilder.toString();
    }

You can use the returned string the way you want.

Note: An easy way would be to keep appending results to String s but that is not the best way because Strings are immutable and will cause a new string to be generated every time. I would recommend using the StringBuilder.

Yash Verma
  • 64
  • 1
  • 7