• Post Reply Bookmark Topic Watch Topic
  • New Topic

while loop with sentinel  RSS feed

 
Daniel Martos
Ranch Hand
Posts: 126
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In my last assignment, my program had to print out:

Please enter the number of courses you are taking:
Please enter the number of credits you are taking for course 1:
Please enter the number of credits you are taking for course 2:
Please enter the number of credits you are taking for course 3:
Please enter the number of credits you are taking for course 4:,

and so on, for as many courses that I am taking.....so I used this code



This assignment, I have to eliminate the "Please enter the number of courses you are taking" and the student needs to enter the number of credits for each class until they reach a sentinel value (in this case -1) and I'm completely lost, any advice would help. Needs to be in a while loop.
   
 
Ganesh Patekar
Bartender
Posts: 726
23
Eclipse IDE Hibernate jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I understood correctly, you want student to enter credits for course and add those credits until student enter value of credit as -1 ?

Then you may need int variable credit for input from student, a condition if credit is positive then add that credit to sumCredits, execute while loop till credit != -1. If want to print no of course while asking student to enter credit then you need another int variable counter to count entered credits for no of courses.

You can use nextInt() method of Scanner for input of credit.
 
Junilu Lacar
Sheriff
Posts: 11477
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
Daniel,

Don't be thrown off by the fancy name; there's nothing super complex about sentinel values.  In plain English pseudocode:

 
Daniel Martos
Ranch Hand
Posts: 126
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ganesh Patekar wrote:If I understood correctly, you want student to enter credits for course and add those credits until student enter value of credit as -1 ?

Then you may need int variable credit for input from student, a condition if credit is positive then add that credit to sumCredits, execute while loop till credit != -1. If want to print no of course while asking student to enter credit then you need another int variable counter to count entered credits for no of courses.

You can use nextInt() method of Scanner for input of credit.


Yes you understood correctly, I do not need to enter number of courses
 
Daniel Martos
Ranch Hand
Posts: 126
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:Daniel,

Don't be thrown off by the fancy name; there's nothing super complex about sentinel values.  In plain English pseudocode:



If I eliminate this from my code:

What would I set numCourses equal to?
 
Henry Wong
author
Sheriff
Posts: 23295
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:
What would I set numCourses equal to?


Well, simply, you don't. The loop can't terminate with a loop count anymore, as the count isn't known in the beginning. You need to use some sort of flag, which is set, as you are reading in the input from the user.

Henry
 
Junilu Lacar
Sheriff
Posts: 11477
180
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:
If I eliminate this from my code:

What would I set numCourses equal to?


I hate to keep having to say this: I don't want to sound mean but frankly, this is the kind of thing an aspiring programmer has to learn to figure out for himself/herself. It's called logic. Granted, it may take some time for you to get a hang of thinking in a systematic way but if you don't flex your gray matter, then I'm afraid you're never going to be able to handle more difficult and complex tasks.

That said, I feel for you so I'll give more pseudocode:

When you exit the WHILE loop after the user enters the sentinel value, the count will tell you how many times the "DO stuff" part was done.
 
Daniel Martos
Ranch Hand
Posts: 126
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:
Daniel Martos wrote:
What would I set numCourses equal to?


Well, simply, you don't. The loop can't terminate with a loop count anymore, as the count isn't known in the beginning. You need to use some sort of flag, which is set, as you are reading in the input from the user.

Henry


I wish I knew what that meant.  I don't know what a flag is.....My assignment is due in an hour and I'm freaking out.  If you have been paying attention this week, you know I haven't waited until the last minute to start working on this.
 
Daniel Martos
Ranch Hand
Posts: 126
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:
Daniel Martos wrote:
If I eliminate this from my code:

What would I set numCourses equal to?


I hate to keep having to say this: I don't want to sound mean but frankly, this is the kind of thing an aspiring programmer has to learn to figure out for himself/herself. It's called logic. Granted, it may take some time for you to get a hang of thinking in a systematic way but if you don't flex your gray matter, then I'm afraid you're never going to be able to handle more difficult and complex tasks.

That said, I feel for you so I'll give more pseudocode:

