java


My if-else statement only works once


First question here, so please do not be rude...
Anyway, I recently discovered a problem with if/else if. Showing the code written down below. The problem is that my else if parentesis only works once. When i have written a word matching the one from the register the else if won't work anymore. Is it any errors in the code or is it netbeans that is bugging me out? Would appreciate any help I can get.
Hope you can understand the syntax even though it is written on swedish...
private void årsök_txfActionPerformed(java.awt.event.ActionEvent evt) {
try {
int söktår = Integer.valueOf(årsök_txf.getText());
info_txa.setText("");
for (Register register1 : register) {
int finns = register1.getAr();
if (finns==söktår) {
info_txa.append(register1 + "");
hittad = true;
} else if (hittad==false) {
info_txa.setText("Tyvärr så utgavs det inga filmer "+söktår);
}
}
} catch (NumberFormatException e) {
JOptionPane.showMessageDialog(null, "Vänligen mata in ett tal som sökt utgivningsår!");
}
}
The question is not very clear. But this is definitely a coding error and nothing to do with Netbeans. This is what I understood, please confirm if this is what you mean:
Once Register contains a word that matches söktår, after that else-if block is not executed.
The reason for the else if to not get executed is that it only gets executed if hittad is false, and once you find the match, you set hittad to be true so it will never enter the else-if block after that.
The problem is in your if block ,You have assign 'hittad = true' ,but have not reset it again ,since your else if have condition as 'hittad==false' which will never true again ,so else if block will never execute after execution of if block code.
Hope It will help for you.
If I understand from your comments what you are trying to accomplish, then perhaps the loop could be greatly simplified as so (only showing general example structure):
Boolean found = false;
for(obj : objects)
if (obj.equals(searchCriteria)) {
found = true;
break;
}
if (!found)
system.out.println("object not found.");

Related Links

Load java.util.logging.config.file for default initialization
compare hexadecimal values java
How can I separate tokens in Java when there are some null tokens
Refactor for loops into function
Overloaded constructor ambiguity
How do I register a service in OSGi (Apache Felix)?
Getting frames from Video Image in Android
Curl syntax simulation in Java
JAXB Types problem
Issue with trying to Login to a https secure using apache commons httpclient class
Best Way to Determine if *.doc File is RTF with Java or ColdFusion
How to check whether an executable JAR has finished in another JVM
Getting unique result in Hibernate
Regular expressions in JDBC
firePropertyChange on Sequence generated Id
How do I build a HTML table from Glazed List?

Categories

HOME
yii2
log4j
image
debugging
tizen
drivers
kde
angular-material
raspberry-pi
office365api
spagobi
bookshelf.js
gis
malloc
icloud
flyway4
maude-system
kentor-authservices
quickbooks
jplayer
n-gram
zebra-printers
try-catch
nodatime
conemu
jasonette
excel-2007
bootstrap-tour
url-scheme
replaceall
large-file-upload
vision
newline
gsmcomm
ioio
catel
protovis
asset-pipeline
android-nestedscrollview
jquery-bootgrid
withings
service-discovery
ansible-playbook
texmaker
turbogears
gpx
smartcontracts
elgg
magma
font-size
windows-mobile-6.5
flow-control
chord-diagram
crosswalk-runtime
specrun
parallel-data-warehouse
google-feed-api
removechild
slicknav
login-control
xml-attribute
fuzzy-search
holder.js
make-install
ptrace
asp.net5
independentsoft
photobucket
freedesktop.org
rtbkit
wordml
deis
service-accounts
sonarqube5.1.2
bridge.net
python-green
formatjs
facebook-graph-api-v2.4
gadt
javafx-webengine
codeigniter-routing
asp.net-web-api-odata
code-access-security
ceil
typeof
htmlcleaner
balanced-payments
mbr
geos
soundtouch
padarn
pygit2
wse3.0
reddot
nsdatecomponents
android-sdk-2.1
w3wp.exe
mirah
ext3
database-management
libs
xetex
anti-piracy
uiq3

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App