0

How come the last line of this code is not picking up the string filename in the code?

if (ressound == R.id.sound1) {
    String filename = "sound1" + ".ogg";
} else {
    String filename = "sound1" + ".ogg";
}
boolean exists = (new File(path)).exists();   
if (!exists) { new File(path).mkdirs(); }   

FileOutputStream save;
try { 
    save = new FileOutputStream(path + filename);  
3
  • You'll want to fix your indentation too as proper indentation and formatting makes it much easier for both you and us to debug your code. Commented Feb 5, 2011 at 21:22
  • 2
    Oddly you are setting filename to "sound1"+".ogg" in both cases, so why have an if-else at all? Commented Feb 5, 2011 at 21:25
  • 1
    What I think is even more odd is that you seem to concatenate two strings where you could simply put it into one. The way you are doing it now calls a StringBuilder internally which would not be required if you'd put it as one string like this String filename = "sound1.ogg". Commented Feb 5, 2011 at 21:33

2 Answers 2

14

You're declaring the variable in the scope of the if respectively else branch. Out of this scope the variable is not accessible.

Use this instead:

String filename;
    if (ressound == R.id.sound1) {
           filename="sound1"+".ogg";
       } else{
            filename="sound1"+".ogg";
       }
             boolean exists = (new File(path)).exists();   
             if (!exists){new File(path).mkdirs();}   

             FileOutputStream save;
             try { 
              save = new FileOutputStream(path+filename);  
Sign up to request clarification or add additional context in comments.

3 Comments

btw I see now that bot branches are doing the same thing. Is that maybe the problem``
You don't need to initialize filename to "" as long as you assign it a value in both branches (the if and else blocks). You can use String filename; alone. Seeing an initialized value would make me expect a case where it doesn't get changed.
Yes thats true. I'll change it
3

Your code as it is won't even compile as both filename variables you've declared are gone out of scope where you create the FileOutputStream.

I would do this:

private String getFilename() {
    if (ressound == R.id.sound1) {
        return "sound1"+".ogg";
    }
    return "sound1"+".ogg";
}

And then call it from your other method:

boolean exists = (new File(path)).exists();   
if (!exists){ new File(path).mkdirs(); }   

FileOutputStream save;
try { 
    save = new FileOutputStream(path+getFilename()); 

As I've said in the comment above I don't know why you assign the same value to filename in both cases, as in getFilename() always returns "sound1.ogg". Maybe that was your bug but I left it as you have it.

1 Comment

also, thanks for the alternative. i just copy pasted the lines from trying to run the if statements. so the variables were the same. obv, they are suppose to be different. thanks a lot

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.