I'm trying to debug a portion of code for an assignment (I'm still very new to Java) and have combed through many resources to solve this conflict but still can't quite work it out.
public static void main(String [] args){
Scanner keyboard = new Scanner(System.in);
String input = null;
do
{
System.out.println("Enter 'A' for option A or 'B' for option B.");
String input = keyboard.next();
input.toLowerCase();
input.charAt(0);
}
while ((input != "a") || (input != "b"));
}
I always get a Duplicate Local Variable error with the input String.
Any help would be greatly appreciated!
The problem is you are declaring input inside
do{}
again. So it should just beinput=keyboard.next();
You have declared the
input
variable twice . Inside themain
method declareinput
only once. Use the following code insidedo {}
:replace
with
If you put a
String
before the variable name it is a declaration. And you can declare a variable name only once in a scope.As far as I know, this is not how you hide the variables.
Here is what I mean
You have declared the
input
variable twice. You will need to change this line:to this:
Also, this code will most likely not work:
In Java, Strings are compared using the
.equals()
method, so this line:needs to be changed to this:
Yoy've duplicated
String input
declaration. Once is enough.