• Post Reply Bookmark Topic Watch Topic
  • New Topic

having trouble with some inputs and if/else statements  RSS feed

 
Matthew Joseph
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am having an issue with my if statements. basically, i am trying to cycle thru class names. 1 means yes, 2 means no. when i input 1, it gives responses for both 1 and 2. when i input 2, it gives the correct output. i have been staring at it for hours with no success. any assistance would be greatly appreciated! below is the code. i tried to only to add the pertitnent part.

 
sai rama krishna
Ranch Hand
Posts: 536
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How are you inputting?
 
Campbell Ritchie
Marshal
Posts: 56576
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

I have had to shorten most of your lines because long lines can be difficult to read.
Go through your ifs with a pencil. Follow the path of execution if you enter 1, 2, or 3. Particularly if you enter 1 and then go into the 2nd if (line 21→else on line 26).

Actually I think you would do well to use a switch for that sort of code.
 
Campbell Ritchie
Marshal
Posts: 56576
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sai rama krishna wrote:How are you inputting?
Ordinary keyboard input with a Scanner.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would suggest a couple things:

1) give us EXACTLY what it prints for each case.
2) Try printing out the value of answer to be sure it is what you really think it is. I will usually put something before and after, so I can see any white space characters - although if answer is an int it may not matter.

3) When you enter '1', are you SURE it prints the responses for 1 and 2? I would guess it is printing the response for 1 and the response in the else clause of your second if. (this is the big hint on what's wrong...)
 
Campbell Ritchie
Marshal
Posts: 56576
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also what does counter mean? What happens to its value if finished[counter] is not 0?
 
Matthew Joseph
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i will work on line length campbell. i will definitely look into using a switch.
also counter is just a variable to count in a while statement.
finished array is all set to 0. first time through, if you say you took the class, it goes to 1.
you can run it multiple times, skipping previous "yes" answers.
your right fred. output is:
counted
else not counted
im only on a break at work, so i cant delve into the code at the moment. ill check the second if after work
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so...it is printing out the line in he body of the if on line 15, and then the else from line 26...

for an if-else statement, you will ONLY ever go into one or the other...so..why would we be going into the else? because...it is NOT part of the if on line 15.

Here is that code boiled down to the most basic:


Does that help you see the problem?
 
Matthew Joseph
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks guys! fred, i changed the second if to an else if statement, and it works perfect. ive been staring at this code for days. i knew it was something stupid i was missing
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!