kafka96
kafka96

Reputation: 31

How to print String

I have this String which I want to return but I cannot because it says that "print" cannot be resolved as a variable. This is my code:

public static String enrcyptText(String str, int shift){
        int count = 0;
        String[] parts = str.split("[\\W]");
        for(String word : parts){ 
                shift = shift * (count + 1) + 1;
                count++;
                encryptWord(word, shift);
                String[] phrase = new String[]{word};
                String print = String.join(" ", phrase);
            }
        return print;
    }

any Idea?

Upvotes: 1

Views: 131

Answers (3)

Tunaki
Tunaki

Reputation: 137064

You have a logic error in your code: you are encrypting correctly each word but you are not building correctly the encrypted phrase. In each iteration of the loop, you are recreating the phrase when you should be adding element to the phrase array.

public static String enrcyptText(String str, int shift) {
    int count = 0;
    String[] parts = str.split("[\\W]");
    String[] phrase = new String[parts.length]; // initialising an array containing each encrypted word
    for (String word : parts) {
        shift = shift * (count + 1) + 1;
        count++;
        String encryptedWord = encryptWord(word, shift);
        phrase[count - 1] =  encryptedWord; // updating the encrypted phrase array
    }
    return String.join(" ", phrase); // joining the phrase array
}

In this code, we are creating a phrase array before the loop. In each iteration, we update this array with the encryped word. When we have all the encrypted words, the loop terminates and we join all of the parts together.

I am also guessing that the encryptedWord actually returns the encrypted word. This method can't modify the word given as parameter.

Upvotes: 0

T.J. Crowder
T.J. Crowder

Reputation: 1074088

There are several problems there.

  1. You've declared print only within the loop body. It doesn't exist outside of it. So you need to move your String print outside the loop.

  2. You're also assigning to it on every loop iteration, which will overwrite the previous value it had. It's unclear what you want to do instead, but you're not going to want to do that.

  3. These two lines also don't make any sense:

    String[] phrase = new String[]{word};
    String print = String.join(" ", phrase);
    

    Since there will only be one entry in phrase, you'll end up with print having the same value word had.

  4. You seem to expect that encryptWord can modify the string passed into it. It can't.

Taking a stab at it, I'm thinking your goal is to "encrypt" individual words from a sentence, then recombine the result into a space-delimited set of encrypted words. If so, see comments:

public static String enrcyptText(String str, int shift){
    int count = 0;
    String[] parts = str.split("[\\W]");
    // For updating the array, better to use the classic
    // for loop instead of the enhanced for loop
    for (int i = 0; i < parts.length; ++i){ 
        shift = shift * (count + 1) + 1;
        count++;                      // Could do this before previous line and remove the + 1 in (count + 1)
        parts[i] = encryptWord(parts[i], shift); // See note below
    }
    return String.join(" ", parts);
}

Note that I'm using a return value from encryptWord. That's because strings in Java are immutable (cannot be changed), and so encryptWord can't change what we pass into it; it can only give us back a new string to use instead.

Upvotes: 1

Kumar Abhinav
Kumar Abhinav

Reputation: 6675

print variable has scope inside the braces.You should move the print variable outside braces to make it visible to the code.Also,since it is a local variable ,print should be initialized with a default value(in my case,it is null).The compiler will complain that print remain uninitialized(though this is not related to the main question)

public static String enrcyptText(String str, int shift){
        int count = 0;
        String[] parts = str.split("[\\W]");
        String print = null;
        for(String word : parts){ 
                shift = shift * (count + 1) + 1;
                count++;
                encryptWord(word, shift);
                String[] phrase = new String[]{word};
                print = String.join(" ", phrase);
            }
        return print;
    }

Upvotes: 0

Related Questions