Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

newbie question  RSS feed

 
barry richard
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
I am just learning java. This is a program that i typed (from the text book). According to the textbook it should run normally but it does not


This produces an output


What am I doing wrong?
Thanks for your time
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One thing that jumps out at me is that you have no breaks in your cases, so it will fall through, execute all the cases starting with the first match, and finally execute the default case, no matter what you enter.
 
Kemal Sokolovic
Bartender
Posts: 825
5
Java Python Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are missing break statement in each case of your switch. Check the tutorial:
The break statements are necessary because without them, statements in switch blocks fall through: All statements after the matching case label are executed in sequence, regardless of the expression of subsequent case labels, until a break statement is encountered.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12542
48
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I believe they don't WANT breaks in each case. If the person chooses option 'e', then they get every option - the new car scent, tire treatment, etc.

However, there may be ONE missing break statement...

Another thing you are doing wrong is that your class name isn't capitalized. Conventions say this class should be named "Practice", not "practice".
 
barry richard
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes fred you are right. I do not want breaks, I want all options below the selection to execute

I did both
1. added a break below default:
2. changed the class name starting with capital letter i.e practice to Practice

It still giving me the same result

Thanks for your time
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
barry richard wrote:Yes fred you are right. I do not want breaks, I want all options below the selection to execute

I did both
1. added a break below default:


You need to add one before the default case to prevent it from falling into that case.
 
Kemal Sokolovic
Bartender
Posts: 825
5
Java Python Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Then break should be in last case before default only, right?
 
Henry Wong
author
Sheriff
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
barry richard wrote:Yes fred you are right. I do not want breaks, I want all options below the selection to execute

I did both
1. added a break below default:
2. changed the class name starting with capital letter i.e practice to Practice

It still giving me the same result

Thanks for your time



Debugging would be good here... for example, you can print out the character you are switching on, to see just what the program actually sees during the test run.

Henry
 
barry richard
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes I will post the corrected code


Still same result. Did you mean after default. Oops, I was missing a break before and after default

That fixes it

thanks a lot
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It should be:



If it's E, it will execute E, D, C, B, A, but not default. If it's B, it will execute B, A, but not default. And so on. If it's none of those letters, it will execute default.

I believe you're allowed to put a break at the end of the default case as well (though it may generate a compiler warning), but it's not necessary. What matters is that you have one right before the default case.
 
barry richard
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Jeff
 
fred rosenberger
lowercase baba
Bartender
Posts: 12542
48
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so...do you understand why Jeff's solution is correct? Do you understand what the break statement does, and why it needs to go where he put it?
 
barry richard
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes I do.

thank you
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!