java


Java executuion of else is expected but always if is executing [duplicate]


This question already has an answer here:
How do I compare strings in Java?
23 answers
I think you have understood what I am trying to do. Give a prompt when user press OK without changing he default TEXT. But the first condition seems to be TRUE. I don't know if it is a problem of if statement or my code.I have included the whole class .Be sure to read comment to avoid reading unnessasary code.
ok = JButton
text = JTextField
int c = 0;
public class handler implements ActionListener{
public void actionPerformed(ActionEvent e){
//
//The following lines are not nessary for the question.
if(ok==e.getSource()){
if(!(male.isSelected() || female.isSelected()) && c==0){
c++;
JOptionPane.showMessageDialog(null,"Hey you haven't selected your gender. Do you wish to proceed","Warning",JOptionPane.PLAIN_MESSAGE);
}
}
//well these lines were not necessary
//They were just in the same class
//
//
//Here the else condition should execute
//
if(ok==e.getSource()) && (text.getText() != "Enter your name")){
JOptionPane.showMessageDialog(null,"Your name is "+text.getText(),"Name",JOptionPane.PLAIN_MESSAGE);
}else if((ok==e.getSource()) && (text.getText() == "Enter your name")){
JOptionPane.showMessageDialog(null,"Hey type in your name buddy ","Name",JOptionPane.PLAIN_MESSAGE);
}
}
}
So the problem is that the if condition gets executed, while the else if should when we are not changing the default text "Enter your name". I tried changing the text and in that condition also the if block is executing. And please do a full explanation of your answer. I am a beginner with rusted skills.
You need use other method to compare the strings, for example here:
text.getText() == "Enter your name")
you can use equals:
text.getText().equals("Enter your name")

Related Links

How can I make this if-else statement into a switch statement?
unexpedtedly close application
How to throw exception from spring batch processor process() method to Spring batch job started method?
refactoring input check for hangman in java with acm
How to make a .jar out from an Android Studio project
Java 7 applet self signed certificates
switch behaviour - java
ArrayList lists null instead of instances of the object [closed]
error illegal start of expression
Cannot load library jni ffmpeg in intellij idea
Running Servlet while JSP page is being displayed
JUnit test fail
outOfMemory Exception while re-encoding image
Exception in thread main" java.util.InputMismatchException
Quickblox requestBuilder [or]
Position image in a JTextPane (like using setBounds to position JPanel in a JFrame)

Categories

HOME
ms-access
magnific-popup
sqlite-net-extensions
tizen-web-app
jxls
umd
yum
django-imagekit
awesome-wm
mapserver
finite-automata
invantive-sql
grails3
mustache.php
claims-based-identity
p-value
facet
dynamic-featured-image
poltergeist
fifo
tasklet
winrt-xaml-toolkit
xmlreader
wallpaper
wpf-controls
bower-install
plsql-psp
io-redirection
scaffold
automake
awt
dynamic-reports
botbuilder
network-flow
python-c-api
ionicons
flutterwave
ado.net-entity-data-model
fody
login-control
trash
superstack
bgp
tform
cloudbees
msys
teamcity-8.0
embedded-code
event-bubbling
cisco-ios
iiviewdeckcontroller
kineticjs
googlemock
uitouch
seaside
braille
undefined-reference
message-driven-bean
xsockets.net
ms-project-server-2010
listings
odata4j
hungarian-algorithm
random-seed
sabredav
javaspaces
stage
rfc1123
actionview
pyinotify
digest-authentication
gallio
exchange-server-2003
mtj
noscript
genealogy
ntvdm.exe

Resources

Encrypt Message