You guys...I need a little help...What I want to do is...ask a user if he wants to enter more records, and if so, he can enter as many as he likes... Well I am stuck on how to do this.. I know I need something like
System.out.println("Do you want to enter more pets?"); System.out.println("Enter y for yes, or n for no."); answer = SavitchIn.readLineNonwhiteChar(); } while ((answer =='y') || (answer =='Y'));
But I'm not exactly sure where to put it. I know my code so far, only lets you input five pets at a time, and I did that on purpose....but now I want them to be able to enter any number of pets.
Can you give me some hints...and remember...talk in small talk for me...I'm not that well versed yet with Java.
Rose, you could use a while loop around the lines where you get the pet's info. And while answer.equals('y') .... keep going. The user would have to enter 'y' when prompted otherwise the while loop would exit.
I have seen your posts on this message board several times in the past and I admire the effort you are giving towards learning how to program in Java. I admit that it is a challenging task at times. However, I wonder if there is something missing in your learning process. I find it helpful to understand when to use certain tools. For example, I'm sure you have learned about the basic control structures that are available in Java such as if statements, for loops, and while loops. However, it's not enough to know that these tools exist. You also need to be intimately familiar with the kinds of situations that warrant there use.
In this case, you describe in your original post a situation where you need to repeat something over and over again. To me, this just screams "LOOP!" That's still not quite enough information, though, because Java has two different types of loops: for and while. Typically, you use a for loop when you know EXACTLY how many times the action is repeated; it stops after a certain number of repitions (or iterations). On the other hand, a while loop uses some situation arises that determines when to stop looping. So this implies that, since you don't know how many times to repeat, that you need to use a while loop.
My point here isn't to just answer your question directly, as you can see by my rambling. I hope you can make some effort to understand when it is appropriate to use each of the tools that you learn about while you learn about programming computers. If you can do this then you will find that learning other programming languages will be much, much easier. As much as I like Java, I would be willing to bet that other languages will come along to replace it. This means that learning how to learn a programming language is much more important than just learning the language itself.
I have to agree with what Layne said. I don't want to discourage you in any way but here's a few things you might do to speed along your process.
1)Learn to use the API.
2)Create a program on your own, find something you're interested in and create a program for it. If you love to cook you could do a recipe program, or you could do a simple banking program, or even a program that keeps track of your favorite TV shows or something. The point is sometimes by only doing your school/book assignments you just do whatever is needed to complete the assignment and don't think that deeply about what your doing, or more importantly why your doing it.
3) This forum is wonderful place, but if you use it as a crutch to answer every question you don't really learn to think and find things for yourself.
4) Learn to think as a programmer in everyday life. It may sound stupid but while you're driving to school or work or whatever imagine you're in a program. If I turn left here this happens, else I keep on the same path. While I have my foot on the gas my going forward loop continues. For every mile I go I get one mile closer to having to fill my gas tank. Programming is all about decisions, everything else is just syntax.
4) Learn to think as a programmer in everyday life.
All good advice from Hentay and Layne both. And these things go for just about any programming language, not just Java.
I'll add a corollary to #4:
5) Learn to think as a human in programming life.
In other words, if you had to do the problem yourself, or (better yet) if you had to tell someone else how to do it, rather than get a computer to do it, what would you do? What sort of instructions would you write down? I recommend imagining a really, really dumb intern.
Or, as Peter Halpern once put it, "if you can't write it down in English, you can't code it."
You guys thanks for all the wonderful help and advice!! You see, I do think about what a program is doing, or what it is supposed to do. I know exactly in my 'head' or 'on a piece of paper' what I need my program to do, but sometimes coding it, is the tricky part for me.
I wish so badly that I had more time to just spend with my programming, I honestly, truly love programming, but it's so hard to concentrate on just that, when I have four other classes to study for too. That's what keeps me from learning as much Java as I need to. My classes this semester, all of them, require a great deal of study. Not an excuse, just the facts.
Trust me, I wake up at night, and think to myself, "Oh, I'll try this in the morning to see if it will work". Programming fascinates the heck out of me. I haven't learned the API thing to tell you the truth. Actually, I had never even heard of it, till you guys mentioned it on here one day. I've got to teach myself how to use that.
I do understand my program needs a loop, I just have a hard time figuring out how to actually code that where it belongs, without getting some type of syntax error.
I figured out something yesterday on a program just by trying different things until I made it do what I wanted it to do, and I was very proud of myself!!
As far as my school assignments go. I ALWAYS want to do more than what the assignment asks for. I can see all kinds of improvements to the assignments, that I believe could be made, it's mainly the time thing that hinders me.
And yes, I agree, it's nice to write pseudocode or some type of code in 'dummy terms', so that any idiot could write the program.
I thoroughly believe in great instructions for any type of job, with no pieces left to figure out.
I try not to use this forum as a crutch....I just love to get feed back, hints as to what I am doing right or wrong. I want to write the programs myself and figure them out, but I have found that two heads are better than one, and one programmer might see a great way of doing something better than you had coded, and vice versa. I love learning from others, especially if they know the language well. They can always teach me things.
I love to teach myself. Strange as it may seem, actually sometimes 'I' have shown my class mates how to do something, whether it be in Java, Visual Basic.NET, or other languages I have learned.
You can learn so much from others.
I tried to answer all of you in one post. Sorry for the delay in my response....had to go to a funeral today out of town, and it's now 4:30, and I am just now getting to work. I'll be here till 9 Pm....Ouch!!!
Trust me, I wake up at night, and think to myself, "Oh, I'll try this in the morning to see if it will work".
Heh, I've done that so many times. I hate it when I wake up in the morning and forgot my brilliant idea. I used to have a notebook or paper by my bed so I could write something down to help me remember. Unfortunately, I got out of the habit of doing that.
Also, I DEFINITELY understand the time constraints. I'm not very good at managing my time and often my OTHER classes suffer because I really get into a programming project.
Well good luck with your further coding endeavors and come back with more questions. I hope I didn't come off like we won't answer them. We certainly will, although maybe not in the way you expect.
Layne, I hate that I have to spend so much time on the 'bull' classes, as I call them. I want only to program, not to study goofy classes, but I must try to keep up with those 'goofy' classes or my darn GPA will fall so low. I wish I only had programming classes, that would be great. Just like right now, I have four online psychology test to take, and I'd rather be programming......... Thanks for all of your help!!! It is truly appreciated!!! I haven't put a notebook beside my bed yet, but I think I will.
Keep at it Rose, you have the enthusiasm and if you really want it one day you'll have the time to devote to it (hopefully when you get a job that aloows you to do it full time). Don't let any of my or anyone else's suggestions disourage you, they're intended to help and not hurt your learning.
Good Luck and keep coming back with questions. I always enjoy your posts because you always seem to have a smile on your face!