• Post Reply Bookmark Topic Watch Topic
  • New Topic

switch inside a do while loop?  RSS feed

 
Tomas Hughes
Greenhorn
Posts: 6
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey guys! First of I'm brand new to java and the forums! I've only been learning for a about a week and so far its been pretty smooth, up until now :/ I always try and figure the problem out my self before consulting Google, which led me here, but this time I'm stumped.

Basically the problem is I've been trying to loop my switch (if this is even possible) until the user enters a correct number between 1 and 4. But for some reason when the user enters a correct number (1,2,3,4) the loop runs again. Even when the user enters a wrong number the switch ignores the default and repeats what is in the do. Can someone please explain why it wont work or my mistake. Thanks

Am I just trying to loop something that cannot be looped or have I gone about it the wrong way?

 
Knute Snortum
Sheriff
Posts: 4276
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch. Thank for using code tags.

I think the problem is line 79. Where does count get modified?
 
Tomas Hughes
Greenhorn
Posts: 6
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:Welcome to the Ranch. Thank for using code tags.

I think the problem is line 79. Where does count get modified?


I created the variable count on line 32 giving it the value true. When a user enters 1 - 4 I'm expecting the relevant case to run the block of code and change the variable count to false which in turn should end the loop right?

If the user doesn't enter a number between 1 -4, 5 for example, the default will run and the loop will replay or am I wrong?
 
Knute Snortum
Sheriff
Posts: 4276
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You'll never get out of the do/while loop because count doesn't change inside the loop.
 
Tomas Hughes
Greenhorn
Posts: 6
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So changing count inside the case won't affect the count inside the loops condition?
 
Joe Harry
Ranch Hand
Posts: 10128
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Inside your do, you have to check what the use has entered. If it is anything between 1 and 4 inclusive, you can set the count to false! A small advice for you is to consider classes as Nouns which means they start with a capital letter.
 
Knute Snortum
Sheriff
Posts: 4276
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It looks like your while(count); statement should be below 110, that is, your switch changes count so the while should be below it.

One other thing: it's good to close resources, but System.in is an exception. If you close it, you will never be able to open it again in that program.
 
Tomas Hughes
Greenhorn
Posts: 6
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah I see my mistake. I made the loop without including the switch, I have to move the while(count) down to 110 to include the switch.

Well, I feel dumb now hahaha >.<
 
Knute Snortum
Sheriff
Posts: 4276
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't feel dumb. We all make mistakes in programming and we learn from them.
 
Junilu Lacar
Sheriff
Posts: 11486
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'd like to offer a few nitpicks:

1. The class name mammals
1a. Should be capitalized
1b. Should be singular since each instance will represent one type of mammal

Prefer: class Mammal instead

2. The method name calculateHowManyYearsLeft
2a. While it's pretty descriptive, a more succinct name could be given
2b. Prefer not to use "calculate" in the name - it implies an implementation detail; you want to hide implementation details and keep things abstract

Prefer something like getYearsLeft() instead

3. Your variable names count and animal
3a. Don't make sense in the context that they're used
3b. Read it out loud. It should be almost like normal conversation
3c. If someone asked you "What's the count?" and you answered "true" or "false", do you think that makes sense?
3d. If someone asked you "What's the animal?" and you answered "1", do you think that makes sense?

Prefer something like option or choice instead of animal
Prefer something like continue keepGoing or askAgain instead of count
(Edit: can't use "continue" because that's a reserved word)

