![]() |
error trappring and looping
the code below is a simple menu driven program to calculate loan stuffs. i have to beable to trap bad user input and tie that with looping of the program. also the main goal of this assignmnet is to use methods. there are my problems
1. in displayMenu(),as it is now, the user must pick a letter from the menu but what if the use picks a bad letter? i have to trap bad input, tell the use that it was a bad input and then get good input. i know this involves some sorta loop and conditional. i dont know how to test input if it is not P,I,T or Q and loop based on that. :
|
you need to throw the menu into a loop and not break out of it if it hits the default switch statement.
|
Quote:
:
|
This is not so much a language issue as it is a design issue. User input devices rarely, if ever, give you directly useful information. They provide binary patterns that indicate that this or that key was pressed, or this or that point on the screen was touched, or whatever. Your machine is not smart. It is merely a reactive device constrained by its hardware implementation.
Your machine has little, if any, utility, out of the box. Free (seemingly) utility comes from those who have decided, for whatever reason, to make your job easier. When you press a key (if you have keys) on your device, it doesn't matter if that key is labeled "ENTER", or "BOINK MY WIFE". That key produces a binary output. Someone gets to determine what it means. In the most common systems available to novices today (desktop/laptop machines), this is a two-step process, or more. The input device produces a code. The OS converts this code to some other code or sequence of codes. The language (which has to be defined, implementation-wise) for that OS converts this other code to some other code, or sequence of codes. The user of that language has to decide whether this latter code represents, say, text, or a number, or some control mechanism. Fortunately, for most users, someone else has done that. Your responsibility is to read what that someone else has said about how that code, so thoughtfully provided, works. Two people working for you may have different ideas. User input is particularly invidious. If you're reading a file, you might have failures because of your own errors, or a corrupt file, or a broken mechanism. With human users, one has the added burden of dealing with inattention, stupidity, maliciousness, or a number of other things. You must know what you're writing, or what your utilities think of certain kinds of input, and how they deal with it. Failing to contend with this situation is, at best, ignorance. At worst, it's negligence or overt malice. Such actions may subject you to no penalties whatsoever. They could conceivably subject you to rather harsh penalties meted out in a court of civil or criminal justice. Budding professionals need to think about and deal with these things. Read your documentation with an eye for your concerns. |
I took some time to review a few things and came up with this. I would like your comments and suggestions for making the code more effient and "better". thanks for the help so far.
:
|
One key is to look at your logic carefully. You're hacking a path through the jungle over and over when your destination is always the same. Why not just,
:
public static char getMenuChoice() |
| All times are GMT -5. The time now is 1:51 AM. |
Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC