• Post Reply Bookmark Topic Watch Topic
  • New Topic

How can you use a loop to track the number of times an option has been chosen?  RSS feed

 
Steven Youngson
Greenhorn
Posts: 6
Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all!

I'm new to the forums and I come with a problem. It's been bugging me for the past 2 days and I can't seem to get anywhere with it!

I've been learning Java through the book "Java in 2 Semesters" which you may be familiar with. It was recommended to me through a gaming community forum and up to this point I was getting on fine with it.

I'm on Chapter 3 Iteration and I'm on to the practical questions at the end of the chapter. It's asking me to track the number of times each option in the program has been chosen and to print this info when option 5 is chosen.

Here is what I've written so far. It allows a user to choose each option and then asks for their choice again. When you choose option 5 it quits. It writes a "1; 2; 3; 4; " at the minute when you choose 5 as this was a vain attempt by me to get it to print out the number of times each case had been chosen.

So far, the program only has a do...while loop to show the menu of options and how to quit the program as I don't know what loop I should be using to get the info returned to me?

What type of loop am I supposed to use and how would it be written in order to return the number of times each option was chosen?:

/* This program is from the "Java in 2 Semesters" book, Chapter 3 Tutorial Exercise 5 (also Practicle Qu3).
This is the program so far. The question asks me to print out the amount of times each option has
been chosen when the program quits by choosing option number 5. I have no idea how to get the program
to track the options and then print them? I'm guessing I have to use a loop but which one and how to
write it I have no idea. The book hasn't given any previous examples as to how to write this and I can't
seem to see any way of doing it. Please help. Cheers!*/



Thanks!
 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

I am not familiar with that book, but I am always suspicious of books which put everything in the main method; you don’t leanr object-orientation like that.
Do you know about arrays? I would suggest you create yourself a VendingMachine class, and a Snack class. The Vending Machine class can have a Snack[] and an int (itemsSold, maybe), as its fields. Or a String[] with the descriptions to create Snack objects from. Remember array indices start at 0, not 1.
The Snack class can have a String (maybe description) as its solitary field (you can add more fields later if required). Give the vending machine class a sell method and a getItemsSold method. Then you can call them from some other method.
 
Steven Youngson
Greenhorn
Posts: 6
Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your reply. I am not familiar with arrays yet. Think they are coverd in a few chapters time of this book.

I think this book is putting everything in the main method at present to limit the amount you need to know to get some sort of tangible result and it will likely progress once I understand the basic functions. It hasn't even went into using a GUI yet, just getting the program to show in the command window at the bottom of my IDE.

Although your post hasn't answered the question I posted, it has answered the question that has been going round in my head:

"Will I be screwed if I can't get this to work or will there be another way of doing this when I learn how to use Java better?"

So from what you've written, it seems like there will be a more effective way of writing such a program once I understand how arrays work and can split the program into different classes?

If that's the case, then brilliant! I can move on to learning more and come back to this at a later date when I understand it better!

Cheers mate!


 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if you can't use arrays...

I would go back to the paper-and-pencil method. How would you, personally, do this task? If you knew you had to ask someone over and over for selections, and then when they said "i'm done choosing" you had to tell them how many times they picked each of 1-4, how would you keep track?

personally, I'd divide a piece of paper into four areas, and make tally marks for each selection. Then, when I was done, I'd look at each section and tell them how many tallies were in each...and that sounds like an array (i.e. on thing with four parts...).

However, you said you can't use arrays. If I can't have one thing with four parts, i may need four things - i.e. four pieces of paper, each labeled "choice 1", "choice 2", etc. And that sounds like four independent variables...
 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Fred is right. And forget about GUIs for the time being. They may be pretty, but they only work when the underlying logic is correct. You need to learn the underlying bit at this stage.
 
Steven Youngson
Greenhorn
Posts: 6
Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Fred, I think you have hit the nail on the head! I could assign 4 different variables, one to each choice, to a loop so that each time it is chosen it will add one to the variable and have the case for option 5 to print out their final accumulation.

Gonna give it a try anyway!

Cheers
 
Steve Fahlbusch
Bartender
Posts: 612
7
Mac OS X Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If i may.... try to get it to work with one or even two choices. Then know you can do it for the rest.

Not that this isn't an important issue, but without arrays,(or other data structures) once you have two, it is all just busy work.

If you want to implement, do so with one or two cases..... after that, it is just busy work.

-steve
 
Steven Youngson
Greenhorn
Posts: 6
Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Eureka!!!

I got it to work! Chuffed with myself!

I declared 4 seperate integers, wrote 4 for loops and added each one to the end of the choices in the cases of the do loop, then added those variables to the print out for each option!

So satisfied with completing that question!

Below is how I wrote it.

Thanks to everyone that posted!!

 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
one suggestion...


a variable name of "one" is a terrible choice. one what? does it hold the value "1"? is it the first instance of something?

using characters is cheap...I would STRONGLY recommend using names like

choiceOneCount
choiceTwoCount
etc.

looking at a variable name like that, it is obvious what it refers to. Remember, you will come back to this code in a few weeks and have to figure it out again. Make it as easy as possible on yourself. A common mantra I've heard is "Always write code as if the next person to read it is a homicidal maniac with your home address".
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Steven Youngson wrote:I've been learning Java through the book "Java in 2 Semesters" which you may be familiar with.

No, and my worries about a book with such a title would be the same as Peter Norvig's; and my worry about the code structure the same as Campbell's.

I got it to work! Chuffed with myself!

Well done. Now try to break some of that method up into logical pieces, and give yourself an even bigger pat on the back when you've done that, because then you'll have started to program.

Winston
 
Steven Youngson
Greenhorn
Posts: 6
Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cool, good shout with the variable naming. Duely noted. As I said I'm right at the beginning of learning the language and tips are much appreciated! I did go back and put comments in the code after I'd posted my results here to jog my memory for when I return to it.

The next bit I've read is going onto objects and classes so from there I should be able to learn how to break my codes up into more than one method!

Thanks again!
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I may be missing something here, but can you explain what the for loops between lines 11 and 34 are doing ?
 
Steven Youngson
Greenhorn
Posts: 6
Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joanne Neal wrote:I may be missing something here, but can you explain what the for loops between lines 11 and 34 are doing ?


Great point!

They were in there as I thought I had to use nested for loops to give the integers a counter in order to keep track of how many times each option was chosen.

I just deleted them and initialised the integers to be zero when they were declared and the program still runs the same! Good spot mate as I would of continued to write them in this way which was totally unnecessary!

Also I took what Fred said and changed the integers to a suitable name.

Here's what it looks like now



Thanks again!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!