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

Regex pattern alternating and sequential mathing
Hibernate Criteria create a count query without asking it beside The query
What is the approach to create a nested list from a linear tree structure in java
Run JUnit (JAVA) tests for a whole dataset from .txt file
Take FileItem.getInputStream as FileInputSTream
Does synchronized lock a Result Set object?
change iteration variable within for loop
Java:package R does not exist [duplicate]
resize JFrame with maximize button
Exception when getting Collection<Long> values using Hibernate - ClassCastException: java.math.BigDecimal cannot be cast to java.lang.Long
get content from sqlite databes and use to log in [closed]
Efficient way of matching linear data with grouped data [closed]
Andorid Java String - Which is better? “” +, toString(), String.valueOf()?
How can I continously check to see if the text inside a text file has changed to empty? Java
How to load image in Viewholder of MovieAdapter?
How do I pass a string which has an apostrophe in it inside a variable in a function?

Categories

HOME
multithreading
coq
hook
kde
include
syntax
grep
mean-stack
cvs
cmd
tizen-web-app
leon
angular-ui-bootstrap
enterprise-library-5
portia
iggrid
session-variables
tomcat6
nstableview
apache-cayenne
windows-phone-7
event-handling
circuit
dynamics-crm-online
seaborn
graphlab
tostring
propel
database-replication
contextmenu
lucene.net
java-3d
errorlevel
kvc
selectedindexchanged
wpfdatagrid
wtx
unboundid
twilio-api
ioio
phpfreechat
y86
normal-distribution
revolution-slider
jquery-bootgrid
unspecified
ssjs
opshub
tasker
ws-security
ensembles
quadratic-programming
keydown
gpx
.net-4.6.2
qcombobox
revapi
multipeer-connectivity
jlink
mplayer
brightcove
typed-lambda-calculus
np-complete
pillow
redundancy
orthogonal
pintos
yt-project
messenger
firmata
grails-tomcat-plugin
intrusion-detection
skobbler-maps
storekit
connect-by
php-internals
asp.net5
two-factor-authentication
historian
execute
intellij-14
cyclomatic-complexity
wordml
kcachegrind
angular-leaflet-directive
sankey-diagram
sonarqube5.1.2
oxwall
mdt
google-reader
p4java
android-2.2-froyo
mcts
random-seed
pygit2
libstdc++
multipage
jspinclude
onsubmit
qtkit
semantic-diff
pydot
vdsp
tomcat-valve
f#-powerpack
google-friend-connect
blitz++
coda-slider
gamma
mirah
ncqrs
msdev
django-notification





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