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

Java android ava.lang.RuntimeException: Parcelable encountered IOException writing serializable object
Convert calendar String to Calendar Object in java
Java - Indented list from a tree
Sort list my lastname
Adding a value to Combo Box from the UI?
a list of nodes that should be visit with equal chance
java.lang.Exception: java.io.IOException: Initialization of all the collectors failed. Error in last collector was :class javafx.util.Pair
Java-How to make delay/wait in the particular block
How to get book information from Google Books API [duplicate]
How to print log of tests (pass/failed) while they are running in suite?
Foreign key constraint fails on one to many
Environment properties not getting read in spring boot
Extract Substring from String java
error when merge using hibernate 3 and ojdbc 7 [duplicate]
Can I get the instance of the calling object in Java?
Spring Boot load beans from context xml in library

Categories

HOME
pdf
variables
coq
hook
zeromq
c#-4.0
fluentd
dictionary
include
syntax
android-4.4-kitkat
adb
electronics
podio
dacpac
windows-10-universal
nano-server
dax
remote-access
dtrace
clojurescript
sms-gateway
introduction
autocad-plugin
jquery-ajaxq
arabic
spark-jobserver
intel-pin
fabric8
phonegap
twilio-api
framemaker
protovis
phpfreechat
textmate
atl
az-application-insights
midl
import-from-excel
modelmapper
thin
atomicity
jlink
csound
sqlbulkcopy
memory-alignment
minimization
elements
jwplayer7
sigabrt
bluemix-app-scan
independentsoft
django-unittest
rgeo
dukescript
ios9.1
graph-drawing
sailfish-os
remobjects
mdt
cctv
typeof
sgen
contenttype
navigationservice
phpthumb
concurrent-collections
uv-mapping
ms-project-server-2010
padarn
enterprisedb
drools-planner
angularjs-controller
itmstransporter
newtonscript
tridion-worldserver
hosts-file
code-cleanup
php-parser
libc++
invite
krl
propagation
cxxtest
zend-translate
sproutcore-2
dentrix
fixed-width
multi-tier
brewmp
audio-capture
kdbg
putchar
zune
geneva-server

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