![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Hobbyist Programmer
|
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. java Syntax (Toggle Plain Text)
__________________
i dont know much about programming but i try to help |
|
|
|
|
|
#2 |
|
Programmer
Join Date: Sep 2005
Posts: 50
Rep Power: 4
![]() |
you need to throw the menu into a loop and not break out of it if it hits the default switch statement.
|
|
|
|
|
|
#3 | |
|
Hobbyist Programmer
|
Quote:
java Syntax (Toggle Plain Text)
![]()
__________________
i dont know much about programming but i try to help |
|
|
|
|
|
|
#4 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#5 |
|
Hobbyist Programmer
|
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.
java Syntax (Toggle Plain Text)
__________________
i dont know much about programming but i try to help |
|
|
|
|
|
#6 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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()
{
Scanner scanner = new Scanner(System.in);
String stringInput;
char cIn;
do
{
stringInput = scanner.next();
cIn = Character.toUpperCase(stringInput.charAt(0));
if ((cIn == 'P') || ((cIn == 'I') || ((cIn == 'L') || ((cIn == 'Q')) return cIn;
System.out.print("Select P,I,T or Q: ");
} while (true)
}
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Looping effect, when entering string in an int variable | lamefif | C++ | 7 | Jan 5th, 2006 11:29 AM |
| Endless looping >_< | jch02140 | C | 13 | Aug 4th, 2005 6:43 AM |
| looping in awk | gwilliam | Sed and Awk | 3 | Jun 20th, 2005 9:12 AM |