• Post Reply Bookmark Topic Watch Topic
  • New Topic

Help w/ Data Input Error

 
Tonia Billiot
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have been having a problem w/ the code below. For reasons unknown to me, I get the error at the beginning of program RUN. It is not allowing time for the user to input the digits, so obviously I am missing something in the code. What, I don't know. This is the error I am recieving: Exception in thread "main" java.lang.NumberFormatException: empty String
at java.lang.FloatingDecimal.readJavaFormatString(Unknown Source)
at java.lang.Double.parseDouble(Unknown Source)
at SalesTaxWizard.useCardMethod(SalesTaxWizard.java:146)
at SalesTaxWizard.main(SalesTaxWizard.java:12)

Run down on code: Panel 2 allows the user to input the amount of grocery & non-grocery sales. Panel three shows the calculations made w/ the input from panel two. Also, I tried to System.out.println(the users input, but get nothing in return), and have also lost panel 3 when I un-comment the Double.parseDouble command.

All help is greatly appreciated. Thanks in advance...
 
David Harkness
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah I see what's going on. Your code is building a bunch of Swing GUI elements, but then you're immediately trying to make use of the user's input. What you need to do is build your GUI and let the main thread complete (end). At this point Swing's event thread will take over and accept user input.

You can add a JButton for the user to press when they're done inputing information, and use an ActionListener to receive the button click and continue doing the work you want.

Another option is to skip creating your own GUI and go for JOptionPane which will simply display a window with a prompt and a single entry field, wait for input, and return it to you.

Let us know which way you want to go, and you might find the Swing forum helpful if you continue with the full GUI.
 
Tonia Billiot
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi David, I�ve done something similar (tax calculations w/ user input) using the JOptionPane in my first Java class and it was easier for me that way. I have to use the JPanels & JFrame on this assignment. This is my ActionPerformed Method:


Let us know which way you want to go, and you might find the Swing forum helpful if you continue with the full GUI.
I will take your advice and post this in the swing forum.
Thanks again.
[ April 27, 2005: Message edited by: Tonia Billiot ]
 
David Harkness
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do I understand correctly that you already had the actionPerformed() method? If so, then you need to add a button "do it" that takes the user's input and processes it.

In your GUI construction code, you cannot dosince the user hasn't had a chance to even see the text field, let alone enter data. You need to move all the calculation code to a method that gets called when the user clicks the "do it" button.

Am I making sense?
 
Tonia Billiot
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I understand correctly, since the 2nd panel is where the user inputs their numbers, I can add the calculation code to my next button on the 2 panel in the ActionPerformed method. The next button can set off the calculations and bring the user to panel 3.

In your GUI construction code, you cannot do
grocVal1 = Double.parseDouble(txtGrsTot.getText());


Can I add the Double.parseDouble to the action performed method(w/ the other calculation code), or is there another way that I have to go?

As you already know, I've been trying to figure this out for a while now. Thanks very much for your assistance.
 
Tonia Billiot
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The program is now working correctly. I just have a few finishing touches. Hopefully, I can do them on my own. If not, I know where to come..

Thank-you David for all of your assistance. I greatly appreciate you taking the time to help me.
 
David Harkness
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Glad I could help! It's a great feeling to help someone learn. As well, it's very gratifying to see when someone "gets it" as you have since you got it working without my reply.

To be complete, however, yes you can put the code in the Next button's actionPerformed. It really comes down to the application needs. If the third panel needs to show calculations performed on the second panel's fields, then you have no choice but to do it there. If not, you can wait to the end.

However, in either case, you can make a nicer user experience by validating the input when the user clicks Next, whether or not you do the calculations. For example, add an initially empty JLabel (errorDisplay) somewhere on the panel. In the code to go to the third panel, check the input, and if it's not a valid double, abort the panel switch and instead display the error. You could alternatively show a dialog box instead of using a label.This allows you to check multiple fields and continue adding validation errors so they can all be displayed at once. You can get trickier (put label below/next to each field, change label colors for fields with errors, let user continue and have a final error report, etc), but this should give you a basic idea of the possibilities.

By the way, I'm not saying to do this if you don't have free time. This is more so you can see how the simple things you're doing now can integrate with more complex features later. Looking at most software should tell you that these principles are clearly not top priority in most companies.
 
Tonia Billiot
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi David,
I took your advice on the try-catch number format exception and it worked great.
Thanks again David
 
Royce Remer
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'd like to thank you (rather indirectly) for your response David.

I was getting that same java.lang.NumberFormatException: empty String, only I had placed my calculations within the ActionListener. I was stumped.

I mulled over your advice to Tonia, and viola! Turns out I had declared my variables (which included parsing of the textfields) inside the main, so even though all of my calculations were inside the ActionListener - the part that counted (the reference to the empty textfield) wasn't!

Thanks

EDIT: Now I've found it's giving me a related error that doesn't make any sense to me



These errors occur at runtime when I actually click on a button. Any ideas?

Incidentally, is there a way to attach text here? My code is rather long...I'm hoping someone will go "Ah! I know the problem without even seeing the code!"
[ May 07, 2005: Message edited by: Royce Remer ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!