My jsp page only executes the else statement [duplicate] - java

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 5 years ago.
I have the following login codes.
if (uname == "Abigail" && password=="Abby14"){
response.sendRedirect("http://localhost:8080/Practical_4/member.jsp");
}
else {
response.sendRedirect("http://localhost:8080/Practical_4/index.html");
}
I realized that my jsp page treats the if-statement as if it's an else statement, and only executes the else-statement.

What you do is comparing addresses where strings are stored and not the strings them selfs in some case java will store same string in same address but you cannot count on that.Here is a code example that should explain the issue
public static void main(String... args) {
String a = "a";
String b = new String("a");
String c = "a";
System.out.println(a==b); // false
System.out.println(a==c); //true
System.out.println(a.equals(b)); // true
}
So the buttom line always use equals insterad of ==

Use equals for string comparison.
if (uname.equals("Abigail") && password.equals("Abby14")){
response.sendRedirect("http://localhost:8080/Practical_4/member.jsp");
}
else {
response.sendRedirect("http://localhost:8080/Practical_4/index.html");
}
Hope this helps.

change to use equals and change to order to prevent null pointer
if ("Abigail".equals(uname) && "Abby14".equals(password)) {

Related

Exit array for loop Java [duplicate]

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 4 years ago.
Consider my code below:
System.out.println("Insert your inventory");
for (int i = 0; i<20;i++) {
System.out.print(i+1+".");
if (inventory[i] == "N" || inventory[i]=="n") {
break;
}
inventory[i] = s.nextLine();
}
How can I exit from this loop if the user enters 'N' or 'n'?
You're comparing string with == operator. It does not yield correct result because your constant string "N" and your input "N" do not have same reference/pointer.
You have to use equals() to guarantee the correct compare result between strings.
Replace
if (inventory[i] == "N" || inventory[i]=="n")
With
if (inventory[i].equals("N") || inventory[i].equals("n"))
You should compare your String variables with the .equals() method instead of the == operator.
An explanation about why this is important can be found here on StackOverflow.

If Else Statement Not Returning Right Value [duplicate]

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 4 years ago.
I am trying to check if a playlist is simple or master by an index value. My problem is when I put the (True) URL it still returns a false statement "This is a simple Playlist".
Any tips on how I can fix this ?
String output = getPlaylistUrl(input);
String mediaRecord = output.substring(399);
String lastRecord = "gear4/prog_index.m3u8";
if (mediaRecord == lastRecord) {
System.out.println("This is a master playlist");
} else {
System.out.println("This is a simple playlist");
}
In Java, strings can not be compared for equality using ==, because == compares two instances, not the content. So unless s1 and s2 are actually the same instance, s1 == s2 will never return true.
You need to use equals(...) to compare two strings for equality.
if (mediaRecord.equals(lastRecord) { ... }
In order to compare Strings you need to use .equals and not ==. Using == compares references and not values

Program not noticing when the string is equal to a specific string [duplicate]

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 6 years ago.
I've been trying to create a program that censors a word but I was having difficulty with that so I tried going back to some of the fundamental code and testing it and I am coming across an odd result.
import java.util.Scanner;
public class TextCensor
{
public static void main(String[] args)
{
String input;
Scanner keyboard = new Scanner(System.in);
input = keyboard.nextLine();
int length = input.length() - 1;
if (length + 1 >= 3)
{
for (int i=0; i<(length - 1); i=i+1 )
{
char first = input.charAt(i);
char second = input.charAt(i+1);
char third = input.charAt(i+2);
String censorCheck = "" + first + second + third;
if (censorCheck == "tag")
{
System.out.println("success");
}
else
{
System.out.println(censorCheck);
}
}
}
else
{
System.out.println(input);
}
}
}
If I input the string "adtag" I will obtain the following output:
adt
dta
tag
yet "success" will never be printed despite the fact that I have printed a censorCheck that is equal to "tag".
String is an object. You have to compare objects by equals():
censorCheck.equalsIgnoreCase("tag")
Ignore case works fir upper letters as well.
Only for primitives you can use comparison by ==:
3 == 3
You are trying to check whether both instance of String is same or not instead of checking contents of both string.
You should try censorCheck.equals("tag") .
To compare whether contents of two string are equal or not in JAVA you should use the equals() method. You cannot compare the value of two string by the == operator . In your case use if (censorCheck.equals("tag")) and see if you get the desired result.

Checking if two strings? are identical and returning true if they are [duplicate]

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 6 years ago.
I have a question regarding comparing two strings? and checking if they are identical. Below is the question explenation
// returns true if f.makeMolecular() is is equal to this.makeMolecular
// e.g. terms = {Term('B',3),Term('A',6)} and f = {Term('B',1),Term('A',3),Term('B',1),Term('A',3)}
// would return true
// The makeMolecular() method sorts the Term Strings into Alphabetical order with the number linked to the letter added and following behind the letter.
E.G Both of the makeMolecular should return a string of "A6B3"
I want to check if they are identical using and IF statement and taking advatange of the == method in Java.
Below folows one solution to checking if the strings are identical, but as mentioned i would like to do it with an iff statement.
{
this.makeMolecular();
f.makeMolecular();
return identical(f);
}
This is what i have tried so far:
char b = f.makeMolecular();
char b = f.makeMolecular();
if (a==b){
return true;
}
else{
return false;
}
and i get the following error: Incompatible types: void cannot be converted to char. I am quite certain the use of "char" is incorrect here, but i am not sure of what to use instead.
So I am wondering how to convert these strings(?) to a constant and then checking if they are identical via an IF-statement.
// Edit:
By request; this is the method for makeMolecular()
public void makeMolecular()
{
char element = ' ';
int atoms = 0;
ArrayList<Term> mol = new ArrayList<Term>();
while (terms.size() > 0){
if (mol.size() > 0 && mol.get(mol.size() - 1).getElement() == nextElement().getElement() ){
atoms = mol.get(mol.size() - 1).getAtoms() + nextElement().getAtoms();
element = nextElement().getElement();
mol.remove(mol.size() - 1);
mol.add(new Term(element,atoms));
}
else {
mol.add(nextElement());
}
terms.remove(nextElement());
}
terms = mol;
}
It should be
String a = f.makeMolecular();
String b = f.makeMolecular();
if(a.equals(b))
return true;
else
return false;

JAVA - Why a == "1" returns false [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
String is not equal to string?
What makes reference comparison (==) work for some strings in Java?
can some one explain me following java code
String a = "1";
if(a == "1") {
//print compare 1 is true;
} else {
//print compare 1 is false;
}
if(a.equals("1")) {
//print compare 2 is true;
} else {
//print compare 2 is false;
}
it results like
compare 1 is false
compare 2 is true
Only explanation i have is that its comparing the memory address not the values itself. But i am not sure. can some please put a light on it. in .Net == operator is overloaded to compare contents of string.
use "1".equals(a) , String is an object so use equals() to compare
I understood that == operator is compare "Is it same object?"
object a is not same object with constant string "1".
so returns false

Categories

Resources