4. Your switch statement has repetitive code. Programming principle to live by: DRY (Don't Repeat Yourself)
4a. Generalize - you defined capabilities of a Mammal as a generalization, use it.
4b. Each instance of Mammal will have similar behaviors as other Mammals
4c. This is kind of a poor example for the use of switch but we'll go with it for now.

Putting all that together, the code would be something like this instead:

 
Knute Snortum
Sheriff
Posts: 4276
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nitpick in your nitpick:

Prefer something like continue or askAgain instead of count


You can't use continue as an identifier. It's a reserved word.

Edit: Ha! you caught it.
 
Junilu Lacar
Sheriff
Posts: 11486
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:Edit: Ha! you caught it.

Yeah, but thanks for pointing it out. At least I know there are people actually reading my long-winded posts
 
Tomas Hughes
Greenhorn
Posts: 6
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This "long-winded post" has helped me out so much though! I really appreciate the fact that you would take time to help a newbie out

I just have one two questions, and it's probably really stupid. But on line 04. What is this doing? Is it creating a variable called animal of type Mammal? What is the "null", I don't think I have seen this term yet.

Again thank you so much for your input guys, it has been so helpful!

Edit: Also on line 13 how come we are doing askAgain = true; Shouldn't it be true regardless of default executing? Why do we have to confirm it is true even though it wouldn't of been changed?
 
Knute Snortum
Sheriff
Posts: 4276
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is it creating a variable called animal of type Mammal?

You got it, basically. Technically, it's declaring a variable of type Mammal and setting that variable to null (nothing). null is kind of like setting a counter to zero, but for an object reference.
 
Knute Snortum
Sheriff
Posts: 4276
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
on line 13 how come we are doing askAgain = true; Shouldn't it be true regardless of default executing? Why do we have to confirm it is true even though it wouldn't of been changed?


I would do the same thing. I would call this defensive programming. You don't have to set askAgain to true, but it shows your intent and protects you if someone else modifies your program to set askAgain to false.
 
Junilu Lacar
Sheriff
Posts: 11486
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tomas Hughes wrote:on line 04. What is this doing? Is it creating a variable called animal of type Mammal? What is the "null", I don't think I have seen this term yet.

null is a special value in Java that is used to represent the concept of "nothing". In this case, we're saying "at this point, the variable animal doesn't reference any particular instance of Mammal".

null can be assigned to any object reference.

Also on line 13 how come we are doing askAgain = true; Shouldn't it be true regardless of default executing? Why do we have to confirm it is true even though it wouldn't of been changed?

You don't. It's good that you asked that because in a way, putting it where I did also violates DRY. I just included it there to make sure you understood the context where it was used and where you might change its value. If you can find a better place for it, you've made progress!

Again thank you so much for your input guys, it has been so helpful!

You're welcome.
 
Junilu Lacar
Sheriff
Posts: 11486
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In fact, you might even say that we could make further improvements to the code I gave.

Another thing you want in your code is consistency and singularity of purpose. You can divide a big program into several smaller chunks of tasks. When you learn about methods, you should keep this in mind. For now, you can chunk your code into "paragraphs", where whitespace helps delineate one paragraph from the next. Each paragraph should have a consistent, singular thought.

We can make the code I suggested before better by rearranging it a little bit.

Now each "paragraph" has a singular purpose, the first to decide which animal to use, the second to display something. Now the code is more symmetrical.

I use the term "paragraph" because I always teach that your program should read like a story that explains to the reader what's happening. The more your program reads like a story, the easier it is for other people to understand it.
 
J. Kevin Robbins
Bartender
Posts: 1801
28
Chrome Eclipse IDE Firefox Browser jQuery Linux MySQL Database Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As long as we're nitpicking, and please understand that this is done in the spirit of teaching, not criticizing, the Mammal class could use some changes. The variables (age, color, etc) should be declared as private and accessed through public getters and setters. They look like so:

This is how you define the api for the class. Now you have to think about this; are getters and setters needed for every variable? For instance, is the Mammal going to change color after it's instantiated? Probably not, so one could argue that a setter isn't needed for color and that particular characteristic should be set in the constructor. (I'm doing my best to think of a mammal that might change color over it's lifetime and I'm coming up empty, but I could be wrong). Now if you create a Reptile class, well, a chameleon could change color so that class is going to need a setter.

We'll get into passing parameters to constructors and having multiple constructors at a later time. I think we've hit you with enough for today. It's Friday after all. I know I'm ready to wrap up the week and go get an adult beverage.

And btw, welcome to the Ranch!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!