Results 1 to 6 of 6
  1. #1
    Join Date
    Apr 2008
    Posts
    7
    Rep Power
    0

    Default 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?

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

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Why did you use BigDecimal there. For me there is no point to use. Because you just prompt some information, not processing.

  3. #3
    Zosden's Avatar
    Zosden is offline Senior Member
    Join Date
    Apr 2008
    Posts
    384
    Rep Power
    7

    Default

    just do if number == 1 || number == 2 etc)
    then whatever
    else
    {
    whatever
    My IP address is 127.0.0.1

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

  5. #5
    Join Date
    Apr 2008
    Posts
    7
    Rep Power
    0

    Default

    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:
    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);
    	}
    thanks for replying so quickly everyone!!

    (^_^)V

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    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

  1. BigDecimal question
    By orchid in forum New To Java
    Replies: 2
    Last Post: 08-12-2008, 02:44 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •