File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Entering many records..Need help! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Entering many records..Need help!" Watch "Entering many records..Need help!" New topic
Author

Entering many records..Need help!

Rose Evans
Ranch Hand

Joined: Oct 14, 2004
Posts: 195
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.



Thanks a million!!!


) ) ) )
Hentay Duke
Ranch Hand

Joined: Oct 27, 2004
Posts: 198
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.
Jeff Bosch
Ranch Hand

Joined: Jul 30, 2003
Posts: 805
You probably want to "seed" the loop by prompting before entering the loop in case the user does not want to enter any records. In Java-like pseudocode, you'd have something like this:



Around and around you will go until the user answers NoLetMeOut, upon which the loop terminates.

Hope that helps!


Give a man a fish, he'll eat for one day. Teach a man to fish, he'll drink all your beer.
Cheers, Jeff (SCJP 1.4, SCJD in progress, if you can call that progress...)
Jeff Jetton
Ranch Hand

Joined: Mar 29, 2005
Posts: 71
and remember...talk in small talk for me...I'm not that well versed yet with Java


That's okay. I'm not that well-versed in Smalltalk.

- Jeff
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Rose,

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.

Keep Coding!

Layne


Java API Documentation
The Java Tutorial
Hentay Duke
Ranch Hand

Joined: Oct 27, 2004
Posts: 198
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.

Keep working at it, you'll get there.
Jeff Jetton
Ranch Hand

Joined: Mar 29, 2005
Posts: 71
Originally posted by Hentay Duke:

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."

Good luck!

- Jeff
Jeff Bosch
Ranch Hand

Joined: Jul 30, 2003
Posts: 805
Rose -

Did you take an algorithms class before jumping into Java? This question is so common that many (if not all) algorithm books cover it in some detail.
Rose Evans
Ranch Hand

Joined: Oct 14, 2004
Posts: 195
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!!!

Thanks for all your comments!!!
Rose Evans
Ranch Hand

Joined: Oct 14, 2004
Posts: 195
You guys...lookie, lookie....See I figured out something all by myself. I added scroll bars to a different program......I looked and looked for some great examples on how to do it, and I did it!!!
Jeff Bosch
Ranch Hand

Joined: Jul 30, 2003
Posts: 805
I've read your posts ==> You've figured out lots of things on your own. You go, girl!
Rose Evans
Ranch Hand

Joined: Oct 14, 2004
Posts: 195
Thanks Jeff, I'm really trying!!!
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
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
Rose Evans
Ranch Hand

Joined: Oct 14, 2004
Posts: 195
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.
Jeff Bosch
Ranch Hand

Joined: Jul 30, 2003
Posts: 805
I've kept a notebook by my bed for years. Many times I've been grateful for having it there, and for whoever it was who gave me the idea mumble-mumble years ago.
Hentay Duke
Ranch Hand

Joined: Oct 27, 2004
Posts: 198
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!
Rose Evans
Ranch Hand

Joined: Oct 14, 2004
Posts: 195
Hentay,

Thanks for your post. I always read everyone's advice and try to look on the bright side of it all. I do appreciate everyone's help.
Thank you all so much!!!



I'll keep at it, and see where it takes me. It's time to go home for the day, so everyone have a great weekend.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Entering many records..Need help!