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

How to inject GWT Activity#start with a specific Widget?
How to dynamically load values into Tomcat's Context XML file
I am getting java.lang.NullPointerException
Oracle's date and time without daylight savings via JDBC
Classes / methods - removed / changed in SDK
BroadcastReceiver don't receive keypress on Camera button
Why does my JAX-WS call to Exchange Web Services work under Java 6 but fail under Java 7 with “request requires HTTP authentication: Unauthorized”?
download a file to particular location in client system
Error on starting play framework application in cloudfoundry
Setting up class for XML deserialization using Simple (XNA to Java porting)
How can I start coding with Oracle's Nashorn JS Engine and when will it replace Rhino in the OpenJDK?
How do I log from a mapper? (hadoop with commoncrawl)
Jar Maker — Which is the main class?
Disabling all Keys and Key-Combinations in JAVA
Using CertPathBuilder and PKIX with non-X509 certificates
Why is my date not being set correctly to 30 days in the future?

Categories

HOME
multithreading
osgi
variables
jdo
openmp
relayjs
jsp-tags
rubygems
ckeditor
opengl-es-2.0
datastax-java-driver
constraint-programming
qt-creator
google-cloud-spanner
apache-cayenne
leiningen
ibm-odm
flask-wtforms
sms-gateway
emulator
swingx
autocad-plugin
tar
centos6.5
tooltipster
bpel
librato
copying
wpf-controls
jmonkeyengine
wixsharp
fedex
gzipstream
grid.mvc
arena-simulation
import-from-excel
convertapi
scrollable
network-flow
statsd
recursive-datastructures
lync-client-sdk
gnome-shell-extensions
eventkit
ableton-live
gridpane
phishing
google-web-starter-kit
mathematica-frontend
xml-attribute
mongocsharpdriver
gstreamer-0.10
sigabrt
bgp
revolution-r
prerequisites
endeca-workbench
mutation-observers
feedback
ibaction
appfabric-cache
oberon
c3
winddk
mdt
reactfx
marmalade
mysqltuner
ivyde
web2py-modules
dotnetnuke-5
flash-builder4.5
ril
jplaton
pushbackinputstream
distutils
nsnetservice
funscript
runas
multipage
jquery-ui-layout
punbb
libc++
xmemcached
visitor-statistic
subviews
web-application-design
xsdobjectgen
user-friendly
compiler-specific
javap

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