Alan
Alan

Reputation: 169

A method that calls another method for Strings

I have this assignment below: I have two methods that modify strings simultaneously. I have searched on many posts but couldn't find the answer.

I want the second method to modify (call) the result of the first one. I am a neophyte to Java so thanks for your patience and understanding.

Assignment: Part 1 - Normalize Text

Write a method called normalizeText which does the following:

Removes all the spaces from your text Remove any punctuation (. , : ; ’ ” ! ? ( ) ) Turn all lower-case letters into upper-case letters Return the result. The call normalizeText(“This is some \“really\” great. (Text)!?”) should return

“THISISSOMEREALLYGREATTEXT”

Part 2 - Obfuscation

Write a method called obify that takes a String parameter (the message to be obfuscated) and returns a string in which every vowel (A, E, I, O, U, Y) is preceded by the letters “OB” (be sure to use capital letters).

If we call obify on “THISISSOMEREALLYGREATTEXT”, it should return

“THOBISOBISSOBOMOBEROBEOBALLOBYGROBEOBATTOBEXT”

My code:

public class CryptoAssessment {
    public static void main(String[] args) {
         normalizeText("This is some \“really\” great. (Text)!?"); 
    }

public static void normalizeText(String string_to_encrypt){
    String upper_string = string_to_encrypt.toUpperCase();
    String Capital_Letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    String Result_after_Normalization = "";
    for (int i = 0; i < upper_string.length(); i++) {
        if (Capital_Letters.contains(Character.toString(upper_string.charAt(i))))                 
        {
            Result_after_Normalization =  Result_after_Normalization + Character.toString(upper_string.charAt(i));
        }
    } 
    System.out.print(Result_after_Normalization); 
}
public static void Obfuscation(String string_to_Obfuscate){
    String Vowel_Letters = "AEIOUY";
    String Result_after_Obfuscation = "";
    for (int i = 0; i < string_to_Obfuscate.length(); i++) {
        if (Vowel_Letters.contains(Character.toString(string_to_Obfuscate.charAt(i))))                 
        {
            Result_after_Obfuscation =  Result_after_Obfuscation + "OB" + Character.toString(string_to_Obfuscate.charAt(i)) ;
        }
        else {
            Result_after_Obfuscation =  Result_after_Obfuscation + Character.toString(string_to_Obfuscate.charAt(i));
        }
    } 
    System.out.print(Result_after_Obfuscation); 
}

}

Upvotes: 0

Views: 303

Answers (3)

Shark
Shark

Reputation: 6620

Ah, I get your problem. You don't want to simply pring on the Console System.out - you need to return those strings back to the caller.

public static String normalizeText(String string_to_encrypt){
String upper_string = string_to_encrypt.toUpperCase();
String Capital_Letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
String Result_after_Normalization = "";
for (int i = 0; i < upper_string.length(); i++) {
    if (Capital_Letters.contains(Character.toString(upper_string.charAt(i))))                 
    {
        Result_after_Normalization =  Result_after_Normalization + Character.toString(upper_string.charAt(i));
    }
} 
System.out.print("After normalization: "+Result_after_Normalization); 
return Result_after_Normalization;
}

And lets make the other one return a String as well

public static String Obfuscation(String string_to_Obfuscate){
String Vowel_Letters = "AEIOUY";
String Result_after_Obfuscation = "";
for (int i = 0; i < string_to_Obfuscate.length(); i++) {
    if (Vowel_Letters.contains(Character.toString(string_to_Obfuscate.charAt(i))))                 
    {
        Result_after_Obfuscation =  Result_after_Obfuscation + "OB" + Character.toString(string_to_Obfuscate.charAt(i)) ;
    }
    else {
        Result_after_Obfuscation =  Result_after_Obfuscation + Character.toString(string_to_Obfuscate.charAt(i));
    }
} 
System.out.print("After obfuscation: "+Result_after_Obfuscation);
return Result_after_Obfuscation; 
}

And now the main() becomes this:

    public static void main(String[] args) {
     String result = obfuscate(normalizeText("This is some \“really\” great. (Text)!?")); 
     System.out.println("Result after doing both: "+result);
}

Was typing this out last night when i ran out of battery, so ergo the delay in answering.

Upvotes: 0

Leeqihe
Leeqihe

Reputation: 21

You can use a method's return as another method's argument, as long as the type match.
First change your methods' signature like this(make them to return a value):

public static String normalizeText(String string_to_encrypt){...}
public static String Obfuscation(String string_to_Obfuscate){...}

Then you can use the return value:

String temp = normalizeText("This is some \“really\” great. (Text)!?");
String result = Obfuscation(temp);

Or:

String result = Obfuscation(normalizeText("This is some \“really\” great. (Text)!?"));

Upvotes: 0

Bohemian
Bohemian

Reputation: 425033

To pass the result of a call to method1() to a call to method2():

method2(method1("foo"))

To complete your assignment:

public static void normalize(String str) {
    return str.replaceAll("\\W", "").toUpperCase();
}

public static void obfuscate(String str) {
    return str.replaceAll("[AEIOU]", "OB$0");
}

Upvotes: 1

Related Questions