• Post Reply Bookmark Topic Watch Topic
  • New Topic

do/while loops  RSS feed

 
Daniel Martos
Ranch Hand
Posts: 123
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In my last assignment, my program had to ask if a student is a pennsylvania resident or not (to figure out the cost of their tuition).  So I used this code:



My current assignment states: it will ask for the student’s residency.  Possible residency options are:  P for PA resident, N for a US, non-PA resident and I for international students.  I am unsure how to do this in a do/while loop, any suggestions?
 
Junilu Lacar
Sheriff
Posts: 11165
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, if the user is supposed to enter only "P" or "N", does it make sense to check for "true" or "false"?  Aside from some stylistic improvements, your code is already set up to work properly.
 
Daniel Martos
Ranch Hand
Posts: 123
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:Well, if the user is supposed to enter only "P" or "N", does it make sense to check for "true" or "false"?  Aside from some stylistic improvements, your code is already set up to work properly.


I guess I'm struggling how to add the third option.
 
Junilu Lacar
Sheriff
Posts: 11165
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
BTW, if you want to compare a literal value with something that could potentially be null, it's safer to write this:

Since a literal String is also a full-fledged String object, you can call methods on it. This technique avoids getting a NullPointerException when userInput is null and it also makes it unnecessary to check for null first. Otherwise, you have to write:
 
Junilu Lacar
Sheriff
Posts: 11165
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Daniel Martos wrote:I guess I'm struggling how to add the third option.

You can have as many terms in your boolean expression as you need; you're not limited to two.
 
Junilu Lacar
Sheriff
Posts: 11165
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, why do you need to check for multiple values?  The method is called isPAResident(). Isn't that only true when the user enters "P"?  Seems like checking for any other value is unnecessarily beating around the bush.
 
Junilu Lacar
Sheriff
Posts: 11165
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The code you have written is a good example of why violating the Single Responsibility Principle for methods is bad.  Your method does a few things: Asks user for input, validates the input, and checks if the input is a certain value.  You should at least separate the task of asking the user for input and validating it to a separate method.  That way, the isPAResident() can focus on doing ONE thing only, which is to check if the input indicates that the user is a PA resident.  In the method that's driving your program flow, you would write instead:

This way, isPAResident becomes very simple and straightforward - it can even be implemented as a single line of code.

Additionally, what will you do if you're asked to start reading data from a file or database? How are you going to use your version of isPAResident() if the data doesn't come from the user typing an answer at the console?
 
Daniel Martos
Ranch Hand
Posts: 123
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:Also, why do you need to check for multiple values?  The method is called isPAResident(). Isn't that only true when the user enters "P"?  Seems like checking for any other value is unnecessarily beating around the bush.


I need to change that method name to some more like studentResidency, because I need to check for multiple values. so:



Do I need to change how it returns?

 
Junilu Lacar
Sheriff
Posts: 11165
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do you think makes sense?  If you call a method called studentResidency() and you get back a boolean value that will be true only if the user input was "P", does that make sense?  Or should you just return the validate input that the user entered?  See my previous comment about separating concerns.  User input and validation is one concern that should be separated into a method by itself.

Put in another way, imagine you're having a conversation with a couple of people and this is how it goes:

You: What's your student residency?
Guy1: True.
You: (WTH)

You: And what about your residency?
Guy2: False.
You: (WTH)

That doesn't go very well. On the other hand, this makes more sense:

You: What's your student residency?
Guy1: P
You: Ok, so you're a PA resident.

You: And what about your residency?
Guy2: I
You: Ok, so you're an international student.

Deciding what to name a method and what it should return is easier to do if you try to make it read like a conversation that makes sense.
 
Daniel Martos
Ranch Hand
Posts: 123
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:The code you have written is a good example of why violating the Single Responsibility Principle for methods is bad.  Your method does a few things: Asks user for input, validates the input, and checks if the input is a certain value.  You should at least separate the task of asking the user for input and validating it to a separate method.  That way, the isPAResident() can focus on doing ONE thing only, which is to check if the input indicates that the user is a PA resident.  In the method that's driving your program flow, you would write instead:

This way, isPAResident becomes very simple and straightforward - it can even be implemented as a single line of code.

Additionally, what will you do if you're asked to start reading data from a file or database? How are you going to use your version of isPAResident() if the data doesn't come from the user typing an answer at the console?


This is my first programming class, so as thankful as I am for the advice, some of it is going over my head.  Are you suggesting that I should create 3 separate variables, one for PA resident, one for non-PA resident, and one for international student?  If I do, how does that affect my method?
 
