Help with if statement that wont act right...
When I type in yes it come out with all the text in the yes if statement plus TEXT4(the last if statement). Can some one spot the error? I also tried if ( ! (answer.equals(All)) ) it still comes out with TEXT4....
Code :
public static void main(String args[]){
String answer1;
String yes = "yes";
String no = "no";
String[] All = {"yes", "no"};
Scanner answer = new Scanner(System.in);
System.out.println("TEXT1");
System.out.println("yes/no");
answer1 = answer.nextLine();
answer1.toLowerCase();
if(answer1.equals(yes)){
System.out.println("TEXT2");
}
if(answer1.equals(no)){
System.out.println("TEXT3");
System.out.println("yes/no");
}
if (!answer.equals(yes)&&!answer.equals(no)){
System.out.println("TEXT4");
}
}
Re: Help with if statement that wont act right...
Strings are immutable. Methods such as toLowerCase() do not actually change the String, they return a new String with the transform applied.
To prove this, print out the value of answer1 after you call the toLowerCase() method.
Either save the value returned from the toLowerCase() method, or look at the String API for a function that will help you check for equality while ignoring case.
Re: Help with if statement that wont act right...
HAHAHAHHAHA wow im dumb... I had the wrong variable in it... My bad fixed it.
Thank you kevin for that I will fix that as well.
Closed