When you exit the WHILE loop after the user enters the sentinel value, the count will tell you how many times the "DO stuff" part was done.


Once again, not offended.  This is my 5th week of ever writing code....ever.  It's an accelerated online class. My textbook is horrible.  As you may have noticed I'm trying to figure this stuff out....I feel like I'm starting to get it, but you have to realize some of the advice real life programmers give, isn't running along with what my textbook and professor are teaching, so I'm working to figure this stuff out...I've never heard of set count to 0....I understand do stuff, I think I got increment count, and the user should have already entered a value in this example.  Trust me, If I threw you in a coal mine and told you to start operating million dollar equipment, I'd hope you'd ask me questions too.
 
Knute Snortum
Sheriff
Posts: 4274
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To "set" a variable is just to give it a value:

Here, count is set to zero.  To increment a variable is just to add one to it.

This is such a common thing that it has a shortcut:

and even a shorter shortcut!
 
Junilu Lacar
Sheriff
Posts: 11477
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
Daniel Martos wrote:Once again, not offended.  This is my 5th week of ever writing code....ever.  It's an accelerated online class. My textbook is horrible.  As you may have noticed I'm trying to figure this stuff out....I feel like I'm starting to get it, but you have to realize some of the advice real life programmers give, isn't running along with what my textbook and professor are teaching, so I'm working to figure this stuff out...I've never heard of set count to 0....I understand do stuff, I think I got increment count, and the user should have already entered a value in this example.  Trust me, If I threw you in a coal mine and told you to start operating million dollar equipment, I'd hope you'd ask me questions too.

I want to commend you for your perseverance. I imagine it's not easy to retool yourself after working in a very different industry for a long time. I certainly wish you success and hopefully we can help in whatever way we can. I can tell you this: your attitude, self-awareness, and openness to critique without taking any of it personally even when others might otherwise naturally be defensive is very admirable and will be something that will help you overcome obstacles and challenges. The best programmers are egoless and by that measure alone, you are already well equipped, IMO.
 
Campbell Ritchie
Marshal
Posts: 56527
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Daniel Martos wrote:. . . .  This is my 5th week of ever writing code....ever.  It's an accelerated online class. My textbook is horrible.  . . . .
Which book is it?

I worry about courses which claim to teach everything in n days when everybody knows you need ten years. There is only a certain speed the brain can assimilate such information and you will get information overload if you try to go any faster. That is why students do nothing else but learn: so they have time to understand things. Are you working full-time in parallel with this learning?
 
Henry Wong
author
Sheriff
Posts: 23295
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 wish I knew what that meant.  I don't know what a flag is.


Sorry. I seemed to have tried to explain a term by introducing another one.

In this case, by "flag", I meant any indication that it is time to end the loop. It could be simply a boolean variable, that is set to true, when the user enters "-1" for the number of credits -- and the loop terminates when true. It could be the value that the user enters, saved to a int variable -- and the loop terminates when it is equal to -1.

Henry
 
Daniel Martos
Ranch Hand
Posts: 126
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:
Daniel Martos wrote:. . . .  This is my 5th week of ever writing code....ever.  It's an accelerated online class. My textbook is horrible.  . . . .
Which book is it?

I worry about courses which claim to teach everything in n days when everybody knows you need ten years. There is only a certain speed the brain can assimilate such information and you will get information overload if you try to go any faster. That is why students do nothing else but learn: so they have time to understand things. Are you working full-time in parallel with this learning?


Yes, sir. 60 hours a week.
 
Daniel Martos
Ranch Hand
Posts: 126
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:
Daniel Martos wrote:. . . .  This is my 5th week of ever writing code....ever.  It's an accelerated online class. My textbook is horrible.  . . . .
Which book is it?

I worry about courses which claim to teach everything in n days when everybody knows you need ten years. There is only a certain speed the brain can assimilate such information and you will get information overload if you try to go any faster. That is why students do nothing else but learn: so they have time to understand things. Are you working full-time in parallel with this learning?


Building Java Programs: A Back to Basics Approach
 
Campbell Ritchie
Marshal
Posts: 56527
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you. I presume this is the book you are using.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!