fred rosenberger
lowercase baba
Bartender
Posts: 12542
48
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good programming is kind of like using legos...although you have to make your own lego blocks.

So you want a bunch of little methods that each do one little thing, then you can combine them up into larger things, and so on. 

So, as an example, you might write a method that takes a string as input, and returns true if someone is a PA resident, and false if they are not.

You might have another method that takes a string and returns true if they are an international student, and false if they are not.

Then, you could have another method that takes a string as input, and have it call those first two methods, passing in the string it got. It could have some additional logic to decide if it needs to call both methods or not. And then, it could return a value like 0, 1, or 2 depending on whether they are a PA resident, an International student, or a non-resisdent. (You will eventually learn about Enums, which would be better than 0,1,2 but i wouldn't worry about them just yet). the pseudo code would be something like




 
Daniel Martos
Ranch Hand
Posts: 123
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fred rosenberger wrote:Good programming is kind of like using legos...although you have to make your own lego blocks.

So you want a bunch of little methods that each do one little thing, then you can combine them up into larger things, and so on. 

So, as an example, you might write a method that takes a string as input, and returns true if someone is a PA resident, and false if they are not.

You might have another method that takes a string and returns true if they are an international student, and false if they are not.

Then, you could have another method that takes a string as input, and have it call those first two methods, passing in the string it got. It could have some additional logic to decide if it needs to call both methods or not. And then, it could return a value like 0, 1, or 2 depending on whether they are a PA resident, an International student, or a non-resisdent. (You will eventually learn about Enums, which would be better than 0,1,2 but i wouldn't worry about them just yet). the pseudo code would be something like






That would be easier, but we have to use a while loop or do/while loop. 
 
Junilu Lacar
Sheriff
Posts: 11165
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since you have written a method, I assume you at least understand what methods are for, which is to separate some functionality into it's own little "box" within the program. It's kind of like how a house is divided into different areas for cooking, hanging out, sleeping, taking care of personal needs, etc. Each room in the house has its own purpose. Likewise, a method in a program should have its own little purpose and task to complete. When you put multiple concerns/tasks in a method, it's kind of like putting your bed in your bathroom, or worse, putting your refrigerator in your bathroom.  You don't want to do that. Lumping  the task of getting user input in one method with checking if the user input represents a PA Resident is kind of the same thing, you don't want to do that.

Maybe right now your program looks something like this:

What I'm saying is that your program should have separate methods for different tasks, like this:

If you think of this program as a "conversation," it's one that goes something like this:


What you say on line 2 is done in one method and the determination that you make on line 6 is done in a separate method. So each method is really responsible for different parts of this "conversation" that you're trying to script out.
 
Junilu Lacar
Sheriff
Posts: 11165
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Daniel Martos wrote:
That would be easier, but we have to use a while loop or do/while loop. 

There's nothing that I see that's keeping you from doing that.

That's still using a loop AND it separates the task of getting user input and validation. If you use a while loop, same thing:

 
Junilu Lacar
Sheriff
Posts: 11165
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you find this all too confusing, you can still modify your current code based on the other hints you've already been given. I encourage you to revisit the other ideas later on though because these are important if you want to be able to write more complex programs. Separation of Concerns is a core programming principle that helps you write well-structured and well-organized programs.  It is especially important once you move away from writing all these public static methods and start using objects and object-oriented programming concepts.

I'd really be interested to hear what you think is way over your head though. It will help me understand what I might be able to simplify or clarify further.
 
Junilu Lacar
Sheriff
Posts: 11165
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's another tip: 

All three of these boolean expressions will be true if s is "A", "B", or "C".  Only caveat is that they will also be true if s is "AB", "BC", or "ABC"
 
Daniel Martos
Ranch Hand
Posts: 123
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fred rosenberger wrote:Good programming is kind of like using legos...although you have to make your own lego blocks.

So you want a bunch of little methods that each do one little thing, then you can combine them up into larger things, and so on. 

So, as an example, you might write a method that takes a string as input, and returns true if someone is a PA resident, and false if they are not.

You might have another method that takes a string and returns true if they are an international student, and false if they are not.

Then, you could have another method that takes a string as input, and have it call those first two methods, passing in the string it got. It could have some additional logic to decide if it needs to call both methods or not. And then, it could return a value like 0, 1, or 2 depending on whether they are a PA resident, an International student, or a non-resisdent. (You will eventually learn about Enums, which would be better than 0,1,2 but i wouldn't worry about them just yet). the pseudo code would be something like








I tried this and it isn't working? any ideas?  
   
 
Daniel Martos
Ranch Hand
Posts: 123
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:Here's another tip: 

All three of these boolean expressions will be true if s is "A", "B", or "C".  Only caveat is that they will also be true if s is "AB", "BC", or "ABC"




I tried this and it didn't work, any ideas?
 
Henry Wong
author
Sheriff
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Daniel Martos wrote:
I tried this and it didn't work, any ideas?


Well, what are the compiler error messages you are getting? And what do you think those error messages mean?

Henry
 
Norm Radder
Ranch Foreman
Posts: 2240
28
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it didn't work

It would be helpful if you described what happened when the code was compiled and executed.
 
Daniel Martos
Ranch Hand
Posts: 123
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Norm Radder wrote:
it didn't work

It would be helpful if you described what happened when the code was compiled and executed.

 
Daniel Martos
Ranch Hand
Posts: 123
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Norm Radder wrote:
it didn't work

It would be helpful if you described what happened when the code was compiled and executed.


 
Knute Snortum
Sheriff
Posts: 4081
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can't use multiple whiles with a do loop.

do {
    statements;
} while (condition);
 
Campbell Ritchie
Marshal
Posts: 55768
163
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:You can't use multiple whiles with a do loop. . . .
... but you can use multiple whiles. If you get the problem about unreachable code sorted out (you must not put code directly after a return statement), you will get runtime problems with the remaining whiles. Design suggestion: never write a semicolon after while (...)
 
Junilu Lacar
Sheriff
Posts: 11165
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Daniel Martos wrote:


You already wrote something that works, what exactly are you trying to "fix"?
 
Henry Wong
author
Sheriff
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:
You already wrote something that works, what exactly are you trying to "fix"?


I think the OP wants to "return" the result, which includes all the cases like in the do-while loop ... but instead of making the return statement like the condition in the do-while loop, made the do-while loop conditional more like the return (multiple) statement instead.

I agree with Junilu. It may be better to undo this last set of changes. And try to fix it again.

Henry
 
Daniel Martos
Ranch Hand
Posts: 123
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:
Daniel Martos wrote:


You already wrote something that works, what exactly are you trying to "fix"?


I couldn't figure out how to get the return result....
 
Daniel Martos
Ranch Hand
Posts: 123
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I couldn't figure out how to get the return result....
I need it to return results whether the input is P, N, or I
 
Knute Snortum
Sheriff
Posts: 4081
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Couldn't you just return the variable ans?
 
Junilu Lacar
Sheriff
Posts: 11165
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Daniel Martos wrote:I couldn't figure out how to get the return result....


I think maybe you're overthinking things. It's really quite simple:

1. You ask the user to enter an answer to your prompt.
2. You check that the answer is valid
3. You already have the validated answer in a variable (can you tell us which variable holds the valid answer?)
4. You want to return the answer that was validated

Now, does this give you any idea of what you need to return?

In other words, does "return the answer that was validated" translate to return !ans.equalsIgnoreCase(...)?

In even simpler terms, do you want to have conversation 1 below or conversation 2?

Conversation 1:
You: Please tell me if you're a P, N, or I
Guy: P
You: ok, so True

Conversation 2:
You: Please tell me if you're a P, N, or I
Guy: P
You: Ok, so PA Resident

Right now, your code represents conversation 1.  How do you make it represent conversation 2?
 
Daniel Martos
Ranch Hand
Posts: 123
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Martos4.java:63: error: incompatible types: String cannot be converted to boolean
      return ans;
             ^
1 error

----jGRASP wedge: exit code for process is 1.
----jGRASP: operation complete.
Knute Snortum wrote:Couldn't you just return the variable ans?
 
Daniel Martos
Ranch Hand
Posts: 123
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:
Daniel Martos wrote:I couldn't figure out how to get the return result....


I think maybe you're overthinking things. It's really quite simple:

1. You ask the user to enter an answer to your prompt.
2. You check that the answer is valid
3. You already have the validated answer in a variable (can you tell us which variable holds the valid answer?)
4. You want to return the answer that was validated

Now, does this give you any idea of what you need to return?

In other words, does "return the answer that was validated" translate to return !ans.equalsIgnoreCase(...)?

In even simpler terms, do you want to have conversation 1 below or conversation 2?

Conversation 1:
You: Please tell me if you're a P, N, or I
Guy: P
You: ok, so True

Conversation 2:
You: Please tell me if you're a P, N, or I
Guy: P
You: Ok, so PA Resident

Right now, your code represents conversation 1.  How do you make it represent conversation 2?


Yes, that is where I'm having the problem.  I understand that I need to make it conversation 2, but I don't know where I'm going wrong with it.
 
Junilu Lacar
Sheriff
Posts: 11165
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry if this sounds mean, Daniel, but I really have a hard time understanding how you can't see the answer -- it's both mind-boggling and aggravating. Please go back a few responses and read each one very carefully. Somebody actually gave you the answer outright.
 
Daniel Martos
Ranch Hand
Posts: 123
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:Sorry if this sounds mean, Daniel, but I really have a hard time understanding how you can't see the answer -- it's both mind-boggling and aggravating. Please go back a few responses and read each one very carefully. Somebody actually gave you the answer outright.


Not mean at all, I can understand how a programmer can get frustrated with a 38-year old coal miner trying to learn coding for the first time.....I tried to return ans, but it didn't work.
I tried return ans
return !ans.equalsIgnoreCase (ans);
return ans.equalsIgnoreCase (ans);

I know it's right before my eyes, but I'm not seeing it, man.
 
Junilu Lacar
Sheriff
Posts: 11165
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, I'm going to do you a favor and give you the proper code:

We don't like giving out solutions like this because we feel that in general, it deprives you of a learning opportunity. These things can happen sometimes where you have something right in front of you but just don't recognize it. But please do us a favor in return and understand WHY this code works and why what you were doing didn't.  We would be especially happy if you posted a reply explaining exactly why this code works and why your code didn't.
 
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
Daniel Martos wrote:
I tried return ans
return !ans.equalsIgnoreCase (ans);
return ans.equalsIgnoreCase (ans);

I know it's right before my eyes, but I'm not seeing it, man.


Neither one of those examples is returning the ans variable. Have you tried ...



Henry
 
Daniel Martos
Ranch Hand
Posts: 123
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:Sorry if this sounds mean, Daniel, but I really have a hard time understanding how you can't see the answer -- it's both mind-boggling and aggravating. Please go back a few responses and read each one very carefully. Somebody actually gave you the answer outright.


I know I must return ans, but my method needs to return a String, not a boolean......that's where this issue seems to be and I don't know how to fix that
 
Junilu Lacar
Sheriff
Posts: 11165
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Daniel Martos wrote:I know I must return ans, but my method needs to return a String, not a boolean......that's where this issue seems to be and I don't know how to fix that

This says you're going to return a String:

This says you're going to return a boolean:

 
Daniel Martos
Ranch Hand
Posts: 123
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:
Daniel Martos wrote:
I tried return ans
return !ans.equalsIgnoreCase (ans);
return ans.equalsIgnoreCase (ans);

I know it's right before my eyes, but I'm not seeing it, man.


Neither one of those examples is returning the ans variable. Have you tried ...



Henry


 

----jGRASP exec: javac -g Martos4.java
Martos4.java:31: error: incompatible types: boolean cannot be converted to String
String paResident = isPAResident();
                                ^
Martos4.java:34: error: incompatible types: String cannot be converted to boolean
double tuitionCost = calculateTuition(numCourses, paResident);
                                                  ^
Martos4.java:63: error: incompatible types: String cannot be converted to boolean
      return ans;
             ^
Note: Some messages have been simplified; recompile with -Xdiags:verbose to get full output
3 errors

----jGRASP wedge: exit code for process is 1.
----jGRASP: operation complete.
 
Daniel Martos
Ranch Hand
Posts: 123
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:
Daniel Martos wrote:
I tried return ans
return !ans.equalsIgnoreCase (ans);
return ans.equalsIgnoreCase (ans);

I know it's right before my eyes, but I'm not seeing it, man.


Neither one of those examples is returning the ans variable. Have you tried ...



Henry


 

----jGRASP exec: javac -g Martos4.java
Martos4.java:31: error: incompatible types: boolean cannot be converted to String
String paResident = isPAResident();
                                ^
Martos4.java:34: error: incompatible types: String cannot be converted to boolean
double tuitionCost = calculateTuition(numCourses, paResident);
                                                  ^
Martos4.java:63: error: incompatible types: String cannot be converted to boolean
      return ans;
             ^
Note: Some messages have been simplified; recompile with -Xdiags:verbose to get full output
3 errors

----jGRASP wedge: exit code for process is 1.
----jGRASP: operation complete.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!