Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 04-28-2008, 10:59 AM
Member
 
Join Date: Apr 2008
Posts: 7
shinjitsunohana is on a distinguished road
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?

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); } }
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.

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!
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 04-28-2008, 11:05 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,609
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
Why did you use BigDecimal there. For me there is no point to use. Because you just prompt some information, not processing.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Has someone helped you? Then you can
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
their helpful post.

Want to make your IDE the best?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 04-28-2008, 11:07 AM
Zosden's Avatar
Senior Member
 
Join Date: Apr 2008
Posts: 386
Zosden is on a distinguished road
just do if number == 1 || number == 2 etc)
then whatever
else
{
whatever
__________________
My IP address is 127.0.0.1
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 04-28-2008, 11:09 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,609
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
JOptinPane return a String. So convert into an int value can do the comparing.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Has someone helped you? Then you can
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
their helpful post.

Want to make your IDE the best?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 04-28-2008, 11:59 AM
Member
 
Join Date: Apr 2008
Posts: 7
shinjitsunohana is on a distinguished road
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:
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); }
thanks for replying so quickly everyone!!

(^_^)V
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 04-28-2008, 12:12 PM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,609
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
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.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Has someone helped you? Then you can
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
their helpful post.

Want to make your IDE the best?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
BigDecimal question orchid New To Java 2 08-12-2008 03:44 PM


All times are GMT +3. The time now is 10:54 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org