Julien Powell
Julien Powell

Reputation: 176

Java - "varible name" cannot be resolved to a variable - I cannot find where my error is

I have read the other questions with the same title but none have helped with my issue and nothing online has helped either.

I am new to Java and am trying to get a basic program running but I keep getting the aforementioned error.

Code below.

package loopy;
import java.io.*;

public class loopy {
    public static void main (String[] args) {
        // TODO: Use a loop to print every upper case letter
        for (int i = 65; i < 91; i++) {
            System.out.println((char)i);
        }
        // TODO: Get input from user. Print the same input back but with cases swapped.
          BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
          try {
            String input = in.readLine();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
          try {
            in.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        toggleStringCase(input);


    }

    // TODO: Implement this function to return the opposite case of the letter given. DO NOT USE any built in functions.
    // How to handle the case where the char given is not a letter?
    private static char toggleCase(char c) {
        return c;
    }

    // TODO: Implement this function to toggle the case each char in a string. Use toggleCase() to help you.
    private static String toggleStringCase(String str) {
        return str;
    }

}

Where it says toggleStringCase(input); is where I am getting the error trying to pass the variable to a function.

Nothing i have read suggests what I might be doing wrong.

I am sure it must be a basic error but could someone please point me in the right direction.

Have I missed some syntax somewhere?

Upvotes: 1

Views: 93

Answers (4)

Mark Melgo
Mark Melgo

Reputation: 1478

variable input can't be solved to a variable since you don't have an input variable in the scope of your main method (the scope where you are using the input variable as a parameter of toggleStringCase method). You only have input variable in the scope of your try which means that input variable is only accessible within the try and since you are using input variable outside the try that is why it produces the error.

There are 2 possible ways to fix this:

  • To fix this you should move your declaration of input variable in the scope of your main method. I have updated your code below:
package loopy;
import java.io.*;

public class loopy {
    public static void main (String[] args) {
        // TODO: Use a loop to print every upper case letter
        String input = ""; // DECLARE input HERE so that it can be used in the scope of your main method
        for (int i = 65; i < 91; i++) {
            System.out.println((char)i);
        }
        // TODO: Get input from user. Print the same input back but with cases swapped.
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        try {
            input = in.readLine(); // get the actual input

        // The try/catch below are commented out since you can combine it to the try/catch above
        // START
        //} catch (IOException e) {
        //    // TODO Auto-generated catch block
        //    e.printStackTrace();
        //}
        //try {
        // END

            in.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        toggleStringCase(input);


    }

    // TODO: Implement this function to return the opposite case of the letter given. DO NOT USE any built in functions.
    // How to handle the case where the char given is not a letter?
    private static char toggleCase(char c) {
        return c;
    }

    // TODO: Implement this function to toggle the case each char in a string. Use toggleCase() to help you.
    private static String toggleStringCase(String str) {
        return str;
    }

}
  • Or you can move the function call for toggleStringCase inside your try-catch. Refer to the code below.
package loopy;
import java.io.*;

public class loopy {
    public static void main (String[] args) {
        // TODO: Use a loop to print every upper case letter
        for (int i = 65; i < 91; i++) {
            System.out.println((char)i);
        }
        // TODO: Get input from user. Print the same input back but with cases swapped.
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        try {
            String input = in.readLine(); // get the actual input
            toggleStringCase(input); // MOVE IT HERE
        // The try/catch below are commented out since you can combine it to the try/catch above
        // START
        //} catch (IOException e) {
        //    // TODO Auto-generated catch block
        //    e.printStackTrace();
        //}
        //try {
        // END

            in.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        // toggleStringCase(input); // was moved inside try-catch


    }

    // TODO: Implement this function to return the opposite case of the letter given. DO NOT USE any built in functions.
    // How to handle the case where the char given is not a letter?
    private static char toggleCase(char c) {
        return c;
    }

    // TODO: Implement this function to toggle the case each char in a string. Use toggleCase() to help you.
    private static String toggleStringCase(String str) {
        return str;
    }

}

Upvotes: 2

Khairul Bashar Lemon
Khairul Bashar Lemon

Reputation: 669

You have to move your variable [input] to your scope. cause you declare it inside a try block, but want to use out of the scope.

public class loopy {
public static void main (String[] args) {
    // TODO: Use a loop to print every upper case letter
    for (int i = 65; i < 91; i++) {
        System.out.println((char)i);
    }
    String input=null;
    // TODO: Get input from user. Print the same input back but with cases swapped.
    BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    try {
        input = in.readLine();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    try {
        in.close();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    toggleStringCase(input);


}

// TODO: Implement this function to return the opposite case of the letter given. DO NOT USE any built in functions.
// How to handle the case where the char given is not a letter?
private static char toggleCase(char c) {
    return c;
}

// TODO: Implement this function to toggle the case each char in a string. Use toggleCase() to help you.
private static String toggleStringCase(String str) {
    return str;
}

}

Upvotes: 2

NiksVij
NiksVij

Reputation: 193

classic eg of scope problem. The var input is only accessible inside the try block or what is under the braces {} Move your toggleStringCase(input); in the try block of input itself

public class loopy {
    public static void main (String[] args) {
        // TODO: Use a loop to print every upper case letter
        for (int i = 65; i < 91; i++) {
            System.out.println((char)i);
        }
        // TODO: Get input from user. Print the same input back but with cases swapped.
          BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
          try {
            String input = in.readLine();
            toggleStringCase(input); // moved

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
          try {
            in.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

Or you can declare String input outside try block with some default/init value like

String input = "default value";// moved
try {
    input = in.readLine();
    toggleStringCase(input); 
}

Upvotes: 1

Elliott Frisch
Elliott Frisch

Reputation: 201467

input only has scope in the try block, move the call there. Also, I would prefer a try-with-resources over explicitly closing in with another try block. But, it should be noted that closing in also closes System.in (which is a global variable) and great care should be taken in doing so (since any future attempts to read from System.in will fail)

try (BufferedReader in = new BufferedReader(new InputStreamReader(System.in))) {
    String input = in.readLine();
    toggleStringCase(input);
} catch (IOException e) {
    e.printStackTrace();
}

Upvotes: 4

Related Questions