ps. also have a look of the JFormattedTextF ield; it takes care of most
those nasty mechanics.
kind regards,
Jos
Yes, I looked at it after I finished my little demo, but it allows you to enter characters outside of its preferred set and then rejects them at the end as opposed to my class which doesn't even allow you to enter non-numeric characters. However, JFormattedTextF ield would have been a better base class to use instead of JTextField. I'll leave that as an exercise for the reader. ;o)>>> Sam
I think I may have something here guys, I've been putting it down and getting back into it to free my mind. Needs more work, I seem to be looping fine but I move on to next InputDialog even after data were found to be invalid. I do get the InputBox again to re-enter but only after I have gone through the others. I may have to go away and come back again, but what do you see happening:
// Gets three input values--loan amount, interest rate, and
// loan period
[CODE=JAVA]
private void getInput() {
double loanAmount = 0, annualInterestR ate = 0;
int loanPeriod = 0;
String inputStr;
//An attempt to ensure user adds in amount from 100 to 1000000, no more, no less...
while (loanAmount < 100 || loanAmount > 1000000 ) {
inputStr = JOptionPane.sho wInputDialog(nu ll, "Loan Amount (Dollars + Cents):");
try {
loanAmount = Double.parseDou ble(inputStr);
// catch the user's entry here and throw an error
//User will need to re-enter data to continue
if (loanAmount < 100 || loanAmount > 1000000 ) {
throw new Exception("Loan Amount is invalid, please try again...");
}
//Catch instances were user either adds in vowels or leaves pop up empty
//Our user will need to re-enter information but the correct data type, digits only...
} catch (NumberFormatEx ception e) {
JOptionPane.sho wMessageDialog( null, "'" + inputStr
+ "' is an invalid data type\n"
+ "Please enter digits only");
//Catch instances were user either adds in vowels or leaves pop up empty
//Our user will need to re-enter information but the correct data type, digits only...
} catch (Exception e) {
try {
annualInterestR ate = Double.parseDou ble(inputStr);
// catch the user's entry here and throw an error
//User will need to re-enter data to continue
if (annualInterest Rate < 5 || annualInterestR ate > 9 ) {
throw new Exception("Loan Amount is invalid, please try again...");
}
//Catch instances were user either adds in vowels or leaves pop up empty
//Our user will need to re-enter information but the correct data type, digits only...
} catch (NumberFormatEx ception b) {
JOptionPane.sho wMessageDialog( null, "'" + inputStr
+ "' is an invalid data type\n"
+ "Please enter digits only");
//Catch instances were user either adds in vowels or leaves pop up empty
//Our user will need to re-enter information but the correct data type, digits only...
} catch (Exception b) {
while (loanPeriod < 1 || loanPeriod > 30 ) {
inputStr = JOptionPane.sho wInputDialog(nu ll, "Loan Period (Number of years):");
try {
loanPeriod = Integer.parseIn t(inputStr);
// catch the user's entry here and throw an error
//User will need to re-enter data to continue
if (loanPeriod < 1 || loanPeriod > 30 ) {
throw new Exception("Loan Period is invalid, please try again...");
}
//Catch instances were user either adds in vowels or leaves pop up empty
//Our user will need to re-enter information but the correct data type, digits only...
} catch (NumberFormatEx ception i) {
JOptionPane.sho wMessageDialog( null, "'" + inputStr
+ "' is an invalid data type\n"
+ "Please enter digits only");
//Catch instances were user either adds in vowels or leaves pop up empty
//Our user will need to re-enter information but the correct data type, digits only...
} catch (Exception i) {
I guess it is okay to loop through other InputDialog boxes to again arrive at the original InputDialog that errored, then re-enter the data. It'd be cool to get it to just go right back to the loanAmount Dialog after data were found to be incorrect, then move on to next Dialog, it's possible right!
Almost forgot to mention, I am also trying to salvage ideas from your code to see if that remedies the issue...
Thanks for posting:-)
Last edited by Dököll; Mar 27 '08, 06:31 AM.
Reason: text...
I still cannot shake it guys, I am trying to have my InputDialog to loop back to me so I can re-enter, and only after that part is handled, errors checked should I move on to the next Input
[CODE=JAVA]
//An attempt to ensure user adds in amount from 100 to 1000000, no more, no less...
while (loanAmount < 100 || loanAmount > 1000000 ) {
inputStr = JOptionPane.sho wInputDialog(nu ll, "Loan Amount (Dollars + Cents):");
try {
loanAmount = Double.parseDou ble(inputStr);
// catch the user's entry here and throw an error
//User will need to re-enter data to continue
if (loanAmount < 100 || loanAmount > 1000000 ) {
throw new Exception("Loan Amount is invalid, please try again...");
}
//Catch instances were user either adds in vowels or leaves pop up empty
//Our user will need to re-enter information but the correct data type, digits only...
} catch (NumberFormatEx ception e) {
JOptionPane.sho wMessageDialog( null, "'" + inputStr
+ "' is an invalid data type\n"
+ "Please enter digits only");
//Catch instances were user either adds in vowels or leaves pop up empty
//Our user will need to re-enter information but the correct data type, digits only...
} catch (Exception e) {
Looks like my only option thus far is go through all InputDialogs. Can anyone give me an idea how to finish checking loanAmount for valid data and if invalid to loop back to its InputDialog to re-enter stuff, pheew that was a long one:-)
Thanks much!
Dököll
Last edited by Dököll; Apr 3 '08, 10:59 AM.
Reason: uneeded text...
Java class is all over for me. Squeeked by with a 106 average! LOL! Now, I'm taking an intro to XML. It's taught by the same teacher as the Java course, but it's all on-line and should be much less work.
Java class is all over for me. Squeeked by with a 106 average! LOL! Now, I'm taking an intro to XML. It's taught by the same teacher as the Java course, but it's all on-line and should be much less work.
106 average! LOL! a definite WOW to me, good job... You can now relax... We have six more labs about and one final:-)
Looks like my only option thus far is go through all InputDialogs. Can anyone give me an idea how to finish checking loanAmount for valid data and if invalid to loop back to its InputDialog to re-enter stuff, pheew that was a long one:-)
Thanks much!
Dököll
Please disregard this one; rahter than ending the loop at the end of the class, ending it as an item by itself allows the loop to continue and demands for legal data. I think I was trying to envelope all loops together, together this time did not work:
[CODE=JAVA]
//An attempt to ensure user adds in amount from 100 to 1000000, no more, no less...
while (loanAmount < 100 || loanAmount > 1000000 ) {
inputStr = JOptionPane.sho wInputDialog(nu ll, "Loan Amount (Dollars + Cents):");
try {
loanAmount = Double.parseDou ble(inputStr);
// catch the user's entry here and throw an error
//User will need to re-enter data to continue
if (loanAmount < 100 || loanAmount > 1000000 ) {
throw new Exception("Loan Amount is invalid, please try again...");
}
//Catch instances were user either adds in vowels or leaves pop up empty
//Our user will need to re-enter information but the correct data type, digits only...
} catch (NumberFormatEx ception e) {
JOptionPane.sho wMessageDialog( null, "'" + inputStr
+ "' is an invalid data type\n"
+ "Please enter digits only");
//Catch instances were user either adds in vowels or leaves pop up empty
//Our user will need to re-enter information but the correct data type, digits only...
} catch (Exception e) {
}//This braket is therefore needed to end the loop for the loanAmount.
//Even though the InputDialog does in fact come it is annoying having to wait
//to see the box again. This one works better:-)
[/CODE]
In a bit, I have another brain tease; to my standards anyway:-)
Last edited by Dököll; Apr 6 '08, 11:36 PM.
Reason: text...
I have been toying with this bit of code in my book to allow a non-fixed array to become fixed and vise versa, just need some direction of how to go about it:
[CODE=JAVA]
int automobile; //declare automobile array
int [] number; //declare array size/number of automobiles here
number = Integer.parseIn t(
JOptionPane.sho wInputDialog(nu ll,"Enter as many automobiles needed, we suggest 3 at most:"));
number = new int[automobile];
...
[/CODE]
I added the number right in here: [ ] , just to see, it did not like it there:-)
What are your thoughts?
Köll
Last edited by Dököll; Apr 7 '08, 12:04 AM.
Reason: code...
I have been toying with this bit of code in my book to allow a non-fixed array to become fixed and vise versa, just need some direction of how to go about it:
[CODE=JAVA]
int automobile; //declare automobile array
int [] number; //declare array size/number of automobiles here
number = Integer.parseIn t(
JOptionPane.sho wInputDialog(nu ll,"Enter as many automobiles needed, we suggest 3 at most:"));
number = new int[automobile];
...
[/CODE]
I added the number right in here: [ ] , just to see, it did not like it there:-)
What are your thoughts?
Köll
My thoughts are that you don't really understand what objects and arrays are
all about. When you declare a non-primitive variable you actually have a remote
control to your television but you don't have a television yet. All TVs so to speak,
in Java, have to be 'new'd. That creates a TV for you and you can assign it to
your remote control (variable).
Also with arrays, which are objects. you have to 'new' them before you can
manipulate them with your remote control. Arrays always have a fixed size that
you set when you 'new' the array. There are no 'unfixed' arrays, they don't exist.
Java uses two different syntaxes for array remote controls:
[code=java]
int cStyle[];
int[] javaStyle;
[/code]
The first style is made to make those old C programmers feel more comfortable
but the second style is the prefered one. Both styles create a TV remote control
but there's no TV yet.
If you want to make an array larger or shorter you have to 'new' another array,
copy the corresponding elements from the old to the new array and set the
remote control to point to the new array. That's exactly what the ArrayList does.
There is no magic behind the scenes.
Arrays are a bit special objects w.r.t. their methods: arrays don't have them, i.e.
they're plain data objects and you even can't extend them by subclassing them
(there is nothing to subclass).
When you 'new' an array you can specify any number >= 0 because creating
arrays is done at runtime, not at compile time; that adds a bit more flexibility
than you have in C because in that language the size of the array must be
known to the compiler and therefore must be the value of a constant expression.
If I were to have the need to set my arrary size I would achieve it by doing this:
[CODE=JAVA]
Automobile [] automobile; //declare automobile array
automobile = new Automobile[5]; //create automobile array
[/CODE]
What JosAH said. Note how close you are from getting it right. Take your fixed size example and rewrite it as:
[CODE=JAVA]
Automobile [] automobile;
int automobileSize = ...; //compute size
automobile = new Automobile[automobileSize];
[/CODE]
or
[CODE=JAVA]
int automobileSize = ...; //compute size
Automobile [] automobile = new Automobile[automobileSize];
[/CODE]
Comment