1

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?

2
  • 1
    declare the variable at the beginning, a variable is generally is not visible outside a scope { } Commented Oct 3, 2015 at 16:13
  • as Lorenzo says, the variable print only exists within the for loop. Define it before the for loop and you'll be able to print it. Commented Oct 3, 2015 at 16:15

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you this worked except the fact that for the shift value I have to use in the encryptWord method I have to declare another value to equalize with the formula (shift * (count + 1) + 1) otherwise I get a wrong result.(That's my bad though :D)
0

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;
    }

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.