AppletTalk.com Forum Index AppletTalk.com
Java discussions newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Problem with if-else statement in for loop

 
Post new topic   Reply to topic    AppletTalk.com Forum Index -> Java Help
View previous topic :: View next topic  
Author Message
jcastile@blueyonder.co.uk
Guest





PostPosted: Tue Dec 21, 2004 1:22 pm    Post subject: Problem with if-else statement in for loop Reply with quote



I am having problems with this section of code...

public void updateRank(){
int x = -1;
int y = -1;
String winnerMemNum = JOptionPane.showInputDialog
("Enter winner's membership number:");

int wMemNum = Integer.parseInt(winnerMemNum);

for(int i=0;i Player temp = (Player)list.get(i);
if(temp.getMemberNumber() == wMemNum)
x = i;
else
System.out.println("Player not found");
break;
}

I want the condition in the if statement to be tested for every element
in the list before the code in the else block executes, but at the
moment it is only testing the first element. I know I need to put
brackets in, but when I did that, the whole thing stopped working! Any
ideas?

Back to top
Heiner Kücker
Guest





PostPosted: Tue Dec 21, 2004 1:58 pm    Post subject: Re: Problem with if-else statement in for loop Reply with quote



[email]jcastile (AT) blueyonder (DOT) co.uk[/email]
Quote:
I am having problems with this section of code...

public void updateRank(){
int x = -1;
int y = -1;
String winnerMemNum = JOptionPane.showInputDialog
("Enter winner's membership number:");

int wMemNum = Integer.parseInt(winnerMemNum);

for(int i=0;i Player temp = (Player)list.get(i);
if(temp.getMemberNumber() == wMemNum)
x = i;
else
System.out.println("Player not found");
break;
}

I want the condition in the if statement to be tested for every element
in the list before the code in the else block executes, but at the
moment it is only testing the first element. I know I need to put
brackets in, but when I did that, the whole thing stopped working! Any
ideas?


The break statement stops the for loop.

Heiner Kuecker
Internet: http://www.heinerkuecker.de http://www.heiner-kuecker.de
JSP WorkFlow PageFlow Page Flow FlowControl Navigation: http://www.control-and-command.de
Java Expression Formula Parser: http://www.heinerkuecker.de/Expression.html
CnC Template Technology http://www.heinerkuecker.de/Expression.html#templ



Back to top
Paul O'Donnell
Guest





PostPosted: Tue Dec 21, 2004 11:07 pm    Post subject: Re: Problem with if-else statement in for loop Reply with quote



[email]jcastile (AT) blueyonder (DOT) co.uk[/email] wrote:
Quote:
I am having problems with this section of code...

public void updateRank(){
int x = -1;
int y = -1;
String winnerMemNum = JOptionPane.showInputDialog
("Enter winner's membership number:");

int wMemNum = Integer.parseInt(winnerMemNum);

for(int i=0;i Player temp = (Player)list.get(i);
if(temp.getMemberNumber() == wMemNum)
x = i;
else
System.out.println("Player not found");
break;
}

I want the condition in the if statement to be tested for every element
in the list before the code in the else block executes, but at the
moment it is only testing the first element. I know I need to put
brackets in, but when I did that, the whole thing stopped working! Any
ideas?


Hi,

I don't know if you indent you code and use lots of white space (maybe
the newsreader took it out) but I thing it would serve you well to start
doing so. I am not just being anal retentive - indentation and
whitespace will help you see your logic and find our errors more
quickly. Here is a pretty version of your code. Look closely at the else
clause - I suspect that the break; statement should be enclosed in a
block with the "Player not found" print statemennt. It should work if
you enclose both of these statements in braces - they are a block of
code to be executed only when the else clause is executed. Otherwise the
break statement gets executed after the if clause. The proper
indentation should help you see this more clearly.

public void updateRank()
{
int x = -1;
int y = -1;
String winnerMemNum = JOptionPane.showInputDialog("Enter winner's
membership number:");

int wMemNum = Integer.parseInt(winnerMemNum);

for(int i=0;i {
Player temp = (Player)list.get(i);
if(temp.getMemberNumber() == wMemNum)
x = i;
else
System.out.println("Player not found");
break;
}


Paul


Back to top
Paul O'Donnell
Guest





PostPosted: Tue Dec 21, 2004 11:11 pm    Post subject: Re: Problem with if-else statement in for loop Reply with quote

Paul O'Donnell wrote:
Quote:
jcastile (AT) blueyonder (DOT) co.uk wrote:

I am having problems with this section of code...

public void updateRank(){
int x = -1;
int y = -1;
String winnerMemNum = JOptionPane.showInputDialog
("Enter winner's membership number:");

int wMemNum = Integer.parseInt(winnerMemNum);

for(int i=0;i Player temp = (Player)list.get(i);
if(temp.getMemberNumber() == wMemNum)
x = i;
else
System.out.println("Player not found");
break;
}

I want the condition in the if statement to be tested for every element
in the list before the code in the else block executes, but at the
moment it is only testing the first element. I know I need to put
brackets in, but when I did that, the whole thing stopped working! Any
ideas?


Hi,

I don't know if you indent you code and use lots of white space (maybe
the newsreader took it out) but I thing it would serve you well to start
doing so. I am not just being anal retentive - indentation and
whitespace will help you see your logic and find our errors more
quickly. Here is a pretty version of your code. Look closely at the else
clause - I suspect that the break; statement should be enclosed in a
block with the "Player not found" print statemennt. It should work if
you enclose both of these statements in braces - they are a block of
code to be executed only when the else clause is executed. Otherwise the
break statement gets executed after the if clause. The proper
indentation should help you see this more clearly.

public void updateRank()
{
int x = -1;
int y = -1;
String winnerMemNum = JOptionPane.showInputDialog("Enter winner's
membership number:");

int wMemNum = Integer.parseInt(winnerMemNum);

for(int i=0;i {
Player temp = (Player)list.get(i);
if(temp.getMemberNumber() == wMemNum)
x = i;
else
System.out.println("Player not found");
break;
}


Paul


Oops, the for loop also needs a closing brace, the closing brace that
you have closes the UpdateRank method. See how much easier it is to spot
these things when you when you write pretty code?

Paul


Back to top
Ryan Stewart
Guest





PostPosted: Wed Dec 22, 2004 12:43 pm    Post subject: Re: Problem with if-else statement in for loop Reply with quote

"Paul O'Donnell" <odonnellp (AT) rogers (DOT) com> wrote

[...]
Quote:
The proper indentation should help you see this more clearly.

public void updateRank()
{
int x = -1;
int y = -1;
String winnerMemNum = JOptionPane.showInputDialog("Enter winner's
membership number:");

int wMemNum = Integer.parseInt(winnerMemNum);

for(int i=0;i {
Player temp = (Player)list.get(i);
if(temp.getMemberNumber() == wMemNum)
x = i;
else
[...]

Ironically, your code appears unindented in my news reader. Try using spaces
instead of tabs (if you didn't) in accordance with standards. Also wrap your
lines at 80 according to standards so they appear correctly. And as long as
we're talking standards, an opening brace goes at the end of the line after
one white space, and every logical block gets braces, even if it's one line
after an if or else. I'll leave it there atm. Other things are minor in
comparison.



Back to top
Andrew Thompson
Guest





PostPosted: Wed Dec 22, 2004 1:12 pm    Post subject: Re: Problem with if-else statement in for loop Reply with quote

On Wed, 22 Dec 2004 06:43:29 -0600, Ryan Stewart wrote:

Quote:
Ironically, your code appears unindented in my news reader. Try using spaces
instead of tabs ..

Got it in one. Tabs are certainly visible in my news client,
since they default to 8 spaces.

I use TextPad and it indents with tabs, which I prefer, but
generally I remember to do a search/replace of all tabs with
' '* before posting to usenet.

* (or sometimes three spaces ' ', ..if I'm feeling gregarious)

--
Andrew Thompson
http://www.PhySci.org/codes/ Web & IT Help
http://www.PhySci.org/ Open-source software suite
http://www.1point1C.org/ Science & Technology
http://www.LensEscapes.com/ Images that escape the mundane

Back to top
Ryan Stewart
Guest





PostPosted: Wed Dec 22, 2004 10:41 pm    Post subject: Re: Problem with if-else statement in for loop Reply with quote

"Andrew Thompson" <SeeMySites (AT) www (DOT) invalid> wrote

Quote:
I use TextPad and it indents with tabs, which I prefer, but
generally I remember to do a search/replace of all tabs with
' '* before posting to usenet.

* (or sometimes three spaces ' ', ..if I'm feeling gregarious)

I used to use tabs, but decided to go with 4 spaces since it's in the

conventions. It's only a bother when using an editor that doesn't support
smart tabbing or backspacing.



Back to top
Lew Bloch
Guest





PostPosted: Tue Dec 28, 2004 5:27 am    Post subject: Re: Problem with if-else statement in for loop Reply with quote

Ryan Stewart wrote:
Quote:
Ironically, your code appears unindented in my news reader. Try using spaces
instead of tabs (if you didn't) in accordance with standards. Also wrap your
lines at 80 according to standards so they appear correctly. And as long as
we're talking standards, an opening brace goes at the end of the line after
one white space, and every logical block gets braces, even if it's one line
after an if or else. I'll leave it there atm. Other things are minor in
comparison.
The opening brace shouldn't go on the control statement (e.g., "if")

line; that's a suggestion Sun made that has tainted brace-placement
forever afterward.

I will always put my opening brace on its own line, and I do not ask
anyone else to follow my lead. It's so much more logical and readable
to do it correctly rather than according to Sun's conventions (not
"standards", btw!).

Back to top
Ryan Stewart
Guest





PostPosted: Tue Dec 28, 2004 5:38 am    Post subject: Re: Problem with if-else statement in for loop Reply with quote

"Lew Bloch" <justaguy (AT) nospam (DOT) net> wrote

Quote:
The opening brace shouldn't go on the control statement (e.g., "if") line;
that's a suggestion Sun made that has tainted brace-placement forever
afterward.

I will always put my opening brace on its own line, and I do not ask
anyone else to follow my lead. It's so much more logical and readable to
do it correctly rather than according to Sun's conventions (not
"standards", btw!).

Been down this road before. The Java standard is well set and accepted. Your
"logical", "readable", and "correctly" are all subjective, yet you state
them as facts. As for convention vs standard, I still don't understand why
people try to make such a big deal of that. Here are two definitions from
the American Heritage Dictionary, 4th edition:

1) A widely used and accepted device or technique
2) Something, such as a practice or a product, that is widely recognized or
employed

Which one is the definition of standard and which is the definition of
convention?

Something I heard from an Oracle developer once: Standards are like
toothbrushes. Everybody has one. You use it every day because you know it's
good for you. But you'd rather die than use mine.

As for me, I prefer to conform to a published, well-defined, and widely
accepted standard and have my code easily read by the rest of the Java world
that does the same.



Back to top
George W. Cherry
Guest





PostPosted: Tue Dec 28, 2004 5:55 pm    Post subject: Re: Problem with if-else statement in for loop Reply with quote


"Ryan Stewart" <zzanNOtozz (AT) gSPAMo (DOT) com> wrote

Quote:
"Lew Bloch" <justaguy (AT) nospam (DOT) net> wrote in message
news:Ld-dnWOk2427cE3cRVn-3A (AT) comcast (DOT) com...
The opening brace shouldn't go on the control statement (e.g., "if")
line; that's a suggestion Sun made that has tainted brace-placement
forever afterward.

I will always put my opening brace on its own line, and I do not ask
anyone else to follow my lead. It's so much more logical and readable to
do it correctly rather than according to Sun's conventions (not
"standards", btw!).

Been down this road before. The Java standard is well set and accepted.
Your "logical", "readable", and "correctly" are all subjective, yet you
state them as facts. As for convention vs standard, I still don't
understand why people try to make such a big deal of that. Here are two
definitions from the American Heritage Dictionary, 4th edition:

1) A widely used and accepted device or technique
2) Something, such as a practice or a product, that is widely recognized
or employed

Which one is the definition of standard and which is the definition of
convention?

Something I heard from an Oracle developer once: Standards are like
toothbrushes. Everybody has one. You use it every day because you know
it's good for you. But you'd rather die than use mine.

As for me, I prefer to conform to a published, well-defined, and widely
accepted standard and have my code easily read by the rest of the Java
world that does the same.

When I pick up a Java book in which I see

if (condition)
{
doThis();
doThat();
}

I put the book right back down. But when I see

if (condition) {
doThis();
doThat();
}

the book has passed my first test, and
I look at it further. Time is my only real
scarcity, and I don't have time for writers
who write code like Lew's. Among the
many things Sun got right (IMHO), one of
them is left brace placement.

George




Back to top
Andrew McDonagh
Guest





PostPosted: Tue Dec 28, 2004 8:49 pm    Post subject: Re: Problem with if-else statement in for loop Reply with quote

George W. Cherry wrote:

snipped

Quote:
When I pick up a Java book in which I see

if (condition)
{
doThis();
doThat();
}

I put the book right back down. But when I see

if (condition) {
doThis();
doThat();
}

the book has passed my first test, and
I look at it further.


Wow! You really are missing out on some good books then... do you have
a preference for the type face used and colour of the cover as well?

is it just me, or is this the silliest thing you have ever read here?

Back to top
Ishmael Rufus
Guest





PostPosted: Wed Dec 29, 2004 2:30 pm    Post subject: Re: Problem with if-else statement in for loop Reply with quote

Who in the world would argue over brace and bracket placement? Brace
and bracket placement does not effect the functionality of the code. As
for reading it, learn how to be comfortable with both methods you big
babies.

Back to top
jeffc
Guest





PostPosted: Wed Dec 29, 2004 4:03 pm    Post subject: Re: Problem with if-else statement in for loop Reply with quote


"Ishmael Rufus" <sakamura (AT) gmail (DOT) com> wrote

Quote:
Who in the world would argue over brace and bracket placement? Brace
and bracket placement does not effect the functionality of the code.

Apparently, you.



Back to top
George W. Cherry
Guest





PostPosted: Wed Dec 29, 2004 5:15 pm    Post subject: Re: Problem with if-else statement in for loop Reply with quote


"Andrew McDonagh" <news (AT) andrewcdonagh (DOT) f2s.com> wrote

Quote:
George W. Cherry wrote:

snipped

When I pick up a Java book in which I see

if (condition)
{
doThis();
doThat();
}

I put the book right back down. But when I see

if (condition) {
doThis();
doThat();
}

the book has passed my first test, and
I look at it further.

Wow! You really are missing out on some good books then... do you have a
preference for the type face used and colour of the cover as well?

Yes. Arial, 12 point. What are the good books
I'm "missing out on"? All the best books place
the '{' where Sun suggests. I like blue covers.
Since the last election I detest red.

Quote:
is it just me, or is this the silliest thing you have ever read here?

It's you, silly.



Back to top
George W. Cherry
Guest





PostPosted: Wed Dec 29, 2004 5:23 pm    Post subject: Re: Problem with if-else statement in for loop Reply with quote


"Ishmael Rufus" <sakamura (AT) gmail (DOT) com> wrote

Quote:
Who in the world would argue over brace and bracket placement? Brace
and bracket placement does not effect the functionality of the code. As
for reading it, learn how to be comfortable with both methods you big
babies.

Indention doesn't affect the "functionality of code".
Poorly chosen class, method, and variable names
don't affect the "functionality of code".
Comments don't affect the "functionality of code".
Case conventions don't affect the "functionality of code".




Back to top
Display posts from previous:   
Post new topic   Reply to topic    AppletTalk.com Forum Index -> Java Help All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.