Results 1 to 6 of 6
- 04-28-2008, 08:59 AM #1
Member
- Join Date
- Apr 2008
- Posts
- 7
- Rep Power
- 0
BigDecimal - Help formulating if statments
Hi, i'm new here, just joined and this is my first post. I'm working on a project for my first java class and i've run into a bit of a snag.
I'm trying to make an if statement using the BigDecimal class.
The issue is that i can only accept inputs of 1, 2, 8 etc.
How do i form an if statement so I can Display an error message only when an invalid entry is made?
at the end of the try statment there is a finally block, the first els if statement is where is have ended, not knowing how to progress.Java Code:nbrPrintsInput = JOptionPane.showInputDialog("You can order 1, 2, 8, 10, 25, 30, or 50 Prints\n" + "Please Enter a Valid Number of Prints"); try{ nbrPrints = new BigDecimal (nbrPrintsInput); }catch (NumberFormatException nfExRollsIn){ JOptionPane.showMessageDialog(null, "Error: You Have Entered an Invalid Quantity\n" + "Exiting Program\n" + "Please Start Over"); System.exit(0); }catch (Exception exRollsIn){ JOptionPane.showMessageDialog(null, "Error: You Have Entered an Invalid Quantity\n" + "Exiting Program\n" + "Please Start Over"); System.exit(0); }finally{ if (nbrPrintsInput == null){ JOptionPane.showMessageDialog(null, "Now Exiting Program"); System.exit(0); }else if(nbrPrints != new BigDecimal("1")){ JOptionPane.showMessageDialog(null, "Error: You Have Entered an Invalid Quantity\n" + "Exiting Program\n" + "Please Start Over"); System.exit(0); } }
On a side note i find it strange that our professor told us to have the program close when invalid entries occur, when it's just as easy to make a loop and have the user reenter the information
(^_^)V ~ Thanks for your time guys, nice to meet ya!
- 04-28-2008, 09:05 AM #2
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Why did you use BigDecimal there. For me there is no point to use. Because you just prompt some information, not processing.
- 04-28-2008, 09:07 AM #3
just do if number == 1 || number == 2 etc)
then whatever
else
{
whateverMy IP address is 127.0.0.1
- 04-28-2008, 09:09 AM #4
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
JOptinPane return a String. So convert into an int value can do the comparing.
- 04-28-2008, 09:59 AM #5
Member
- Join Date
- Apr 2008
- Posts
- 7
- Rep Power
- 0
i used BigDecimal because it is required to do some calculations later in the code. But then i figured it is better to just start off with an int and convert it to BigDecimal when/if needed.
turns out i didn't need it (newbie mistake) so i kept the int and used this code:thanks for replying so quickly everyone!!Java Code:nbrPrintsInt = Integer.parseInt(nbrPrintsInput); if (nbrPrintsInt != 1&nbrPrintsInt != 2&nbrPrintsInt != 8&nbrPrintsInt != 10& nbrPrintsInt != 25&nbrPrintsInt != 30&nbrPrintsInt != 50){ JOptionPane.showMessageDialog(null, "Error: You Have Entered an Invalid Quantity\n" + "Exiting Program\n" + "Please Start Over"); System.exit(0); }
(^_^)V
- 04-28-2008, 10:12 AM #6
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
That's good choice pal. Converting variable type from one to another is not much difficult in Java. So all the time choose the most suitable data type.
At the time, if you solved the problem please mark the thread as SOLVED, from the thread tools. :)
Similar Threads
-
BigDecimal question
By orchid in forum New To JavaReplies: 2Last Post: 08-12-2008, 01:44 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks